Skip to content

How to Unit Test Lambda Functions in Local/CI Environment? #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
nelsonic opened this issue Jan 26, 2016 · 16 comments
Open

How to Unit Test Lambda Functions in Local/CI Environment? #16

nelsonic opened this issue Jan 26, 2016 · 16 comments

Comments

@nelsonic
Copy link
Member

How are we going to Unit Test the Lambda handler to ensure it works as expected?
https://aws.amazon.com/blogs/compute/serverless-testing-with-aws-lambda/ ?
https://medium.com/@AdamRNeary/developing-and-testing-amazon-lambda-functions-e590fac85df4

@nikhilaravi
Copy link
Member

I would be keen to research this!

@nelsonic
Copy link
Member Author

Please go for it! 👍

@nelsonic
Copy link
Member Author

@nikhilaravi
Copy link
Member

@nelsonic I read through it yesterday but haven't used it yet.

@nikhilaravi
Copy link
Member

@nelsonic what do you think about extending the mock-context-object npm module to generate more accurate responses from the context methods.

It doesn't yet account for different invocation types i.e. Event or Request/Response. From the AWS docs about the context.sucess function:

If the Lambda function is invoked using the Event invocation type (asynchronous invocation), the method will return "HTTP status 202, request accepted" response.
If the Lambda function is invoked using the RequestResponse invocation type (synchronous invocation), the method will return HTTP status 200 (OK) and set the response > body to the string representation of the result.

@nelsonic
Copy link
Member Author

@nikhilaravi submit this as a question on the project repo and see if you get a response before investing the time in a PR... but if you get a quick response, feel free to extend. We need to check tomorrow if they already have a plan for mocking their Lambda functions...
in light of the chat with P today we might end up using simple-mock in which case we will create a [simple-mock-lambda] which uses the module...

@nikhilaravi
Copy link
Member

@nelsonic okay i'll do that now and I was just thinking about how to mock the function invocation similar to the invoke api but a simple-mock-lambda module sounds great.

@nikhilaravi
Copy link
Member

@nelsonic found another example of testing on this repo but the context methods only seem to be for synchronous invocation.

@nelsonic
Copy link
Member Author

Yeah, I haven't yet seen anything that looks elegant for testing Lambda functions ... we might be forced to come up with something better. 😜

@nikhilaravi
Copy link
Member

screen shot 2016-01-29 at 14 11 16

@nikhilaravi
Copy link
Member

All the repos using the mock-context module
https://github.com/search?p=3&q=aws-lambda-mock-context&type=Code&utf8=%E2%9C%93

@nelsonic If we're using promises should we also use an async test runner like ava?

If you return a promise in the test you don't need to explicitly end the test as it will end when the promise resolves.

@nelsonic
Copy link
Member Author

Can we simplify it...?

@nikhilaravi
Copy link
Member

@nelsonic I had a look at the book you sent us and got a few ideas so will have a go at implementing them on Monday :)

@nelsonic
Copy link
Member Author

Sweet! 🍭
Meanwhile hope you have a RESTful remainder of your weekend! 👍

@nikhilaravi
Copy link
Member

haha! @nelsonic I found another module that mocks the context https://github.com/motdotla/node-lambda/blob/master/lib/main.js#L51-L67

However the values passed into the 'context.succeed', 'context.fail' methods are not passed back to the invoker. I think the only way to do this at the moment seems to be using the mock-context module (which uses promises) or using callbacks. I have included examples of both in the PR #45

@sarmistha21
Copy link

sarmistha21 commented Sep 21, 2017

Hi @nikhilaravi / @nelsonic ,
How do I test a lamba when implemented with callbacks and not context? (Node 6.10.0)

Thank you
Sarmistha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants