1

I have multiple files that use sinon to stub the same method Utils.getTimestamp.

When running the tests files, one at a time all tests pass. When running the tests files at once, the tests fail with: TypeError: "Attempted to wrap getTimestamp which is already wrapped"

In both files, I have the describe blocks with a before and after blocks

In the Before block, I stub the method like: getTimestampStub= sinon.stub(Utils, 'getTimestamp') .returns(myTimestamp);

In the After block I restore the method like: getTimestampStub.restore();

I tried this according to this answer: https://stackoverflow.com/a/36075457/6584537

Sample files:

FILE 1

describe("First Stub", () => {
    let getTimestampStub;
    before(() => {
        getTimestampStub= sinon.stub(Utils, 'getTimestamp') .returns("SOME_TIMESTAMP");
    });

    it("Should run some code that uses getTimestamp", () => {
        // Some code that in the process uses `Utils.getTimestamp`
    });
    after(() => {
        getTimestampStub.restore();
    });
});

FILE 2

describe("Second Stub", () => {
    let getTimestampStub;
    before(() => {
        getTimestampStub= sinon.stub(Utils, 'getTimestamp') .returns("SOME_TIMESTAMP");
    });

    it("Should run some OTHER code that uses getTimestamp", () => {
        // Some code that in the process uses `Utils.getTimestamp`
    });

    after(() => {
        getTimestampStub.restore();
    });
});
  • Does this answer your question? [Sinon error Attempted to wrap function which is already wrapped](https://stackoverflow.com/questions/36074631/sinon-error-attempted-to-wrap-function-which-is-already-wrapped) – vsync Apr 24 '23 at 07:21

2 Answers2

2

When Mocha runs multiple files, it runs all the before blocks first. This is true for 1 file or for multiple ones.

The error then is because I was trying to stub the same method before it had a chance to be restored. Something like this:

before()

it()

before() // not restored yet, second sinon.stub is called? "Attempted to wrap ... which is already wrapped"

it()

after() // unwrap

after() // already restored, another error: "Restore is not a function"

The solution then was to create the stub within the Assertion block I needed it. something like this:

File 1

describe("First Stub", () => {
    let getTimestampStub;
    before(() => {});

    it("Should Stub getTimestamp before some code needs it", () => {
        getTimestampStub= sinon.stub(Utils, 'getTimestamp') .returns("SOME_TIMESTAMP");

        // Some code that in the process uses `Utils.getTimestamp`

        getTimestampStub.restore();
    });
    after(() => {});
});

File 2

describe("Second Stub", () => {
    let getTimestampStub;
    before(() => {});

    it("Should Stub getTimestamp before some code needs it", () => {
        getTimestampStub= sinon.stub(Utils, 'getTimestamp') .returns("SOME_TIMESTAMP");

        // Some code that in the process uses `Utils.getTimestamp`

        getTimestampStub.restore();
    });
    after(() => {});
});
0

You can use beforeEach() and afterEach() to replace before() and after() instead of moving the stub and restore calls into each individual it() block.

What is the difference between `before()` and `beforeEach()`?

teebszet
  • 451
  • 1
  • 5
  • 12