Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring back spy.reset() #104

Open
AdamMcCormick opened this issue Aug 16, 2018 · 6 comments
Open

Bring back spy.reset() #104

AdamMcCormick opened this issue Aug 16, 2018 · 6 comments

Comments

@AdamMcCormick
Copy link

A previous issue #27 introduced the reset method to this library which we use extensively. It was removed in 1.0 for no clear reason. I'd like to see it put back

@dabutvin
Copy link

dabutvin commented Sep 4, 2018

@AdamMcCormick can you use restore instead?

chai.spy.restore()

@AdamMcCormick
Copy link
Author

@dabutvin No. Please see the thread at #27. Restore is not the same thing and does not satisfy my use case. It also makes my tests significantly slower given my test setup.

@zinoadidi
Copy link

Any update on this issue?

@keithamus
Copy link
Member

Hey @AdamMcCormick thanks for the issue.

It was removed as it was seen to be an anti pattern because it swaps out internal state which can cause issues in tests - certainly compounding issues if your tests rely on external state. The preferred alternative is to restore and make a new spy.

@AdamMcCormick could you please give us some numbers as to how much slower your tests are by using restore? I'd be surprised to see even 100ms delta between restore/reset.

@jorgeortega
Copy link

jorgeortega commented Mar 5, 2020

You can just do

const spy = () => chai.spy()
let funcToBeSpied

beforeEach(() => {
  funcToBeSpied = spy()
})

@Daghall
Copy link

Daghall commented Dec 28, 2020

With proxyquire you have to clear the require cache and re-proxyquire the module, in order to do a full reset.
That is quite messy. This was my solution for resetting the spy, in lieu of spy.reset():

let barSpy;
const foo = proxyquire("../lib/foo.js", {
  "./bar.js": (...args) => barSpy(...args),
});

describe("foo bar", () => {
  beforeEach(() => {
    barSpy = chai.spy();
  });

  it("bar is called once by foo", () => {
    foo();
    expect(barSpy).to.have.been.called.once;
  });

  it("bar is called once by foo, again", () => {
    foo();
    expect(barSpy).to.have.been.called.once;
  });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants