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
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
@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?
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
@@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.
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.
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.
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?
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
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?
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
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 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
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 RUclips Membership, Pateron, or having some T-Shirts and merch. Is this the kind of thing you were thinking of?
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
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.
Perfect content! I want to leave a comment for supporting chanel!
Much appreciated!
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
There is a video I've done on that just here ruclips.net/video/ul_85jfM0oo/видео.html
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
Hey Pete. Are you using Windows, Mac or Linux?
Also are you entering it all on a single line
Sam - sorry for the delayed response. I'm using windows and yes I'm entering it on all on one line
@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?
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
@@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.
Awesome explaination 👍
Glad it was helpful!
Thank you for sharing
Excellent video
Thanks!
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.
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.
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?
When you run tests it creates a local mock of dynamo so it doesn't polute your real data
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?
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
This will work through cdk code?
Awesome stuff,
Just want to know how can mock context parameter of handler function from unit test, can we pass something like contextGenerator()?
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
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?
The ticks and crosses are the "vs code jest" extension. The multi coloured tabs is "Rainbow intent"
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?
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
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?
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?
@@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
as you will be testing locally, you can just run
usersTableName=user-dev projectsTableName=projects-dev jest
Hi Sam, how can I support your channel? Info here is so pricesless just want to show some appreciation.
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 RUclips Membership, Pateron, or having some T-Shirts and merch. Is this the kind of thing you were thinking of?
ran the command "sls offline start --location ." . showing errors
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
Why not test actual calls to the api gateway instead of testing the handler function?
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.
Can you please create one vblog for cognito service to integrate jest test case. It's mean sign in, sign up, post confirmation.
That sounds like you're wanting to test what the user experience would be? Is that right?
Great content!!
Glad you liked it Christian