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

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…

Contents