vitest doesn't work properly with @nestjs/testing

See original GitHub issue

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 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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:17 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
V-edcommented, May 5, 2022

Use swc plugin

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!

0reactions
sheremet-vacommented, May 5, 2022

After writing the previous message, I decided to look up how Vite uses esbuild. Turns out, Vite uses transform function of esbuild that transforms a single file, and doesn’t support plugins.

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:

  • Build files with esbuild as a bundler (which might require a lot of custom options), and run tests against it
  • Use swc plugin

Closing the issue, since it is out of scope of Vitest.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found