Skip to content

docs(swc): add vitest alias resolution configuration #3204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions content/recipes/swc.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ export default defineConfig({
module: { type: 'es6' },
}),
],
resolve: {
alias: {
// Ensure Vitest correctly resolves TypeScript path aliases
'src': resolve(__dirname, './src'),
},
},
});
```

Expand Down Expand Up @@ -326,6 +332,23 @@ export default defineConfig({
});
```

### 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:

```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.
Expand All @@ -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.
Expand Down