From 8620c3bfa9a9dad73ec22563dce6b30162cff419 Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Sun, 17 Nov 2024 20:51:02 +0100 Subject: [PATCH] :wrench: [#724] Configure test setup for jest compatibility * 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) --- src/vitest.setup.js | 11 +++++++++++ vite.config.mts | 6 ++++++ 2 files changed, 17 insertions(+) create mode 100644 src/vitest.setup.js diff --git a/src/vitest.setup.js b/src/vitest.setup.js new file mode 100644 index 000000000..493ad8515 --- /dev/null +++ b/src/vitest.setup.js @@ -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()); diff --git a/vite.config.mts b/vite.config.mts index 1e9c1e8d0..228685f04 100644 --- a/vite.config.mts +++ b/vite.config.mts @@ -1,3 +1,4 @@ +/// // https://vitejs.dev/config/ import react from '@vitejs/plugin-react'; import lodashTemplate from 'lodash/template'; @@ -30,4 +31,9 @@ export default defineConfig({ }, }, ], + test: { + environment: 'jsdom', + globals: true, // for compatibility with jest + setupFiles: ['./src/vitest.setup.js'], + }, });