From f68f3e1d64c69a008974b1448011d08d2e49c504 Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Sun, 17 Nov 2024 22:47:46 +0100 Subject: [PATCH] :technologist: [#724] Use aliases for tests When using relative imports, vite/vitest complain about ESM modules being used through a CJS package, and setting type: module in package.json breaks a lot of other things. Renaming the module to .mts pushes the problem down the chain. It seems that using the import alias seems to work to resolve the api-mocks package, but there are plenty of other problems... --- .prettierrc.json | 2 +- jsconfig.json | 5 ++++- src/vitest.setup.js | 2 +- vite.config.mts | 3 +++ 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.prettierrc.json b/.prettierrc.json index bc51dad44..560d768c9 100644 --- a/.prettierrc.json +++ b/.prettierrc.json @@ -12,7 +12,7 @@ "tabWidth": 2, "trailingComma": "es5", "useTabs": false, - "importOrder": ["^((api-mocks|components|data|formio|hooks|map|story-utils|types)/(.*)|(api|api-mocks|cache|Context|errors|headers|i18n|sdk|sentry|types|utils))$", "^[./]"], + "importOrder": ["^@/.*", "^((api-mocks|components|data|formio|hooks|map|story-utils|types)/(.*)|(api|api-mocks|cache|Context|errors|headers|i18n|sdk|sentry|types|utils))$", "^[./]"], "importOrderSeparation": true, "importOrderSortSpecifiers": true } diff --git a/jsconfig.json b/jsconfig.json index 5875dc5b6..01a79527b 100644 --- a/jsconfig.json +++ b/jsconfig.json @@ -1,6 +1,9 @@ { "compilerOptions": { - "baseUrl": "src" + "baseUrl": "src", + "paths": { + "@/*": ["./*"] + }, }, "include": ["src"] } diff --git a/src/vitest.setup.js b/src/vitest.setup.js index 493ad8515..8076d16c2 100644 --- a/src/vitest.setup.js +++ b/src/vitest.setup.js @@ -4,7 +4,7 @@ // learn more: https://github.com/testing-library/jest-dom import '@testing-library/jest-dom'; -import mswServer from 'api-mocks/msw-server'; +import mswServer from '@/api-mocks/msw-server'; beforeAll(() => mswServer.listen()); afterEach(() => mswServer.resetHandlers()); diff --git a/vite.config.mts b/vite.config.mts index 228685f04..a964490a7 100644 --- a/vite.config.mts +++ b/vite.config.mts @@ -35,5 +35,8 @@ export default defineConfig({ environment: 'jsdom', globals: true, // for compatibility with jest setupFiles: ['./src/vitest.setup.js'], + alias: { + '@/': new URL('./src/', import.meta.url).pathname, + }, }, });