Encountering an issue while trying to unit test. Here is the error message that I received:
TypeError: Cannot read property 'prototype' of undefined
export class UserService {
constructor(@InjectRepository(User) private readonly userRepository: Repository < User>) { }
spec.ts:
describe('AuthController', () => {
let authController: AuthController;
let authService: AuthService;
let mockRepository = {
};
beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [
TypeOrmModule.forFeature([User]),
],
controllers: [AuthController],
providers: [AuthService, {
provide: getRepositoryToken(User),
useValue: mockRepository
}]
}).compile()
authService = module.get<AuthService>(AuthService);
authController = module.get<AuthController>(AuthController)
});
Seeking assistance with finding a solution for this problem.
MORE INFO:
It appears that there might be an issue related to typeorm
beforeEach(async () => {
const module = await Test.createTestingModule({
}).compile()
authService = module.get<AuthService>(AuthService);
authController = module.get<AuthController>(AuthController)
});
Using the provided code snippet results in the same error. It seems like the only problem arises when typeorm
is added to the test Module.
The failure seems to stem from the dependency chain: AuthController->AuthService->UserService->TypeORM
For reference, the UserService
works successfully when tested using Postman.
No resolution yet:
module = await Test.createTestingModule({
controllers: [AuthController],
components: [
{
provide: AuthService,
useValue: {}
},
{
provide: UserService,
useValue: {}
},
{
provide: getRepositoryToken(User),
useValue: {}
}
],
providers: [
{
provide: AuthService,
useValue: {}
},
{
provide: UserService,
useValue: {}
},
{
provide: getRepositoryToken(User),
useValue: {}
}
]
}).compile()
this.authController = module.get<AuthController>(AuthController)
Additionally
class AuthServiceMock {
logIn(userName) {
return { id:100, isDeleted:false, login:"login", password:"password"};
}
signUp() {
return { expireIn: 3600, token:"token" };
}
}
describe('AuthController', () => {
let module: TestingModule;
let authController: AuthController;
let authService: AuthService;
beforeEach(async () => {
module = await Test.createTestingModule({
controllers: [AuthController],
components: [
],
providers: [
{
provide: AuthService,
useClass: AuthServiceMock
},
]
}).compile()
this.authController = module.get<AuthController>(AuthController)
});