How to write Integration Tests for Serverless AWS Endpoints - API Gateway and Lambda Guide with Jest

  Рет қаралды 16,756

Complete Coding - Master AWS Serverless

Complete Coding - Master AWS Serverless

4 жыл бұрын

Event generator code:
github.com/SamWSoftware/Serve...
Completed Code
github.com/SamWSoftware/Serve...

Пікірлер: 41
@yaroslavdraha9251
@yaroslavdraha9251 3 жыл бұрын
Thanks for the content! Just want to support the channel that is why I left the comment :) Good luck with further videos! (probably i just missed that) It would be nice to mentioned at about approach how to run tests locally and on CI using DynamoDB copy, it would help for less experienced in serverless guys
@CompleteCoding
@CompleteCoding 3 жыл бұрын
There is a video I've done on that just here kzfaq.info/get/bejne/q9KPa5iby7OUn6M.html
@konstantin6524
@konstantin6524 3 жыл бұрын
Perfect content! I want to leave a comment for supporting chanel!
@CompleteCoding
@CompleteCoding 3 жыл бұрын
Much appreciated!
@jc8212
@jc8212 3 жыл бұрын
Awesome explaination 👍
@CompleteCoding
@CompleteCoding 3 жыл бұрын
Glad it was helpful!
@ahmadnabil2441
@ahmadnabil2441 2 жыл бұрын
Great explanation! Do u mind sharing vscode extensions names especially the one which checks the code with (x) and (✓) marks and the one that highlights code blocks with different colors?
@CompleteCoding
@CompleteCoding 2 жыл бұрын
The ticks and crosses are the "vs code jest" extension. The multi coloured tabs is "Rainbow intent"
@maopuerta3430
@maopuerta3430 3 жыл бұрын
Excellent video
@CompleteCoding
@CompleteCoding 3 жыл бұрын
Thanks!
@cristianecheverria3995
@cristianecheverria3995 4 жыл бұрын
Great content!!
@CompleteCoding
@CompleteCoding 4 жыл бұрын
Glad you liked it Christian
@EstebanHelgueroCardiff
@EstebanHelgueroCardiff 3 жыл бұрын
Hi, when you save data into DynamoDB, then you have to delete it? Or it's just a mock of Dynamo that you are using on this example?
@CompleteCoding
@CompleteCoding 3 жыл бұрын
When you run tests it creates a local mock of dynamo so it doesn't polute your real data
@MarzanDaniel
@MarzanDaniel Жыл бұрын
Thank you for sharing
@vigneshdev5762
@vigneshdev5762 2 жыл бұрын
Awesome stuff, Just want to know how can mock context parameter of handler function from unit test, can we pass something like contextGenerator()?
@CompleteCoding
@CompleteCoding 2 жыл бұрын
Yes, if you need to mock context the that would be the easiest way. Over time you can add parameters to vary the context data you generate
@madhavareddy3488
@madhavareddy3488 Жыл бұрын
Hello Sam, does these integration tests leverage emulated API gateway? I want to test the endpoint using it's route (e.g.: GET - /api/players) to test API key authentication. Is this possible?
@CompleteCoding
@CompleteCoding Жыл бұрын
You could do it another way. When you start jest, it starts serverless offline. Your jest tests then call the serverless offline endpoints. You can even then reuse your tests on your deployed resources too which is really cool
@cristianecheverria3995
@cristianecheverria3995 4 жыл бұрын
I'm having issues running integration tests after I decided to create multiple environments for my deploys, not sure if that's related but when I run the test using "tableName=user jest" it says "ResourceNotFoundException: Cannot do operations on a non-existent table" when I try to write into Dynamo table. Any suggestion?
@CompleteCoding
@CompleteCoding 4 жыл бұрын
Is the table that you are passing in (user) the same as the name of the table you've set in your resources config in serverless?
@cristianecheverria3995
@cristianecheverria3995 4 жыл бұрын
@@CompleteCoding I created a Gist file where you can see a resumed version of my serverless.yml file. Not sure if it looks ok, right now it works for my purpose of having multiple environments using --stage in AWS. gist.github.com/crisecheverria/15ea7aa275df344f1d1f647eb0af91d1
@CompleteCoding
@CompleteCoding 3 жыл бұрын
as you will be testing locally, you can just run usersTableName=user-dev projectsTableName=projects-dev jest
@mojgandadashi1
@mojgandadashi1 2 жыл бұрын
Sam do you have a video of writing the tests with java? what will be the difference if I want to write the tests in java?
@CompleteCoding
@CompleteCoding 2 жыл бұрын
If there is a java test library that can execute a function with an set event then it would be very similar. If you're also testing with a dynamo table you'll have to script a local version of dynamo to start and the configure your dynamo to connect to that. With JS, that is done by the jest dynamo plugin. There might even be a similar plugin for java
@PeteWhelan
@PeteWhelan 4 жыл бұрын
Hi Sam Having problems with this one - jest command wont take the env variables - every time I try to add the tableName= (at 20:23 in the tutorial) and get : "not recognized as the name of a cmdlet, function, script file, or operable program...." I have googled / stack overflowed this but can't find an answer that works for me. - any ideas whats wrong? Thanks in advance again
@CompleteCoding
@CompleteCoding 4 жыл бұрын
Hey Pete. Are you using Windows, Mac or Linux? Also are you entering it all on a single line
@PeteWhelan
@PeteWhelan 4 жыл бұрын
Sam - sorry for the delayed response. I'm using windows and yes I'm entering it on all on one line
@frozenn00b
@frozenn00b 3 жыл бұрын
@8:00 Why not define a default response object, then use the object spread method which has been supported since Node 8.6? ie: eventGenerator(options) { const default = {default event stuff}; return { ...default, ...options }; } -- this is a lot more flexible than the method you use, and requires no editing if later on you need to redefine a variable that you have blank in the default?
@CompleteCoding
@CompleteCoding 3 жыл бұрын
spread works brilliantly with flat objects but struggles with deep or nested objects. One thing we're setting it response.requestContext.httpMethod. If we used spread then you would have to pass in the whole requestContext object, not just the method value. You could also make a recursive function that means you can pass in partial objects and it joins them but that would be a lot more work
@frozenn00b
@frozenn00b 3 жыл бұрын
@@CompleteCoding I've been doing spreading for 2 years now, never seen a "struggle". Do you have any references for that as I would be curious what "struggles" exist? As far as the comment about passing the parent objects, yes, but it does also make it more readable, easier to maintain, and far more flexible/future proof.
@privymassage3458
@privymassage3458 3 жыл бұрын
Hi Sam, how can I support your channel? Info here is so pricesless just want to show some appreciation.
@CompleteCoding
@CompleteCoding 3 жыл бұрын
Right now the best thing I can do is to like and comment on the new videos that come out. It really helps show the video to more people. I'm toying with the KZfaq Membership, Pateron, or having some T-Shirts and merch. Is this the kind of thing you were thinking of?
@wendyliew7254
@wendyliew7254 2 жыл бұрын
what if the lambda is not exposed to API Gateway nor AppSync? can we still write some integration test against it? It's only called by some other lambda(s) that were exposed via AppSync.
@CompleteCoding
@CompleteCoding 2 жыл бұрын
Integration tests are often hard to define with Serverless. In this video I go with "testing a lambda locally without mocking - using local versions instead". In this case you can integration test any Lambda exactly as I do in this video. You may need to change the event that you pass into the handler from your test. The other definition of integration testing is "testing a deployed Lambda that integrates with all of the real services". You can also do this with no-API lambdas. Once everything is deployed, in your tests use the same `lambda.invoke` that you would when calling from another lambda. That will trigger the real lambda and you can see if your tests work.
@jc8212
@jc8212 3 жыл бұрын
Can you please create one vblog for cognito service to integrate jest test case. It's mean sign in, sign up, post confirmation.
@CompleteCoding
@CompleteCoding 3 жыл бұрын
That sounds like you're wanting to test what the user experience would be? Is that right?
@gajendrakumarmangaraja1261
@gajendrakumarmangaraja1261 4 жыл бұрын
ran the command "sls offline start --location ." . showing errors
@CompleteCoding
@CompleteCoding 4 жыл бұрын
The first thing is to check your version of serverless-offline. There was a change to v6 that broke the way that it works with the dynamo configuration. You can manually set the version to 5 and it should work
@nemesaenz8793
@nemesaenz8793 Жыл бұрын
Why not test actual calls to the api gateway instead of testing the handler function?
@CompleteCoding
@CompleteCoding Жыл бұрын
To test the actual endpoints you'd need to deploy the service first. This is actually what I do a lot more now. I automatically deploy a temporary stack to a 'testing' account when a PR is created. The tests then run and if they fail, that shows up in the PR.
Running Tests as part of the Serverless Deployment Process on AWS
9:31
Complete Coding - Master AWS Serverless
Рет қаралды 909
Serverless Offline for testing on your local machine. Tutorial with DynamoDB
18:16
Complete Coding - Master AWS Serverless
Рет қаралды 35 М.
FOOLED THE GUARD🤢
00:54
INO
Рет қаралды 64 МЛН
Wait for the last one! 👀
00:28
Josh Horton
Рет қаралды 136 МЛН
1❤️
00:17
Nonomen ノノメン
Рет қаралды 13 МЛН
Unit Testing in Serverless - tutorial including DynamoDB
27:24
Complete Coding - Master AWS Serverless
Рет қаралды 15 М.
Stop Writing So Many Tests
10:02
Web Dev Simplified
Рет қаралды 82 М.
AWS Lambda Cold Starts Deep Dive - How they work
17:14
Complete Coding - Master AWS Serverless
Рет қаралды 7 М.
How to send an email with Amazon SES and Serverless - tutorial
20:02
Complete Coding - Master AWS Serverless
Рет қаралды 26 М.
TESTING YOUR INFRASTRUCTURE AS CODE WITH AWS CDK (Typescript + Jest)
55:27
How to Create a WebSocket API using Amazon API Gateway and AWS Lambda - full tutorial
44:14
Complete Coding - Master AWS Serverless
Рет қаралды 35 М.
Testing with Jest: From zero to hero
36:56
LogRocket
Рет қаралды 63 М.
Adding New Data to your DynamoDB Database
14:45
Complete Coding - Master AWS Serverless
Рет қаралды 10 М.
Неразрушаемый смартфон
1:00
Status
Рет қаралды 2,3 МЛН
ИГРОВОВЫЙ НОУТ ASUS ЗА 57 тысяч
25:33
Ремонтяш
Рет қаралды 331 М.