dwyl / aws-sdk-mock

:rainbow: AWSomocks for Javascript/Node.js aws-sdk tested, documented & maintained. Contributions welcome!
Apache License 2.0
1.11k stars 109 forks source link

Unable to mock CodePipeline #199

Open chriswilty opened 4 years ago

chriswilty commented 4 years ago

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.

winjer commented 4 years ago

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

chriswilty commented 4 years ago

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