Skip to content

Commit b0b8d77

Browse files
Merge pull request #3204 from mag123c/docs(recipes)vitest-alias
docs(swc): add vitest alias resolution configuration
2 parents d51e129 + 9f3bb1f commit b0b8d77

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

content/recipes/swc.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,12 @@ export default defineConfig({
280280
module: { type: 'es6' },
281281
}),
282282
],
283+
resolve: {
284+
alias: {
285+
// Ensure Vitest correctly resolves TypeScript path aliases
286+
'src': resolve(__dirname, './src'),
287+
},
288+
},
283289
});
284290
```
285291

@@ -326,6 +332,23 @@ export default defineConfig({
326332
});
327333
```
328334

335+
### Path aliases
336+
337+
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:
338+
339+
```ts
340+
import { resolve } from 'path';
341+
342+
export default defineConfig({
343+
resolve: {
344+
alias: {
345+
'src': resolve(__dirname, './src'),
346+
},
347+
},
348+
});
349+
```
350+
This ensures that Vitest correctly resolves module imports, preventing errors related to missing dependencies.
351+
329352
#### Update imports in E2E tests
330353

331354
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:
344367
}
345368
```
346369

370+
347371
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.
348372

349373
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.

0 commit comments

Comments
 (0)