Attempting here to test the validateUser method of the AuthService service.
However, within the validateUser method, it contains a call to a function within UsersService, named user, which queries the database for a specific user. For the unit test to work without a database connection, I'm overriding this method within the providers array when creating the testing module.
The issue is that the mocked UsersService is not being called - the real method in the real service is the one that is actually being called.
My best guess is that this is something with dependencies - maybe I'm importing incorrectly and that means that the real one is being called? I'm at a loss here. Code is below.
let authService: AuthService;
beforeEach(async () => {
const userData = {
id: 1,
username: "John",
email: "[email protected]",
password: await bcrypt.hash("password", 10),
verified: true
};
const module = await Test.createTestingModule({
providers: [
AuthService,
{
provide: UsersService,
useValue: {
user: jest.fn().mockResolvedValue(userData)
}
}
],
imports: [
AppModule,
MailModule,
HttpModule
]
}).compile();
authService = await module.get(AuthService);
});
describe("when validating a user", () => {
it("should verify a hashed password successfully", async () => {
const result = await authService.validateUser("", "password");
expect(result).toBe(true);
});
});