This is in NestJs, using Jest. I am trying to test my controller, which relies on my “main” service. I wish to perform e2e testing on the controller. (To clarify, my smaller unit tests of the dependency services are all working fine – no issues there.)
My controller’s constructor and first method I am testing look like this:
export class MyController{
constructor(
private aService: AService,
private bService: BService,
//etc etc multiple such services
) {}
@Post('/login')
async login(@Body() logDto: LogDTO)
{
console.log('aService: ' + this.aService);
console.log('bService: ' + this.bService);
//etc etc
//then the code
}
}
I’ve set up the test as normal, tediously importing everything, mocking repos, etc, and when I missed things, I got an error saying I missed importing some service or so on.
My very first test in the spec.ts is:
//controller, aService, bService, etc were all defined in the beforeEach block
it('should be defined', () => {
expect(controller).toBeDefined();
expect(aService).toBeDefined();
expect(bService).toBeDefined();
//etc
}
This test is passing. Everything is defined fine in my spec.ts. However, the moment I try to run a test involving the actual controller.ts, I get errors like “Cannot read properties of undefined” the moment I try to execute a method from a dependency service. And, those console.logs confirm that the dependency services are all undefined in the actual controller itself.
I tried to ensure that I didn’t miss any imports, but without any actually useful error messages, I’m completely stuck. I’ve never done E2E testing with Jest before, so this is completely new ground for me, too.