Courses / Frontend Testing
18. jest.spyOn()
Using jest.spyOn to observe calls to an existing object method without replacing it.
JavaScript
jest.spyOn()
A utility that watches (spies on) an existing object method to record calls without replacing its behavior by default.
"I want to verify a function was called during a test but still use its real implementation."
jest.spyOn creates a spy wrapper around an existing object's method so you can inspect calls, arguments, return values, and restore the original method later. By default it preserves the original implementation unless you mock the return or implementation explicitly.
"I want to verify a function was called during a test but still use its real implementation."
jest.spyOn creates a spy wrapper around an existing object's method so you can inspect calls, arguments, return values, and restore the original method later. By default it preserves the original implementation unless you mock the return or implementation explicitly.