You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[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
+
importswcfrom'unplugin-swc';
117
+
import { defineConfig } from'vitest/config';
118
+
119
+
exportdefaultdefineConfig({
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
+
importswcfrom'unplugin-swc';
133
+
import { defineConfig } from'vitest/config';
134
+
135
+
exportdefaultdefineConfig({
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
+
importswcfrom'unplugin-swc';
149
+
import { defineConfig } from'vitest/config';
150
+
151
+
exportdefaultdefineConfig({
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:
"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