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).
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.
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:
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.
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.
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
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.