Search code examples
node.jsunit-testingsinon

mock the same function twice in the same test differently based on the argument I sent


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?


Solution

  • 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();
        });
    });