dwyl / learn-aws-lambda

✨ Learn how to use AWS Lambda to easily create infinitely scalable web services
1.05k stars 203 forks source link

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

Open nelsonic opened 8 years ago

nelsonic commented 8 years ago

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 commented 8 years ago

I would be keen to research this!

nelsonic commented 8 years ago

Please go for it! :+1:

nelsonic commented 8 years ago

@nikhilaravi are you using the gulpfile.js from https://medium.com/@AdamRNeary/a-gulp-workflow-for-amazon-lambda-61c2afd723b6 ?

nikhilaravi commented 8 years ago

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

nikhilaravi commented 8 years ago

@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 commented 8 years ago

@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 commented 8 years ago

@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 commented 8 years ago

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

nelsonic commented 8 years ago

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

nikhilaravi commented 8 years ago
screen shot 2016-01-29 at 14 11 16
nikhilaravi commented 8 years ago

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 commented 8 years ago

Can we simplify it...?

nikhilaravi commented 8 years ago

@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 commented 8 years ago

Sweet! :lollipop: Meanwhile hope you have a RESTful remainder of your weekend! :+1:

nikhilaravi commented 8 years ago

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 commented 7 years ago

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

Thank you Sarmistha