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.
Spying to observe interactions
From the course: Node.js: Testing and Code Quality
Spying to observe interactions
- [Instructor] So far we've been mocking functions and modules which replaces. What if we want to keep the original implementation around, but still keep track of how it's being used? With Jest, we can use spies to observe interactions. The spyOn method creates a Jest mock function similar to jest.fn and jest.mock, but it also tracks calls to method name on the object. Something to keep on mind, spyOn calls the spied method. If that's not what you need, use a mock implementation instead and totally control the behavior. When you're done with spying on the method, it's best practice to restore the method back to normal. Otherwise, other requires may have inconsistent behavior. The spyOn mocks have a method mockRestore, which does just that. Within reservations, what are some things that can be spied on? We tested the validate function, but we can go one step further and test to see if it was called and how it was called.…
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)
-
-