From f4c150e9bee62530b7af4dd360de475335131f35 Mon Sep 17 00:00:00 2001 From: JaeHo Jang Date: Tue, 18 Feb 2025 22:57:44 +0900 Subject: [PATCH 1/2] docs(swc): add vitest alias resolution configuration (nest-14653) --- content/recipes/swc.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/content/recipes/swc.md b/content/recipes/swc.md index 1e2d5c9633..ea1ea36c3a 100644 --- a/content/recipes/swc.md +++ b/content/recipes/swc.md @@ -280,6 +280,12 @@ export default defineConfig({ module: { type: 'es6' }, }), ], + resolve: { + alias: { + // Ensure Vitest correctly resolves TypeScript path aliases + 'src': resolve(__dirname, './src'), + }, + }, }); ``` @@ -326,6 +332,23 @@ export default defineConfig({ }); ``` +### Handling Path Aliases in Vitest + +Unlike Jest, Vitest does not automatically resolve TypeScript path aliases like `src/`. This may lead to dependency resolution errors during testing. To resolve this issue, add the following `resolve.alias` configuration in your `vitest.config.ts` file: + +```ts +import { resolve } from 'path'; + +export default defineConfig({ + resolve: { + alias: { + 'src': resolve(__dirname, './src'), + }, + }, +}); +``` +This ensures that Vitest correctly resolves module imports, preventing errors related to missing dependencies. + #### Update imports in E2E tests Change any E2E test imports using `import * as request from 'supertest'` to `import request from 'supertest'`. This is necessary because Vitest, when bundled with Vite, expects a default import for supertest. Using a namespace import may cause issues in this specific setup. @@ -344,6 +367,7 @@ Lastly, update the test scripts in your package.json file to the following: } ``` + These scripts configure Vitest for running tests, watching for changes, generating code coverage reports, and debugging. The test:e2e script is specifically for running E2E tests with a custom configuration file. With this setup, you can now enjoy the benefits of using Vitest in your NestJS project, including faster test execution and a more modern testing experience. From 9f3bb1faa73084a597af08cd214b8305f7cb8fde Mon Sep 17 00:00:00 2001 From: Kamil Mysliwiec Date: Wed, 19 Feb 2025 09:49:16 +0100 Subject: [PATCH 2/2] Update content/recipes/swc.md --- content/recipes/swc.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/recipes/swc.md b/content/recipes/swc.md index ea1ea36c3a..bff4a5308c 100644 --- a/content/recipes/swc.md +++ b/content/recipes/swc.md @@ -332,7 +332,7 @@ export default defineConfig({ }); ``` -### Handling Path Aliases in Vitest +### Path aliases Unlike Jest, Vitest does not automatically resolve TypeScript path aliases like `src/`. This may lead to dependency resolution errors during testing. To resolve this issue, add the following `resolve.alias` configuration in your `vitest.config.ts` file: