Exercise 1 solution (Testing for Hash Password Functions)

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.

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.

This video is available to students only
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
  • [00:00 - 00:11] In the previous lesson, we introduced an exercise where we needed to write a unit test for hash password function. I hope you were able to create a working test case.

    [00:12 - 00:23] Now let's explore the solution step by step. First, we need to import the unit we intend to test, and I'll do that in user selfish.test.js.

    [00:24 - 00:41] In this case, we import the hash password function. Next, we create a test case nested within a describe block.

    [00:42 - 00:49] We need to import the hash password function. We need to import the hash password function.

    [00:50 - 00:57] We need to import the hash password function. We need to import the hash password function.

    [00:58 - 01:05] We need to import the hash password function. We need to import the hash password function.

    [01:06 - 01:27] To structure our test, we need to use the arrangeactasset pattern. This structure helps us organize our test and makes the intent behind each test clear.

    [01:28 - 01:39] Using the arrangeactasset pattern, we set up our test by creating a password string. This password will be used as the impute for the hash password function.

    [01:40 - 02:21] Then we act by executing the hash password function with the password as the argument. Finally, we set the password and the hash password equal.

    [02:22 - 02:32] We use pcripts.compare method to compare the password with the hash password. This ensures that the hashing process is functioning correctly.

    [02:33 - 02:40] We need to import the hash password function with the hash password. This is a test case.

    [02:41 - 02:52] We need to import the hash password function with the hash password. We need to import the hash password function with the hash password.

    [02:53 - 03:12] We need to import the hash password function with the hash password. This is important to note that in this test, we are only testing the code we wrote.

    [03:13 - 03:23] We don't need to test third-party dependencies such as BCrypt. The creators of BCrypt have already tested their library and it's their responsibility to ensure its correctness.

    [03:24 - 03:37] I will go ahead and run my test now. We can see that all our tests run without failing.

    [03:38 - 03:52] I would like to improve on the organization of the test cases in this file by creating a describe block for user service. Then nesting the different describe blocks inside this user service.

    [03:53 - 04:24] If I run our test again, you will see that having this nested describe blocks provide context to our test. It helps in the way just reporter shows our tests in the terminal.

    [04:25 - 04:43] We can see a nested structure here where at the top level we have the user service as defined here. We have a password correct as defined here in this describe block.

    [04:44 - 05:05] Also, hash password as defined here in this describe block and the individual test cases are on that each of these describe blocks. By following these steps and focusing on testing our own code, we can confidently test the hash password function.

    [05:06 - 05:18] Remember, if we don't trust the third party dependency, it's best to find an alternative or avoid using it in our application rather than trying to test this functionality. So in the next lesson.