Search code examples
javascriptunit-testingsinon

Sinon.js assert stub throws exception when passed parameter


I'm new to Sinon.js I want a function to throw an exception when passed a value and assert that it's thrown/caught. I tried multiple things but I haven't been able to make it work. I'm currently there:

The test:

  t.test('Assert that thrown errors are caught if 401: unauthorized error is returned', async (st) => {

    let retrieveConfig = sinon.stub();
    retrieveConfig.withArgs(true).returns(true);
    retrieveConfig.withArgs(false).throws('testName'['optional message']);

    let partnerList = [true, false];

    const dataSources = {
      storageService: {
        retrieveConfig
      }
    };

    assert.throwsException(await isCompliant(dataSources, partnerProfileIdList));
    st.end();
  });

I want retrieve config to throw an exception when passed false, and I want to make sure it's caught

My function

async function isCompliant(dataSources, partnerList) {
  for (let i = 0; i < partnerList.length; i++) {
    try {
      let response = await dataSources.storageService.retrieveConfig(partnerList[i]);
      if (!response ){
        return false;
      }
    } catch (e) {
      throw 401;
    }
  }
  return true;
}

I haven't been able to find an example that does what I am trying to do, and I don't understand what is missing in my code to make this work


Solution

  • You should use stub.resolves(value) and stub.rejects(value) for the retrieveConfig stub.

    Causes the stub to return a Promise which resolves/rejects to the provided value.

    Note: I don't know what test framework and assertion library you are using. So I will use chai as the assertion library and mocha as the test runner.

    E.g.

    import { isCompliant } from ".";
    import sinon from 'sinon';
    import chai, { expect } from 'chai';
    import chaiAsPromised from 'chai-as-promised';
    
    chai.use(chaiAsPromised)
    
    describe('74548905', () => {
      it('should pass', async () => {
        let retrieveConfig = sinon.stub();
        retrieveConfig.withArgs(true).resolves(true);
        retrieveConfig.withArgs(false).rejects(new Error('test error'));
    
        let partnerList = [true, false];
    
        const dataSources = {
          storageService: {
            retrieveConfig
          }
        };
    
        await expect(isCompliant(dataSources, partnerList)).to.eventually.rejected;
      });
    });
    

    Test result:

      74548905
        ✓ should pass
    
    
      1 passing (22ms)
    
    ----------|---------|----------|---------|---------|-------------------
    File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
    ----------|---------|----------|---------|---------|-------------------
    All files |   77.78 |       50 |     100 |      75 |                   
     index.ts |   77.78 |       50 |     100 |      75 | 6,12              
    ----------|---------|----------|---------|---------|-------------------
    

    package versions:

    "sinon": "^8.1.1",
    "chai": "^4.3.7",
    "chai-as-promised": "^7.1.1"