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?

Was cruising through this great tut, but after successfully running unit tests, failed to serverless deploy with this error:
“TypeError [ERR_INVALID_ARG_TYPE]: The “to” argument must be of type string. Received undefined
at validateString (internal/validators.js:117:11)
at Object.relative (path.js:438:5)”

From that point any serverless command resulted in the same error. The project downloaded from here: https://github.com/AnomalyInnovations/serverless-stack-demo-api/tree/handle-api-gateway-cors-errors runs fine. From what I can see I have everything in order. The only difference is additional resources and resulting changes to serverless.yml that were not in the tut.

Any idea how to troubleshoot? I went through steps again with the same results. What am I missing?

That’s really weird. Can you paste the full error plus the command here?

If all your commands are failing. Maybe try removing your node_modules and running npm install again?

Yeah, smth got screwed up with either project or node_modules. Restarting the project helped. Thanks!

1 Like

In my opinion, this chapter should be a bit more detailed. The guide as a whole is very detailed, and enjoy reading every part of it, however, comparing this chapter to the rest of the guide, I would expect to see more.

I guess the following subjects would be great:

  1. What should be tested for stacks and/or resources?
  2. Testing an API Gateway route

Thanks for the chapter though!

Yeah this chapter is fairly open ended. We should have some best practices here for testing.

1 Like

I’m following the web site instructions but it appears to be missing the code to calculate the cost (or did I just skip something?). I get the following output when running npx sst test

Preparing your SST app
 FAIL  test/cost.test.js
  ● Test suite failed to run

    Cannot find module '../src/util/cost' from 'test/cost.test.js'

    > 1 | import { calculateCost } from "../src/util/cost";
        |                               ^
      2 |
      3 | test("Lowest tier", () => {
      4 |   const storage = 10;

      at Resolver.resolveModule (node_modules/@serverless-stack/cli/node_modules/jest-resolve/build/resolver.js:324:11)
      at Object.<anonymous> (test/cost.test.js:1:31)

I’ve copy the code from the Github repository here:
https://github.com/serverless-stack/demo-notes-app/blob/main/src/util/cost.js

Hmm the paths seem right? Did you get this when you tried it on that repo?

Sorry, I did not explain myself correctly. The import line is fine - but the cost.js file was not created. Did I miss the instructions to create that file? I did not see any such instructions.

We added it in the previous chapter: Add an API to Handle Billing

My bad - I skipped that chapter as I was not interested in adding billing.
Thanks for the great material!

can you help me ?