method to reset spy object
See original GitHub issueHello,
is there a method to reset a spy object for reuse?
var boom = chai.spy(originalFn);
originalFn();
expect(boom).to.have.been.called.once; // passes
boom.reset();
expect(boom).to.have.been.called.once; //fails
Was helpful in another test framework and thought it would be great here. I can open up a pull request too if it’s a matter of resetting some internal vars for the callCount
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:11 (8 by maintainers)
Top Results From Across the Web
How to reset a spy in Jasmine?
This resets the already made calls to the spy. This way you can reuse the spy between tests. The other way would be...
Read more >How to automatically reset mocks and restore spies in Jest
“restoreMocks”: true : restores methods that were spied using jest.spyOn( .. ) to its original method. This flag is independent of the ...
Read more >spy.reset() not working for toHaveBeenCalledTimes #1396
This seems to be working fine for me. You do need to call .calls.reset() not just .reset() on the spy to get it...
Read more >Jest set, clear and reset mock/spy/stub implementation
This post goes through how to set, reset and clear mocks, stubs and spies in Jest using techniques such as the beforeEach hook...
Read more >Jasmine 2 Spy Cheat Sheet
Call through to the original · Return the specified value · Call the given function instead of the real one · Throw an...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Jasmine automatically cleans up all spies in afterEach callback
What you need to do is to add
hi @stalniy, sure, I can answer a possible use case. It’s a convenience method on your spy when making multiple calls to it that you can inspect, and maybe you want to reset after interacting with the spy.
And same as you just mentioned above, it can be used in an
afterEach
block to reuse if the spy is cached to a var