in this video I'm showing how I'm testing the reset password request feature in my authentication system based on Next JS and Express.js. For testing I'm using Sinon and Mocha.
Category
🦄
CreativityTranscript
00:00In this video, I will show how I'm testing the reset password request using Sinon and Mocha.
00:13But first, I will show a manual test.
00:20This is the code for the reset password request.
00:35Here, I'm extracting the email from the request body and using Mongoose to verify if a user with that email exists.
00:59Then, I'm using the JSON web token library to generate a token valid for 10 minutes, which will be used as an identifier for the user in the reset password link.
01:26The reset password link will be added in the template, which will be sent by email with NodeMailer.
01:50In the test file, I'm creating a stub for this method to substitute it with a fake function and avoid a real search.
02:19I just will let it return with the wanted response.
02:38Next, I'm creating another stub to substitute the real code generation with a fake function that will return fake code.
03:08When I run a test, when I will run the reset method, all these requests
03:37will be replaced with a fake function to avoid real requests to the database, code generation, and email sending.
03:57Then, I'm creating another stub to replace or substitute the read transport object with SMTP configuration.
04:18Since this object has the sendMail method, I'm creating a stub even for this method and providing it as a response to the read transport tab.
04:47Finally, when I'm calling this method to request a reset password link in my test file, the created stubs will provide the expected response to all these methods.
05:10In this way, I will just test how is handled the response.
05:25This test case is used only for success response.
05:34If I want to test a failed response, for example, the user won't be found in the database.
06:04For this stub, I will add as a response null.
06:34And let's test.
06:53For now, I will let it true here. I need to get an error.
07:16Now, I will change with a false.
07:40And it works as expected.