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

Поделиться
HTML-код
  • Опубликовано: 3 дек 2024

Комментарии • 42

  • @konstantin6524
    @konstantin6524 3 года назад

    Perfect content! I want to leave a comment for supporting chanel!

  • @yaroslavdraha9251
    @yaroslavdraha9251 4 года назад +1

    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  4 года назад

      There is a video I've done on that just here ruclips.net/video/ul_85jfM0oo/видео.html

  • @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 года назад +1

      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.

  • @jc8212
    @jc8212 3 года назад

    Awesome explaination 👍

  • @MarzanDaniel
    @MarzanDaniel Год назад

    Thank you for sharing

  • @maopuerta3430
    @maopuerta3430 4 года назад

    Excellent video

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

  • @EstebanHelgueroCardiff
    @EstebanHelgueroCardiff 4 года назад

    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  4 года назад

      When you run tests it creates a local mock of dynamo so it doesn't polute your real data

  • @madhavareddy3488
    @madhavareddy3488 2 года назад

    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  2 года назад

      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

  • @bhavyamikkilineni5107
    @bhavyamikkilineni5107 11 дней назад

    This will work through cdk code?

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

  • @ahmadnabil2441
    @ahmadnabil2441 3 года назад

    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  3 года назад

      The ticks and crosses are the "vs code jest" extension. The multi coloured tabs is "Rainbow intent"

  • @mojgandadashi1
    @mojgandadashi1 3 года назад

    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  3 года назад

      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

  • @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  4 года назад

      as you will be testing locally, you can just run
      usersTableName=user-dev projectsTableName=projects-dev jest

  • @privymassage3458
    @privymassage3458 4 года назад +2

    Hi Sam, how can I support your channel? Info here is so pricesless just want to show some appreciation.

    • @CompleteCoding
      @CompleteCoding  4 года назад

      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?

  • @gajendrakumarmangaraja1261
    @gajendrakumarmangaraja1261 4 года назад

    ran the command "sls offline start --location ." . showing errors

    • @CompleteCoding
      @CompleteCoding  4 года назад +1

      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 2 года назад

    Why not test actual calls to the api gateway instead of testing the handler function?

    • @CompleteCoding
      @CompleteCoding  2 года назад

      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.

  • @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?

  • @cristianecheverria3995
    @cristianecheverria3995 4 года назад

    Great content!!