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.