Search code examples
node.jschaisinonaws-sdk-jssinon-chai

how to stub and expect a call to this aws-sdk service using sinon and chai


I have this exported function and I'd like to test that sts.assumeRole gets called:

const AWS = require('aws-sdk')
const sts = new AWS.STS();

module.exports.assumeRole = async (roleArn) => {
  let params = {
    RoleArn: roleArn,
    RoleSessionName: 'MessagingSession',
  }

  return await sts.assumeRole(params).promise()
}

This is what I have tried:

const chai = require('chai')
const expect = chai.expect
const sinon = require('sinon')
chai.use(require('sinon-chai'))
const AWS = require('aws-sdk')

describe('assumeRole', () => {
  it('throws an exception if roleArn is not present', async () => {
    const authenticationService = require('../services/authenticationService.js')
    await expect(authenticationService.assumeRole('')).to.eventually.be.rejectedWith('no ARN present')
  })

  it('calls sts assumeRole', () => {
    const stsStub = sinon.stub({ assumeRole: () => {} });
    const awsStub = sinon.stub(AWS, 'STS');
    awsStub.returns(stsStub);

    const authenticationService = require('../services/authenticationService.js')

    authenticationService.assumeRole('a-role-arn')

    expect(stsStub).to.have.been.calledOnce
  })
})

This results in TypeError: { assumeRole: [Function: assumeRole] } is not a spy or a call to a spy!

I'm new to spies and stubs and javascript testing, any help is appreciated in getting this test working!

Note: I want to be using the expect assertion syntax and don't want to use any other external packages

EDIT: now using slideshowp2 's solution I have the following:

const chai = require('chai')
chai.use(require('chai-as-promised'))
const expect = chai.expect
const sinon = require('sinon')
chai.use(require('sinon-chai'))
const AWS = require('aws-sdk')

describe('assumeRole', () => {
  it('throws an exception if roleArn is not present', async () => {
    const authenticationService = require('../services/authenticationService.js')
    await expect(authenticationService.assumeRole('')).to.eventually.be.rejectedWith('no ARN present')
  })

  it('calls sts.assumeRole', async () => {
    const stsFake = {
      assumeRole: sinon.stub().returnsThis(),
      promise: sinon.stub(),
    };
    const STSStub = sinon.stub(AWS, 'STS').callsFake(() => stsFake);
    const authenticationService = require('../services/authenticationService.js')

    await authenticationService.assumeRole('a-role-arn')

    sinon.assert.calledOnce(STSStub);
    sinon.assert.calledWith(stsFake.assumeRole, { RoleArn: 'a-role-arn', RoleSessionName: 'MessagingSession' });
    
    STSStub.restore();
  })
})

These two tests now work and pass independently but run all together the second one fails. I get the error ValidationError: 1 validation error detected: Value 'a-role-arn' at 'roleArn' failed to satisfy constraint: Member must have length greater than or equal to 20 It looks like the STS library is not being stubbed anymore


Solution

  • Here is the unit test solution:

    index.js:

    const AWS = require('aws-sdk');
    const sts = new AWS.STS();
    
    module.exports.assumeRole = async (roleArn) => {
      let params = {
        RoleArn: roleArn,
        RoleSessionName: 'MessagingSession',
      };
    
      return await sts.assumeRole(params).promise();
    };
    

    index.test.js:

    const sinon = require('sinon');
    const AWS = require('aws-sdk');
    
    describe('62918753', () => {
      it('should pass', async () => {
        const stsFake = {
          assumeRole: sinon.stub().returnsThis(),
          promise: sinon.stub(),
        };
        const STSStub = sinon.stub(AWS, 'STS').callsFake(() => stsFake);
        const authenticationService = require('./');
        await authenticationService.assumeRole('a-role-arn');
        sinon.assert.calledOnce(STSStub);
        sinon.assert.calledWith(stsFake.assumeRole, { RoleArn: 'a-role-arn', RoleSessionName: 'MessagingSession' });
        STSStub.restore();
      });
    });
    

    unit test result with coverage report:

      62918753
        ✓ should pass (919ms)
    
    
      1 passing (926ms)
    
    ----------|---------|----------|---------|---------|-------------------
    File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
    ----------|---------|----------|---------|---------|-------------------
    All files |     100 |      100 |     100 |     100 |                   
     index.js |     100 |      100 |     100 |     100 |                   
    ----------|---------|----------|---------|---------|-------------------