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

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.…

Contents