admin管理员组文章数量:1335138
I am looking for a way to test my NestJs PlayerController with Jest. My controller and service declaration:
import { QueryBus, CommandBus, EventBus } from '@nestjs/cqrs';
/**
* The service assigned to query the database by means of mands
*/
@Injectable()
export class PlayerService {
/**
* Ctor
* @param queryBus
*/
constructor(
private readonly queryBus: QueryBus,
private readonly mandBus: CommandBus,
private readonly eventBus: EventBus
) { }
@Controller('player')
@ApiUseTags('player')
export class PlayerController {
/**
* Ctor
* @param playerService
*/
constructor(private readonly playerService: PlayerService) { }
My test:
describe('Player Controller', () => {
let controller: PlayerController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [PlayerService, CqrsModule],
controllers: [PlayerController],
providers: [
PlayerService,
],
})pile();
controller = module.get<PlayerController>(PlayerController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
...
Nest can't resolve dependencies of the PlayerService (?, CommandBus, EventBus). Please make sure that the argument at index [0] is available in the PlayerService context.
at Injector.lookupComponentInExports (../node_modules/@nestjs/core/injector/injector.js:180:19)
Any way to work around this dependency issue?
I am looking for a way to test my NestJs PlayerController with Jest. My controller and service declaration:
import { QueryBus, CommandBus, EventBus } from '@nestjs/cqrs';
/**
* The service assigned to query the database by means of mands
*/
@Injectable()
export class PlayerService {
/**
* Ctor
* @param queryBus
*/
constructor(
private readonly queryBus: QueryBus,
private readonly mandBus: CommandBus,
private readonly eventBus: EventBus
) { }
@Controller('player')
@ApiUseTags('player')
export class PlayerController {
/**
* Ctor
* @param playerService
*/
constructor(private readonly playerService: PlayerService) { }
My test:
describe('Player Controller', () => {
let controller: PlayerController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [PlayerService, CqrsModule],
controllers: [PlayerController],
providers: [
PlayerService,
],
}).pile();
controller = module.get<PlayerController>(PlayerController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
...
Nest can't resolve dependencies of the PlayerService (?, CommandBus, EventBus). Please make sure that the argument at index [0] is available in the PlayerService context.
at Injector.lookupComponentInExports (../node_modules/@nestjs/core/injector/injector.js:180:19)
Any way to work around this dependency issue?
Share Improve this question edited Apr 9, 2019 at 8:11 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Apr 8, 2019 at 5:59 invernomutoinvernomuto 10.2k5 gold badges42 silver badges53 bronze badges1 Answer
Reset to default 6It does not work because you are importing PlayerService
. You can only import modules, providers can be imported via a module or declared in the providers
array:
imports: [PlayerService, CqrsModule]
^^^^^^^^^^^^^
However, in a unit test you want to test single units in isolation, not the interaction between different units and their dependencies. So better than importing or declaring your dependencies, would be providing mocks for the PlayerService
or the providers of the CqrsModule
.
See this answer for a distinction between unit and e2e tests.
See this answer on how to create mocks.
本文标签: javascriptTest NestJs Service with JestStack Overflow
版权声明:本文标题:javascript - Test NestJs Service with Jest - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742269166a2443978.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论