Skip to content

Commit e62eec1

Browse files
authored
docs(swc) add vitest integration section
1 parent b72c01d commit e62eec1

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

content/recipes/swc.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,90 @@ If your ORM does not provide a similar workaround, you can define the wrapper ty
9595
*/
9696
export type WrapperType<T> = T; // WrapperType === Relation
9797
```
98+
### Vitest
99+
100+
[Vitest](https://vitest.dev/) is a fast and lightweight test runner designed to work with Vite. It provides a modern, fast, and easy-to-use testing solution that can be integrated with NestJS projects.
101+
102+
#### Installation
103+
104+
To get started, first install the required packages:
105+
106+
```bash
107+
$ npm i --save-dev vitest unplugin-swc @swc/core @vitest/coverage-c8
108+
```
109+
110+
#### Configuration
111+
112+
Create a vitest.config.ts file in the root directory of your application with the following contents:
113+
114+
```ts
115+
import swc from 'unplugin-swc';
116+
import { defineConfig } from 'vite';
117+
118+
export default defineConfig({
119+
test: {
120+
globals: true,
121+
alias: {
122+
'@src': './src',
123+
'@test': './test',
124+
},
125+
root: './',
126+
},
127+
resolve: {
128+
alias: {
129+
'@src': './src',
130+
'@test': './test',
131+
},
132+
},
133+
plugins: [swc.vite()], // This is required to build the test files with SWC
134+
});
135+
```
136+
137+
This configuration file sets up the Vitest environment with the necessary aliases, root directory, and SWC plugin. You should also create a separate configuration
138+
file for e2e tests, with an additional field `include` that specifies the test path regex:
139+
140+
```ts
141+
import swc from 'unplugin-swc';
142+
import { defineConfig } from 'vite';
143+
144+
export default defineConfig({
145+
test: {
146+
include: ['**/*.e2e-spec.ts'],
147+
globals: true,
148+
alias: {
149+
'@src': './src',
150+
'@test': './test',
151+
},
152+
root: './',
153+
},
154+
resolve: {
155+
alias: {
156+
'@src': './src',
157+
'@test': './test',
158+
},
159+
},
160+
plugins: [swc.vite()],
161+
});
162+
```
163+
164+
#### Update imports in E2E tests
165+
166+
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.
167+
168+
Lastly, update the test scripts in your package.json file to the following:
169+
170+
```json
171+
{
172+
"scripts": {
173+
"test": "vitest run",
174+
"test:watch": "vitest",
175+
"test:cov": "vitest run --coverage",
176+
"test:debug": "vitest --inspect-brk --inspect --logHeapUsage --threads=false",
177+
"test:e2e": "vitest run -c ./vitest.config.e2e.ts"
178+
}
179+
}
180+
```
181+
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.
182+
183+
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.
184+
> info **Hint** You can check out a working example in this [repository](https://github.com/TrilonIO/nest-vitest)

0 commit comments

Comments
 (0)