Categories
JavaScript Answers

How to fix Sinon error Attempted to wrap function which is already wrapped with JavaScript?

Spread the love

To fix Sinon error Attempted to wrap function which is already wrapped with JavaScript, we should restore our stubs after the test is done.

For instance, we write

beforeEach(() => {
  sandbox = sinon.createSandbox();
  mockObj = sandbox.stub(testApp, "getObj", fakeFunction);

});

afterEach(() => {
  sandbox.restore();
});

to call createSandvox to create the sandbox.

Then we call sandvbox.stub to create a stub in the beforeEach callback, which runs before each test.

Then in the afterEach callback, we call sandbox.restore to restore the stubs after each test.

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 *