vitest doesn't work properly with @nestjs/testing
See original GitHub issueDescribe the bug
Running vitest on any sample app from https://github.com/nestjs/nest/tree/master/sample won’t run well with testing modules like this
const module: TestingModule = await Test.createTestingModule({
controllers: [CatsController],
providers: [
{
provide: CatsService,
useValue: {
create: vi.fn().mockResolvedValue(createCatDto),
},
},
],
}).compile();
and leads to errors like this:
FAIL src/users/users.controller.spec.ts > UsersController > create() > should create a user
TypeError: Cannot read properties of undefined (reading 'create')
❯ UsersController.create src/users/users.controller.ts:12:29
10| @Post()
11| create(@Body() createUserDto: CreateUserDto): Promise<User> {
12| return this.usersService.create(createUserDto);
| ^
13| }
14|
Might be related to nestjs itself, but runs well on jest
Reproduction
https://github.com/wight554/vitest-nestjs-testing
System Info
System:
OS: macOS 12.2
CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
Memory: 118.12 MB / 32.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.13.2 - ~/.nvm/versions/node/v16.13.2/bin/node
Yarn: 1.22.15 - ~/.nvm/versions/node/v16.13.2/bin/yarn
npm: 8.1.2 - ~/.nvm/versions/node/v16.13.2/bin/npm
Watchman: 2021.10.04.00 - /usr/local/bin/watchman
Browsers:
Chrome: 98.0.4758.80
Edge: 97.0.1072.76
Safari: 15.3
npmPackages:
vite: ^2.7.13 => 2.7.13
vitest: ^0.2.8 => 0.2.8
Used Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:17 (4 by maintainers)
Top Results From Across the Web
Testing | NestJS - A progressive Node.js framework
Testing. Automated testing is considered an essential part of any serious software development effort. Automation makes it easy to repeat individual tests ......
Read more >Test your TypeScript type with Vitest : r/javascript - Reddit
Unfortunately NestJS doesn't support vitest.
Read more >Getting started with NestJS, Vite, and esbuild - LogRocket Blog
To be sure everything is still working as expected, run the test suite with the npm run test command and we can see...
Read more >Getting Started | Guide - Vitest
Vitest is a blazing fast unit test framework powered by Vite. ... the local setup but doesn't require installing anything on your machine....
Read more >Testing if an event has been triggered in NestJS
Mocking with Jest doesn't seem to work, because we want to test the actual trigger event and not the result. Any suggestions? "node":...
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
With this recommendation, I installed https://github.com/egoist/unplugin-swc and it seems to work fine indeed!
Hoping the decorator proposal progresses faster so esbuild implements it or that vite supports running esbuild plugins so we could use esbuild-plugin-tsc, using esbuild’s fast transpilation while testing with Decorators!
After writing the previous message, I decided to look up how Vite uses
esbuild. Turns out, Vite usestransformfunction of esbuild that transforms a single file, and doesn’t supportplugins.Plugins for esbuild are supported only when using esbuild as a bundler, so Vite actually cannot do anything about it. You can try bundle your files with esbuild and run tests against them, although I don’t think it will be a good DX, but there is nothing Vitest can do to fix this.
To summarize, you can either:
Closing the issue, since it is out of scope of Vitest.