0

I have the following code:

it('Should return error', async () => {
  const searchMetadata = {
    details: {}
  };
  sandbox.mock(resultService).expects('doesSearchExists').atLeast(1).resolves(true);
  sandbox.mock(resultService).expects('doesSearchExists').atLeast(1).withArgs("limit").resolves(false);
  sandbox.mock(analyticsService).expects('getMetadata').throws(UNABLE_TO_CALCULATE_METADATA);
  await GET(request, res, (result: any) => {
    expect(result).to.be.deep.equal(UNABLE_TO_CALCULATE_METADATA)
  });
});

As you can see I am trying to mock the same function doesSearchExists twice differently based on the argument I sent. If limit is sent doesSearchExists should return false and if limit is missing doesSearchExists should return true. But when I try it I get

TypeError: Attempted to wrap doesSearchExists which is already wrapped

any idea how I can achieve the above?

1
  • You need to store the mock and reuse it for the second setup. Commented Jun 15, 2021 at 9:07

1 Answer 1

1

I made a runnable example that fixes your issue.

// Employs 'mini-mocha' to emulate running in the Mocha test runner (mochajs.org)
require("@fatso83/mini-mocha").install();

const sinon = require("sinon");
const {assert} = require('@sinonjs/referee');

// The SUT
const resultService = {
    doesSearchExists(arg) {}
}

describe("SO67960235", function() {
    const sandbox = sinon.createSandbox();

    it('Should return error <-- this title is wrong', async () => {

      // setup mocks
      const mock = sandbox.mock(resultService)
      mock.expects('doesSearchExists').atLeast(1).resolves(true);
      mock.expects('doesSearchExists').atLeast(1).withArgs("limit").resolves(false);
      
      // call your external methods that exercise your mocks
      console.log(await resultService.doesSearchExists("foo")); // true
      console.log(await resultService.doesSearchExists("limit")); // false

      // verify that they were called as expected
      mock.verify();
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.