var MyClassStub = sinon.createStubInstance(MyClass);
MyClassStub doesn't contain static methods. How to fix that?
As MDN describes it, “Static methods are called without instantiating their class and are also not callable when the class is instantiated. Static methods are often used to create utility functions for an application.” In other words, static methods have no access to data stored in specific objects.
The sinon. stub() substitutes the real function and returns a stub object that you can configure using methods like callsFake() . Stubs also have a callCount property that tells you how many times the stub was called. For example, the below code stubs out axios.
To stub a dependency (imported module) of a module under test you have to import it explicitly in your test and stub the desired method. For the stubbing to work, the stubbed method cannot be destructured, neither in the module under test nor in the test.
static method:
sinon.stub(YourClass, 'yourClassMethod').callsFake(() => { return {} })
not static method:
sinon.stub(YourClass.prototype, 'yourClassMethod').callsFake(() => { return {} })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With