question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

method to reset spy object

See original GitHub issue

Hello,

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:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

8reactions
stalniycommented, Apr 25, 2018

Jasmine automatically cleans up all spies in afterEach callback

What you need to do is to add

afterEach()=>{
  chai.spy.restore(class, ‘anotherFunction')
})
0reactions
d48commented, Apr 25, 2018

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found