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.
@@ -113,23 +114,14 @@ Create a vitest.config.ts file in the root directory of your application with th
113
114
114
115
```ts
115
116
importswcfrom'unplugin-swc';
116
-
import { defineConfig } from'vite';
117
+
import { defineConfig } from'vitest/config';
117
118
118
119
exportdefaultdefineConfig({
119
120
test: {
120
121
globals: true,
121
-
alias: {
122
-
'@src': './src',
123
-
'@test': './test',
124
-
},
125
122
root: './',
126
123
},
127
-
resolve: {
128
-
alias: {
129
-
'@src': './src',
130
-
'@test': './test',
131
-
},
132
-
},
124
+
133
125
plugins: [swc.vite()], // This is required to build the test files with SWC
134
126
});
135
127
```
@@ -139,7 +131,23 @@ file for e2e tests, with an additional field `include` that specifies the test p
139
131
140
132
```ts
141
133
importswcfrom'unplugin-swc';
142
-
import { defineConfig } from'vite';
134
+
import { defineConfig } from'vitest/config';
135
+
136
+
exportdefaultdefineConfig({
137
+
test: {
138
+
include: ['**/*.e2e-spec.ts'],
139
+
globals: true,
140
+
root: './',
141
+
},
142
+
plugins: [swc.vite()],
143
+
});
144
+
```
145
+
146
+
Additionaly, you can set `include` and `alias` options to support typescript paths in your tests:
147
+
148
+
```ts
149
+
importswcfrom'unplugin-swc';
150
+
import { defineConfig } from'vitest/config';
143
151
144
152
exportdefaultdefineConfig({
145
153
test: {
@@ -174,11 +182,13 @@ Lastly, update the test scripts in your package.json file to the following:
"test:e2e": "vitest run -c ./vitest.config.e2e.ts"
185
+
"test:e2e": "vitest run --config ./vitest.config.e2e.ts"
178
186
}
179
187
}
180
188
```
189
+
181
190
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
191
183
192
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.
193
+
184
194
> info **Hint** You can check out a working example in this [repository](https://github.com/TrilonIO/nest-vitest)
0 commit comments