Skip to content

Commit 4565b6e

Browse files
Merge pull request #2700 from thiagomini/patch-7
docs(swc) add vitest integration section
2 parents d2defc2 + 363f57d commit 4565b6e

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

content/recipes/swc.md

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

0 commit comments

Comments
 (0)