Testing in a TypeScript/Node/Express backend that makes calls to AWS using Jest

As per title, I have a backend written in TypeScript/Node/Express that makes calls to different AWS services (Cognito, S3, DynamoDB etc.) using AWS SDK.

For example, I will have a route that calls a function that retrieves data from an S3 bucket, or starts the login flow with Cognito.

I would like to start writing a testing suite to test my code using Jest, but I have no idea to do this. Should I make calls to the actual AWS? Or should I recreate the methods from scratch to use in my tests?
I even tried to use a library called aws-sdk-mock but the documentation wasn’t very helpful and I couldn’t understand what I was doing.

I’m just curious to understand how other people have tackled this. Thanks.

Well, I’d recommend you to first create a test strategy. So, using Jest you can do the unit testing layer, which usually assesses only your actual code working - it means, mocking all AWS SDK services and checking all happy/exception paths (think of coverage). You don’t really need a lib for this but feel free to find your solution.

Then, on top of this you can think of an integrated testing layer which runs on an environment with access to the deployed resources - like a Dev/QA env - and then it allows your code to effectively call the resources and assess the results.

This way you cover all aspects of your code: your code logic and objectives in the first layer and the architecture (including CI CD and infrastructure) in the second layer. A third layer could be interesting for user side tests (end-to-end comes to mind).

Don’t knock the mock, this is the way.

Isolate the interfacing to external services and mock the contract to that module. Can totally write additional integration tests to AWS that would be from the interfacing module to AWS, but don’t make those calls in unit.

Finally, Localstack may be useful.

You should have tests that mock out the sdk calls. There’s a blog post about it: https://aws.amazon.com/blogs/developer/mocking-modular-aws-sdk-for-javascript-v3-in-unit-tests/

You can write plenty of these and they should be quick and easy to run.

But you also need tests where you call the actual sdk api, otherwise you won’t know if those api calls are correct.

These tests will be slower and you won’t need so many of them but you still need them.

At work for these tests, we deploy our code to its own environment and then use cucumber.js to test it.

Having a look at the testing pyramid to help you refine your testing strategy.

You have a few options, depending on if you want to test individual modules or test the whole application flow, or some combination of the two.

Unit Testing w/ stubs

If you simply want to stub/mock the AWS modules, jest.mock should work pretty well for your use case. If you’re using the V3 SDK this becomes a lot easier too because all of the AWS service clients are their own packages. For simple one-method mocks you can even do something like this:

import { S3Client } from ‘@aws-sdk/client-s3’;

jest.mock('@aws-sdk/client-s3', () => {
   return {
    S3Client: {
      send: jest.fn().mockResolvedValue({ foo: true })
    }
  }
});

await myCodeThatNeedsS3();

// track calls to send via prototype
expect(S3Client.prototype.send).toHaveBeenCalledTimes(1);

Integration Testing

If you want to test behavior in a more integration test style, then that’s where things like Localstack and dynamodb-local come into play, although they will require that you have external dependencies installed in your build/test environment. Combine these with the supertest library and you can test your app end-to-end with mock services running locally. Although this typically requires a lot more work to get set up, it can be a lot more useful to test things this way.

using localstack is another option if you don’t want to have to deploy to an environment for tests.

Idk how spicy this take is, but my org spends so many engineering hours fighting localstack issues that we would be richer if we spent those dollars on ephemeral account generation.

Hopefully we’ll have some progress in that direction, soon.

This, use interfaces instead.

Separate your tests with unit and integration tests mentioned above. Just be careful when writing integration tests as test calls will write data to your dynamoDB table depending on you function logic

Python has https://github.com/spulec/moto which creates lightweight test doubles so you actually exercise all the code.

Never did find a JS analog

Write stubs for all your API calls using jest.fn(). Do not make actual calls in your tests - you do not need to test your dependencies

Could you please elaborate on what you mean by the third layer?

Oh hai, what an old message. So, by user side tests (a third layer in this case) I meant a Front-End test that would run actual use cases and request your actual API. Very often this End-to-end test runs against your “Stage” environment with the closest version to Production code.