Issue:
I am facing a challenge with the
bcrypt.compare(plainText, hashedPassword)
function not returning true
even when the plain text password matches the one used to create the hashed password during testing.
Situation:
- In my project, I am using Mongodb, Express, NodeJS, Jest, and Supertest.
- Although I have not tested it in Postman yet, I am focusing on improving my skills with Jest first.
- The user created in the beforeEach function in user.test.ts exists in the database, and the hashed password stored in the database corresponds to the one logged in the controller.ts file.
- I did manage to pass the test once, but upon removing some console.logs that were used for debugging, the error resurfaced.
Error Encountered:
FAIL src/components/users/users.test.ts (6.73 s)
User registration
✓ Should create a new user (1985 ms)
✓ Should return 400 if user exists (152 ms)
User login
✕ Should login an existing user with valid credentials and return accessToken (215 ms)
● User login › Should login an existing user with valid credentials and return accessToken
expected 200 "OK", got 400 "Bad Request"
63 | .post('/user/login')
64 | .send(existingUserLogin)
> 65 | .expect(200);
| ^
...
Code Snippets:
users.test.ts
...controller.ts:
...model.ts
...Actions Taken So Far:
- Tried switching the arguments of bcrypt.compare from
tobcrypt.compare(plainText, hashedPassword)
bcrypt.compare(hashedPassword, plainText)
- Rebuilt with tsc
- Used a different password than the one mentioned above
- Ran
npm install
again - Restarted VScode
- Attempted
bcrypt.compare(password.trim(), hashedPassword.trim())
It seems like I may be overlooking a simple detail. Appreciate any assistance provided!