Git Product home page Git Product logo

Comments (11)

stalniy avatar stalniy commented on June 6, 2024 8

Jasmine automatically cleans up all spies in afterEach callback

What you need to do is to add

afterEach()=>{
  chai.spy.restore(class, ‘anotherFunction')
})

from chai-spies.

keithamus avatar keithamus commented on June 6, 2024

Spies do not currently have a reset. I think it'd be a good idea to add one, and fairly simple to add.

Personally, I'd take the existing metadata assignment (L74-L78) and put it into the reset method, then call reset in the factory, so L74-L80 look more like:

    proxy.reset = function () {
        this.__spy = {
            calls: []
          , called: false
          , name: name
        };
        return this;
    }
    return proxy.reset();

Of course, if you were to make a PR, I'd expect some good test coverage to be present for this feature 😄.

from chai-spies.

d48 avatar d48 commented on June 6, 2024

fantastic! Let me see if I can get to this today

from chai-spies.

d48 avatar d48 commented on June 6, 2024

PR #28 created, thanks

from chai-spies.

d48 avatar d48 commented on June 6, 2024

Thanks @keithamus

from chai-spies.

stalniy avatar stalniy commented on June 6, 2024

@d48 could you please describe a use case where you had a need in .reset() method?

from chai-spies.

tamaserdfalvi avatar tamaserdfalvi commented on June 6, 2024

Like if you have a mocked object and you're using the same instance of it for multiple test cases.

from chai-spies.

stalniy avatar stalniy commented on June 6, 2024

The property of good tests is that it should start from clean state otherwise you can’t rely on results.

from chai-spies.

tamaserdfalvi avatar tamaserdfalvi commented on June 6, 2024

Definitely. I'm just describing a possible scenario where it might be useful. For example you have 3 tests which you embed in a describe block and create a mocked object only once in beforeEach.
Then it might be useful

from chai-spies.

mebibou avatar mebibou commented on June 6, 2024

I am coming from jasmine and really confused here, how would you test like this:

describe('myFunction', () => {
  describe('when another function returns true', () => {
    beforeEach(() => {
      chai.spy.on(class, 'anotherFunction', () => true);
    });

    it('should pass', () => {
      expect(myFunction()).to.equal(1);
    });
  });

  describe('when another function returns false', () => {
    beforeEach(() => {
      chai.spy.on(class, 'anotherFunction', () => false);
    });

    it('should not pass', () => {
      expect(myFunction()).to.equal(0);
    });
  });
});

but it tells me that the function is already a spy, even though the spies are not under the same describe (which totally works in jasmine thankfully)

from chai-spies.

d48 avatar d48 commented on June 6, 2024

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

from chai-spies.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.