From 91ca96e4d4ca96c5cbd27367af926ab2d391c436 Mon Sep 17 00:00:00 2001 From: Chris Beer Date: Wed, 27 Nov 2024 09:33:50 -0800 Subject: [PATCH] Re-unify the test suite --- setupTest.js | 12 ++++++++++++ setupUnitTest.js | 6 ------ vitest.config.ts | 10 ++++++++++ vitest.workspace.ts | 29 ----------------------------- 4 files changed, 22 insertions(+), 35 deletions(-) delete mode 100644 setupUnitTest.js delete mode 100644 vitest.workspace.ts diff --git a/setupTest.js b/setupTest.js index d59b83bbb..b2de7e83f 100644 --- a/setupTest.js +++ b/setupTest.js @@ -2,12 +2,24 @@ import '@testing-library/jest-dom'; import { vi } from 'vitest'; import { setupIntersectionMocking } from 'react-intersection-observer/test-utils'; import i18next from 'i18next'; +import createFetchMock from 'vitest-fetch-mock'; import en from './src/locales/en/translation.json'; // vitest doesn't set a default window.origin = 'http://localhost'; vi.setConfig({ testTimeout: 10_000 }); +const fetchMocker = createFetchMock(vi); + +// changes default behavior of fetchMock to use the real 'fetch' implementation and not mock responses +beforeEach((context) => { + if (context.task.file.name.includes('/integration')) { + fetchMocker.disableMocks(); + } else { + // sets globalThis.fetch and globalThis.fetchMock to our mocked version + fetchMocker.enableMocks(); + } +}); /** */ class Path2D { diff --git a/setupUnitTest.js b/setupUnitTest.js deleted file mode 100644 index 09b0da1a3..000000000 --- a/setupUnitTest.js +++ /dev/null @@ -1,6 +0,0 @@ -import createFetchMock from 'vitest-fetch-mock'; - -const fetchMocker = createFetchMock(vi); - -// sets globalThis.fetch and globalThis.fetchMock to our mocked version -fetchMocker.enableMocks(); diff --git a/vitest.config.ts b/vitest.config.ts index 9ebeba961..af425b156 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -32,4 +32,14 @@ export default defineConfig({ '@tests': fileURLToPath(new URL('./__tests__', import.meta.url)), }, }, + test: { + environment: 'happy-dom', + exclude: ['node_modules'], + fileParallelism: false, + globals: true, + include: ['**/*.test.js', '**/*.test.jsx'], + maxConcurrency: 1, + name: 'unit', + setupFiles: ['./setupTest.js'], + }, }); diff --git a/vitest.workspace.ts b/vitest.workspace.ts deleted file mode 100644 index 54aa48290..000000000 --- a/vitest.workspace.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { defineWorkspace } from 'vitest/config'; // eslint-disable-line import/no-unresolved - -export default defineWorkspace([ - { - // add "extends" to merge two configs together - extends: './vitest.config.js', - test: { - environment: 'happy-dom', - exclude: ['node_modules', '__tests__/integration/**'], - globals: true, - include: ['**/*.test.js', '**/*.test.jsx'], - name: 'unit', - pool: 'forks', - setupFiles: ['./setupTest.js', './setupUnitTest.js'], - }, - }, - { - extends: './vitest.config.js', - test: { - environment: 'happy-dom', - exclude: ['node_modules'], - globals: true, - include: ['__tests__/integration/**/*.test.js', '__tests__/integration/**/*.test.jsx'], - name: 'integration', - pool: 'forks', - setupFiles: ['./setupTest.js'], - }, - }, -]);