Unit Tests in Serverless

Typically we end up testing the Lambda function directly as unit tests. But maybe you are looking for integration testing?

Hi Jayar,
Correct, I’m trying to implement a proper integration testing.

Yeah we haven’t covered it here. That might be something we look at in the future.

Thank you Jayair,
That’s pitty, as this tutorial is seriously the best one I could see anywhere on innternet and integration testing is an important task to accomplish in software development workflow.
On intenet I could not find any solution I liked when it comes to integration/acceptance testing using serverless without breaking the CI/CD workflow, so I ended up defining a pattern myself.
I dunno if it is the best way to do it but it is working well for my use case where I had other services to integrate and test on top of dynamoDB (ie. appsync graphql endpoints).

Basically, I used aws sdk to remotely login a test user and then get the deployed endpoints to be tested from a lambda which gets them from a cloudFormation stack output i.e.:
APPSYNC_URL: ${cf:${self:service}-${opt:stage}.GraphQlApiUrl}
and returns it to the integration-test when invoked.

The solution gives me a very high degree of confidence that all services are working well together and doesn’t break the CI/CD workflow everytime an endpoint is changed or added to the service.

On Seed platform worked like a charm.

1 Like

Awesome! Yeah that makes sense. Thanks for sharing!

I also ran into the “SyntaxError: Cannot use import statement outside a module” error. CommonJS style imports did the trick.

I’m using a v4.0 pdf of the tutorial that has Jest as the test runner. Reading up on the comments below, it sounds like there might be a new version of the tutorial using plug-in to run tests.

Oh thanks for letting me know. Can you post a screenshot from the PDF that needs to be updated?

Hello I receive this error whith serverless test option and also jest option the same

      19 files checked.
  testMatch: /Users/ilanohayon/escrow/notes-app-api/**/__tests__/**/*.{js,jsx,ts,tsx}, /Users/ilanohayon/escrow/notes-app-api/**/*.{spec,test}.{js,jsx,ts,tsx} - 0 matches
  testPathIgnorePatterns: /node_modules/ - 19 matches
  testRegex:  - 0 matches
Pattern:  - 0 matches
npm ERR! Test failed.  See above for more details.

Do you have any tests in your project?