Skip to content

Commit

Permalink
🔧 [#724] Configure test setup for jest compatibility
Browse files Browse the repository at this point in the history
* Ensure the test globals are injected (vitest doesn't do this by default)
* Point to setup file to ensure jest matchers are registered (requires
  them to be registered as globals first)
  • Loading branch information
sergei-maertens committed Nov 17, 2024
1 parent a95f466 commit 8620c3b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/vitest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom';

import mswServer from 'api-mocks/msw-server';

beforeAll(() => mswServer.listen());
afterEach(() => mswServer.resetHandlers());
afterAll(() => mswServer.close());
6 changes: 6 additions & 0 deletions vite.config.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference types="vitest/config" />
// https://vitejs.dev/config/
import react from '@vitejs/plugin-react';
import lodashTemplate from 'lodash/template';
Expand Down Expand Up @@ -30,4 +31,9 @@ export default defineConfig({
},
},
],
test: {
environment: 'jsdom',
globals: true, // for compatibility with jest
setupFiles: ['./src/vitest.setup.js'],
},
});

0 comments on commit 8620c3b

Please sign in to comment.