-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@nestjs/testing doesn't work with vitest #9228
Comments
|
@jmcdo29 that's not it, I could even test custom decorators with vitest and use decorators in tests |
Also that's the only problematic case with nest, if you check comments in vitest issue, it works fine when using test module for mocking providers of service, but not for controller |
It is actually the problem, believe it or not. Esbuild has partial decorator support, but it does not support import { Injectable } from "@nestjs/common";
@Injectable()
export class Users2Service {
foo(): string {
return "foo";
}
} And inject it into the @Injectable()
export class UsersService {
constructor(
@InjectModel(User)
private readonly userModel: typeof User,
private readonly user2: Users2Service
) {}
user2foo(): string {
return this.user2.foo();
}
...
} and then create a import { Test, TestingModule } from "@nestjs/testing";
import { User } from "./models/user.model";
import { UsersService } from "./users.service";
import { getModelToken } from "@nestjs/sequelize";
import { Users2Service } from "./user2.service";
import { beforeEach, describe, it, expect } from "vitest";
import * as vi from "vitest";
const usersArray = [
{
firstName: "firstName #1",
lastName: "lastName #1",
},
{
firstName: "firstName #3",
lastName: "lastName #2",
},
];
const oneUser = {
firstName: "firstName #1",
lastName: "lastName #1",
};
describe("UserService", () => {
let service: UsersService;
let model: typeof User;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
UsersService,
{
provide: getModelToken(User),
useValue: {
findAll: vi.fn(() => usersArray),
findOne: vi.fn(),
create: vi.fn(() => oneUser),
remove: vi.fn(),
destroy: vi.fn(() => oneUser),
},
},
{
provide: Users2Service,
useValue: {
foo: () => "test foo",
},
},
],
}).compile();
service = module.get<UsersService>(UsersService);
model = module.get<typeof User>(getModelToken(User));
});
it("should be defined", () => {
expect(service).toBeDefined();
});
describe("user2foo", () => {
it("should return test foo", () => {
vi.expect(service.user2foo()).toBe("test foo");
});
});
}); And run it using
Meaning that Nest was not able to inject the You can use |
Thanks much, now it's crystal clear for me. Will see how can this be fixed from vite side |
@wight554 |
@gabriiels you can see this doc. https://docs.nestjs.com/recipes/swc#vitest |
You can use Vitest with Nest as long as you have the SWC plugin enabled https://docs.nestjs.com/recipes/swc#vitest |
Is there an existing issue for this?
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 vitest-dev/vitest#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
@nestjs/common
@nestjs/core
@nestjs/microservices
@nestjs/platform-express
@nestjs/platform-fastify
@nestjs/platform-socket.io
@nestjs/platform-ws
@nestjs/testing
@nestjs/websockets
Other package
vitest
NestJS version
8.2.3
Packages versions
Node.js version
v14.19.0
In which operating systems have you tested?
Other
No response
The text was updated successfully, but these errors were encountered: