0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

React + TypeScript + Vite + Vitest + ReactTestingLibrary + jest-dom + jsdom

Last updated at Posted at 2024-10-20

React + TypeScript + Viteで作成したフロントエンドのテスト

Vitest + ReactTestingLibrary + jest-dom + jsdomを使用

※React + TypeScript + Viteでフロントエンドを作成している前提です。

必要なライブラリをインストール

  • Vitestのインストール(公式)
npm install -D vitest
npm install -D @testing-library/react @testing-library/dom @testing-library/user-event @types/react @types/react-dom
  • jest-domのインストール(公式)。toBeIntheDocument等を使いたいため。
npm install -D @testing-library/jest-dom
  • jsdomのインストール(公式)
npm install -D jsdom

設定ファイル

vite.config.ts
/// <reference types="vitest" />
import { defineConfig } from 'vite'

export default defineConfig({
  test: {
    // ...
  },
})
vitest-setup.ts
import '@testing-library/jest-dom/vitest'
vite.config.ts
test: {
  // ...
  setupFiles: ['./vitest-setup.ts'], 
}
tsconfig.app.json
"include": [
  // ...
  "./vitest-setup.ts"
],
vite.config.ts
test: {
  // ...
  environment: 'jsdom',
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?