JavaScript Answers

How to stub a class method with Sinon.js and JavaScript?

Spread the love

To stub a class method with Sinon.js and JavaScript, we call the stub method.

For instance, we write

sinon.stub(YourClass.prototype, "myMethod").callsFake(() => {
  return {};

to call stub with YourClass.prototype to stub the myMethod instance method on YourClass.

We call callFake with the mock function for the myMethod method.

Likewise, for static class methods, we write

sinon.stub(YourClass, "myStaticMethod").callsFake(() => {
  return {};

to mock the YourClass.myStaticMethod static method with stub.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *