Unit Testing for the Invalid Password Scenario During Sign-In

Learn how to write a unit test for the signIn method when the user provides an incorrect password.

Test: Invalid password

  • Objective: To ensure the signIn method properly handles cases where a user attempts to sign in with an existing email in our system but an incorrect password.

Key points to test:

  • The function should throw an UnauthorizedException when the password for a registered email is incorrect.

In this scenario, the userRepository utilizes findOneBy to retrieve the user by email during the sign-in process. When bcrypt.compare returns false, signifying the password is incorrect, the signIn function throws an UnauthorizedException.

Let’s follow these steps to write our test:

Step 1: Mocking the findOneBy behavior

Get hands-on with 1200+ tech skills courses.