pnpm add -D -E vitest @vitest/browser-playwright @vitejs/plugin-react @testing-library/react @testing-library/dom @testing-library/jest-dom @testing-library/user-event vite-tsconfig-paths msw @vitest/coverage-v8 fishery @faker-js/fakerimport { defineConfig, defineProject } from 'vitest/config'
import { playwright } from '@vitest/browser-playwright'
import react from '@vitejs/plugin-react'
import tsconfigPaths from 'vite-tsconfig-paths'
export default defineConfig({
test: {
projects: [
defineProject({
test: {
name: 'unit',
include: ['**/__tests__/**/*.unit.{test,spec}.ts'],
environment: 'node',
},
}),
defineProject({
plugins: [tsconfigPaths(), react()],
test: {
name: 'browser',
include: ['**/__tests__/**/*.browser.{test,spec}.ts{,x}'],
setupFiles: ['./vitest.setup.ts'],
browser: {
enabled: true,
provider: playwright(),
headless: true,
instances: [{ browser: 'chromium' }],
},
},
}),
],
},
})import '@testing-library/jest-dom/vitest'package.json の scripts を次のように設定することで便利になります。
{
"scripts": {
// ...
"test": "vitest",
"test:coverage": "vitest run --coverage",
"test:run": "vitest run"
// ...
},
}Playwright の実行に必要なブラウザ本体と依存ライブラリを、以下のコマンドでインストールします。
pnpm dlx playwright install --with-depspnpm dlx playwright uninstall --all


![【Next.js × Vitest】Failed to load PostCSS config : [TypeError] Invalid PostCSS Plugin found at: plugins[0]の解決方法 - Qiita](https://public-content.hackersheet.com/website-og-images/321abfb1-0161-4ac8-aa62-3e12a10bce03.jpeg)