Skip to content

Simple project and test case doesn't work with vitest even with the setup done according to documentation #14653

@abnud11

Description

@abnud11

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I have these two modules:

// a.module.ts
import { Module } from '@nestjs/common';
import { BModule } from 'src/b/b.module';
import { AService } from './a.service';

@Module({
  imports: [BModule],
  providers: [AService],
  exports: [AService],
})
export class AModule {}


// b.module.ts
import { Module } from '@nestjs/common';
import { BService } from './b.service';

@Module({
  providers: [BService],
  exports: [BService],
})
export class BModule {}

And this is AppModule:

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AModule } from './a/a.module';

@Module({
  imports: [AModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

AService has a dependency on BService through its constructor, BService has no dependency, AppService has a dependency on AService through its constructor too.

The above code starts successfully with pnpm start:dev and I can see that AppController responds with Hello World printed from AService .getHello method.

But in this simple test code using vitest:

import { Test } from '@nestjs/testing';
import { AppModule } from './app.module';
import { AService } from './a/a.service';
describe('AppModule', () => {
  it('resolves AService', async () => {
    const module = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();
    expect(module.get(AService)).toBeDefined();
  });
});

The test code fails at the .compile line with this error:

Error: Nest can't resolve dependencies of the AService (?). Please make sure that the argument BService at index [0] is available in the AModule context.

Potential solutions:

  • Is AModule a valid NestJS module?
  • If BService is a provider, is it part of the current AModule?
  • If BService is exported from a separate @module, is that module imported within AModule?
    @module({
    imports: [ /* the Module containing BService */ ]
    })

❯ TestingInjector.lookupComponentInParentModules node_modules/.pnpm/@nestjs+core@11.0.10_@nestjs[email protected][email protected]_rxjs@7.8.1__@nestjs+2b60c0076d4765d97d2800d60ae176d5/node_modules/@nestjs/core/injector/injector.js:262:19
❯ TestingInjector.resolveComponentInstance node_modules/.pnpm/@nestjs[email protected]
@nestjs[email protected][email protected]_rxjs@7.8.1__@nestjs+2b60c0076d4765d97d2800d60ae176d5/node_modules/@nestjs/core/injector/injector.js:215:33
❯ TestingInjector.resolveComponentInstance node_modules/.pnpm/@nestjs[email protected]
@nestjs[email protected][email protected]_rxjs@7.8.1__@nest_e6432f44fe446d0673d4fb9f30af2e0f/node_modules/@nestjs/testing/testing-injector.js:19:45
❯ resolveParam node_modules/.pnpm/@nestjs+core@11.0.10_@nestjs[email protected][email protected]_rxjs@7.8.1__@nestjs+2b60c0076d4765d97d2800d60ae176d5/node_modules/@nestjs/core/injector/injector.js:129:38
❯ TestingInjector.resolveConstructorParams node_modules/.pnpm/@nestjs[email protected]
@nestjs[email protected][email protected]_rxjs@7.8.1__@nestjs+2b60c0076d4765d97d2800d60ae176d5/node_modules/@nestjs/core/injector/injector.js:144:27
❯ TestingInjector.loadInstance node_modules/.pnpm/@nestjs[email protected]
@nestjs[email protected][email protected]_rxjs@7.8.1__@nestjs+2b60c0076d4765d97d2800d60ae176d5/node_modules/@nestjs/core/injector/injector.js:70:13
❯ TestingInjector.loadProvider node_modules/.pnpm/@nestjs[email protected]
@nestjs[email protected][email protected]_rxjs@7.8.1__@nestjs+2b60c0076d4765d97d2800d60ae176d5/node_modules/@nestjs/core/injector/injector.js:98:9
❯ TestingInjector.lookupComponentInImports node_modules/.pnpm/@nestjs[email protected]
@nestjs[email protected][email protected]_rxjs@7.8.1__@nestjs+_2b60c0076d4765d97d2800d60ae176d5/node_modules/@nestjs/core/injector/injector.js:297:17

This is my vitest.config.mts:

import { defineConfig } from 'vitest/config';
import swc from 'unplugin-swc';
export default defineConfig({
  test: {
    globals: true,
  },
  plugins: [swc.vite({ module: { type: 'es6' } })],
});

The above is done as the documentation says here:
https://docs.nestjs.com/recipes/swc#vitest

Minimum reproduction code

https://github.com/abnud11/nestjs-vitest-problem

Steps to reproduce

  1. pnpm install
  2. pnpm test

Expected behavior

The test code should succeed.

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

No response

NestJS version

11.0.10

Packages versions

"dependencies": {
    "@nestjs/common": "^11.0.10",
    "@nestjs/core": "^11.0.10",
    "@nestjs/platform-express": "^11.0.10",
    "reflect-metadata": "^0.2.2",
    "rxjs": "^7.8.1"
  },
  "devDependencies": {
    "@eslint/eslintrc": "^3.2.0",
    "@eslint/js": "^9.20.0",
    "@nestjs/cli": "^11.0.3",
    "@nestjs/schematics": "^11.0.1",
    "@nestjs/testing": "^11.0.10",
    "@swc/cli": "^0.6.0",
    "@swc/core": "^1.10.16",
    "@types/express": "^5.0.0",
    "@types/jest": "^29.5.14",
    "@types/node": "^22.13.4",
    "@types/supertest": "^6.0.2",
    "eslint": "^9.20.1",
    "eslint-config-prettier": "^10.0.1",
    "eslint-plugin-prettier": "^5.2.3",
    "globals": "^15.15.0",
    "prettier": "^3.5.1",
    "source-map-support": "^0.5.21",
    "supertest": "^7.0.0",
    "ts-jest": "^29.2.5",
    "ts-loader": "^9.5.2",
    "ts-node": "^10.9.2",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.7.3",
    "typescript-eslint": "^8.24.0",
    "unplugin-swc": "^1.5.1",
    "vitest": "^3.0.5"
  },
  "packageManager": "[email protected]"

Node.js version

22.13.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

I'm posting this as a bug here because I think it's a documentation bug. The documentation here:
https://docs.nestjs.com/recipes/swc#vitest

It gives the impression that Vitest works and that the SWC plugin is enough, but this seems wrong. I suggest either fixing the documentation to state something extra that should be done to make the test work or just saying that vitest is not supported and explaining why.

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageThis issue has not been looked into

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions