This video is available to students only

Exercise 2

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

Previous LessonUnit test express controllerNext LessonExercise 2 solution

Lesson Transcript

  • [00:00 - 00:18] We have an exercise that will help you understand and implement a Midewell function in Express. By the end of this exercise you'll be able to create unit tests for a Midewell function that checks for a B-R-Tokin in the Request header and handles different scenarios based on its validity.

  • [00:19 - 00:37] A Midewell function in Express is a function that sits between the initial request and the final route under. It has access to the request and response object and can perform additional operations or modifications before passing control to the next Midewell or the final handler.

  • [00:38 - 00:56] In this exercise you will create a unit test for a Midewell function that checks if the Request header contains a B-R-Tokin. A Midewell function receives the request, response and next function as parameters. It first checks if the Request header contains a B-R-Tokin.

  • [00:57 - 01:16] If the token is missing, a Midewell function will return a 400 status code indicating a bad request. As for the case where a B-R-Tokin is presentable invalid, we'll also send the response with 400 status message and this JSON.

  • [01:17 - 01:28] The entry points are the request, the response and the next function. The request and response are objects while the next is a function.

  • [01:29 - 01:48] To test our Midewell function, you need to create test cases that cover different scenarios. In this exercise you will need to focus on mocking the JWT the verify and also asserting that the next method was called.

  • [01:49 - 02:06] In summary, this exercise we allow you to understand and implement test doubles and test the Midewell function in Express. By creating a Midewell function that checks for a B-R-Tokin we can enhance the security and authentication of our API endpoints.

  • [02:07 - 02:17] Through proper testing, including mocking the JWT the verify and asserting the next function we can ensure the correct behavior of a Midewell function. See you in the next lesson.

This lesson preview is part of the Pain Free Mocking with Jest course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

Unlock This Course

Get unlimited access to Pain Free Mocking with Jest, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Pain Free Mocking with Jest