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

Mocking Lambda.invoke fails #217

Open jcald1 opened 4 years ago

jcald1 commented 4 years ago

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.

nelsonic commented 4 years ago

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

jcald1 commented 4 years ago

@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.

TastefulElk commented 4 years ago

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');
  });
});
jcald1 commented 4 years ago

@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.

TastefulElk commented 4 years ago

@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' });
jcald1 commented 4 years ago

@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.