aws-sdk-mock icon indicating copy to clipboard operation
aws-sdk-mock copied to clipboard

Unable to mock CodePipeline

Open chriswilty opened this issue 6 years ago • 2 comments

Summary

I am trying to mock calls to CodePipeline.putJobSuccessResult in a Lambda. However, a real aws-sdk call is being made instead of invoking my stub. I am successfully able to mock out some other services such as DynamoDB and DocumentClient using the same technique, but not CodePipeline.

Versions Jest: 24.7.1 aws-sdk: 2.447.0 aws-sdk-mock: 4.5.0


Example

my.js:

import * as AWS from 'aws-sdk';

exports.handler = async event => {
  const { ['CodePipeline.job']: { id: jobId } = {} } = event;
  if (jobId) {
    await new AWS.CodePipeline({ apiVersion: '2015-07-09' })
      .putJobSuccessResult({ jobId })
      .promise();
  }
  return { message: 'Done!' };
};

my.spec.js:

import * as AWSMock from 'aws-sdk-mock';
import * as AWS from 'aws-sdk';

describe('My spec', () => {
  beforeAll(() => {
    AWSMock.setSDKInstance(AWS);
  });
  
  it('Invokes CodePipeline if job ID present', async () => {
    const successStub = jest.fn().mockResolvedValue(null);
    AWSMock.mock('CodePipeline', 'putJobSuccessResult', successStub);
    
    const jobId = 'yesh';
    const event = { ['CodePipeline.job']: { id: jobId } };
    await lambda.handler(event);
    expect(successStub.mock.calls[0][0]).toEqual({ jobId });

    AWSMock.restore('CodePipeline', 'putJobSuccessResult');
  });
});

Result:

ValidationException: 1 validation error detected: Value at 'jobId' failed to satisfy constraint: Member must satisfy regular expression pattern: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}

at Request.extractError (node_modules/aws-sdk/lib/protocol/json.js:51:27)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:106:20)
....

The stack trace shows that it is invoking the actual aws-sdk library, not the mock.

chriswilty avatar Nov 04 '19 14:11 chriswilty

I am experiencing the same problem. Did you make any progress @chriswilty ?

winjer avatar Feb 15 '20 22:02 winjer

@winjer None at all, sorry. Test still skipped :(

chriswilty avatar Feb 15 '20 22:02 chriswilty