From the course: Node.js: Testing and Code Quality
Unlock the full course today
Join today to access over 24,400 courses taught by industry experts.
Writing your first Jest mock
From the course: Node.js: Testing and Code Quality
Writing your first Jest mock
- We are going to write our first jest mock for our application, but it's going to take a little more work than just creating a new mock function. In lib reservations.js, there's a function named validate, which validates a reservation. We've already written two tests which test the validation library, so there's nothing additional that we need to test for now. However, create calls validate before it persists the record. while validate doesn't access any external resources, it's best practice to unit test in isolation. Remember, we're testing create, not validate and we want our unit tests to be as atomic as possible. Therefore, we'll mock validate when we write a create test. We haven't gotten to the stage of being able to mock calls to the database so we can't pass validation. Therefore, let's mock validate so it arbitrarily returns a promise rejection. In order to do that with a jest mock function, we will…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
Replacing code with test doubles6m 51s
-
(Locked)
Mocking functions for test code7m 1s
-
(Locked)
Writing your first Jest mock5m 46s
-
(Locked)
Mock an entire module with Jest7m 22s
-
(Locked)
Mocking module dependencies6m 19s
-
(Locked)
Spying to observe interactions7m 3s
-
(Locked)
Challenge: Mock a successful reservation creation53s
-
(Locked)
Solution: Mock a successful reservation creation6m 11s
-
(Locked)
-
-