@nestjs/testing doesn't work with vitest
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Current behavior
When trying to run vitest with @nestjs/testing, it throws errors due to UserService being undefined inside of UserController. I tried doing deep digging into nestjs, comparing jest and vitest but can’t really get enough info for debugging. Decided to create this issue to help tracking/fixing https://github.com/vitest-dev/vitest/issues/708
Minimum reproduction code
https://github.com/wight554/vitest-nestjs-testing
Steps to reproduce
npx vitest run src/users/users.controller.vitest.spec.ts
Expected behavior
vitest should work with @nestjs/testing
Package
- I don’t know. Or some 3rd-party package
-
@nestjs/common -
@nestjs/core -
@nestjs/microservices -
@nestjs/platform-express -
@nestjs/platform-fastify -
@nestjs/platform-socket.io -
@nestjs/platform-ws -
@nestjs/testing -
@nestjs/websockets - Other (see below)
Other package
vitest
NestJS version
8.2.3
Packages versions
_ _ _ ___ _____ _____ _ _____
| \ | | | | |_ |/ ___|/ __ \| | |_ _|
| \| | ___ ___ | |_ | |\ `--. | / \/| | | |
| . ` | / _ \/ __|| __| | | `--. \| | | | | |
| |\ || __/\__ \| |_ /\__/ //\__/ /| \__/\| |_____| |_
\_| \_/ \___||___/ \__|\____/ \____/ \____/\_____/\___/
[System Information]
OS Version : macOS Monterey
NodeJS Version : v14.19.0
NPM Version : 6.14.16
[Nest CLI]
Nest CLI Version : 8.1.5
[Nest Platform Information]
platform-express version : 8.2.3
schematics version : 8.0.5
sequelize version : 8.0.0
testing version : 8.2.3
common version : 8.2.3
core version : 8.2.3
cli version : 8.1.5
Node.js version
v14.19.0
In which operating systems have you tested?
- macOS
- Windows
- Linux
Other
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
vitest doesn't work properly with @nestjs/testing #708 - GitHub
Describe the bug Running vitest on any sample app from https://github.com/nestjs/nest/tree/master/sample won't run well with testing modules ...
Read more >Testing | NestJS - A progressive Node.js framework
Automated testing is considered an essential part of any serious software development effort. Automation makes it easy to repeat individual tests or test...
Read more >Test your TypeScript type with Vitest : r/javascript - Reddit
Unfortunately NestJS doesn't support vitest.
Read more >Method unit testing failing in nestjs - Stack Overflow
controller.currentUser is a function. It takes in the Request object and returns request.user . Your test should look something like
Read more >Testing Vite with minimal config using Vitest - LogRocket Blog
Let's discuss how Vitest works, compare it with a popular test suite configuration (Jest and Babel), explore the ways Vitest can simplify ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It is actually the problem, believe it or not. Esbuild has partial decorator support, but it does not support
emitDecoratorMetadata, which is how Nest automagically knows how to inject providers based on classes. With the@Exclude(),class-transformeris reading metadata specifically set for the property, not anything about the property type. For the@InjectModel(), once again, metadata is manually set for the parameter so Nest knows the injection token to use. If we were to create a new serviceUsers2Service, like so:And inject it into the
UsersServiceand add auser2Footo call thefoomethodand then create a
users.service.vitest.spec.tslike so:And run it using
npx vitest run src/users/users.service.vitest.spec.tswe’ll get the error thatMeaning that Nest was not able to inject the
Users2servicebecause it doesn’t know what type to use.You can use
@Inject()on every one of your dependencies to set the token yourself, so that Nest isn’t trying to read the metadata emitted from the decorator, or you can use a tool that doesn’t useesbuild, because it will not support this feature.Thanks much, now it’s crystal clear for me. Will see how can this be fixed from vite side