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

Mocking Lambda.invoke fails

Open jcald1 opened this issue 4 years ago • 6 comments

The following code does not mock out the Lambda service. I'm using version 5.1.0.

const AWS = require('aws-sdk-mock');
AWS.mock('Lambda', 'invoke', { msg : 'success' });

In addition, the Readme should be updated to show a Lambda invoke example.

jcald1 avatar May 19 '20 19:05 jcald1

@jcald1 Pull Request to update the README.md very much welcome. 👍

nelsonic avatar May 20 '20 07:05 nelsonic

@nelsonic , sure, but I can do that after the underlying issue is identified/fixed. I see someone else is not able to mock Lambda invokes.

jcald1 avatar May 20 '20 17:05 jcald1

This seems to work for me on 5.1.0 so I don't think there's an issue. I could add an example to the readme if you want to but it's practically the same example as the DynamoDB one that's already there.

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

describe('lambda mock', () => {
  it('should be able to mock lambda invoke', async () => {
    AWSMock.setSDKInstance(AWS);
    AWSMock.mock('Lambda', 'invoke', (params: any, callback: any) => {
      callback(null, { pk: 'foo', sk: 'bar' });
    });

    const input = { FunctionName: 'myFunction' };
    const lambda = new AWS.Lambda();
    expect(await lambda.invoke(input).promise()).toStrictEqual({ pk: 'foo', sk: 'bar' });

    AWSMock.restore('Lambda');
  });
});

TastefulElk avatar Aug 19 '20 21:08 TastefulElk

@TastefulElk , so it seems that the mock function when mocking Dynamo and Lambda takes a callback, but when mocking S3 and SNS, it takes a value. I would list out in the Readme which mock services use values and which ones use callbacks.

jcald1 avatar Aug 19 '20 21:08 jcald1

@jcald1 No, either should be fine. This works as well for example:

    AWSMock.mock('Lambda', 'invoke', { pk: 'foo', sk: 'bar' });

    const input = { FunctionName: 'myFunction' };
    const lambda = new AWS.Lambda();

    expect(await lambda.invoke(input).promise()).toStrictEqual({ pk: 'foo', sk: 'bar' });

TastefulElk avatar Aug 19 '20 22:08 TastefulElk

@TastefulElk, I'm wondering if just doesn't work when you test a separate imported module that is the one importing AWS/invoking a Lambda. I don't have access to the original code I had used, but https://github.com/dwyl/aws-sdk-mock/issues/215 also had the same issue.

jcald1 avatar Aug 20 '20 00:08 jcald1