Unit Testing for the Valid Credentials Scenario During Sign-In

Learn how to write a unit test for the signIn method when the user provides valid credentials.

Test: Valid credentials

  • Objective: To ensure the signIn function successfully authenticates a user with valid credentials and returns an access token.

Key points to test:

  • The function should return an access token when the user provides a registered email and the correct password.

In this scenario, with the correct email and password, the userRepository findOneBy method retrieves the relevant user. The bcrypt.compare function confirms a correct password match by returning true. Following this, jwtService.signAsync generates an access token. Finally, the signIn function returns an object containing this token, { accessToken: "token123" }.

With a clear understanding of the scenario, let’s follow these steps to write our test:

Step 1: Mocking findOneBy and bcrypt.compare methods

Get hands-on with 1200+ tech skills courses.