Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joeyjiron06/1a2a6da4e8e51ac02eceab363ef7da67 to your computer and use it in GitHub Desktop.
Save joeyjiron06/1a2a6da4e8e51ac02eceab363ef7da67 to your computer and use it in GitHub Desktop.
Vitest + Jsdom + React Testing Library

Vitest + Jsdom + React Testing Library

TLDR; very quick setup to add vitest, jsdom and react testing library

install dependencies

pnpm i --save-dev vitest jsdom @testing-library/react @testing-library/jest-dom

update package.json

"scripts": {
  "test": "vitest"
}

create test/setup.ts

import { expect, afterEach } from 'vitest';
import { cleanup } from '@testing-library/react';
import '@testing-library/jest-dom/vitest';

afterEach(() => {
  cleanup();
});

update vite config

export default defineConfig({
  test: {
    environment: 'jsdom',
    setupFiles: ['./test/setup.ts'],
    testMatch: ['./src/**/*.test.tsx'],
    globals: true
  }
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment