Is there a recommended library for mocking AWS ser...
# help
d
Is there a recommended library for mocking AWS services to test lambdas?
r
d
It seems like it expects that you use clients as shown at this website: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/index.html as opposed to importing the sdk like below:
Copy code
import * as aws from 'aws-sdk';
const s3 = aws.S3();
// ... do things with that such as
s3.upload( ... );
Am I understanding that correctly? My code sample above is what I currently do with my lambda, but the mock library docs seem to imply that they want me to do it the other way as in the link
t
We generally recommend not to mock aws services and run tests against a real deployment
Less room for "works in tests but fails in prod"
d
It seems like it expects that you use clients...
If you are still using the v2 sdk, there is another mocking library you can use, but its also pretty easy to just use
jest.mock
in that case, since they are all together.
We generally recommend not to mock aws services and run tests against a real deployment
@thdxr, I think mocking for unit tests is the only reasonable solution, but I think integration tests can be useful as well as a backup. "Testing pyramid", yeah?
r
I think both unit tests with mocked external APIs and integration tests are valuable.
t
What I do is fully isolate the interactions with AWS stuff - leaving very little code in modules responsible for that. And then I mock those modules when unit testing the rest of my app. That way I don't try to mock the aws stuff itself (which will all due respect will never give you what you really want - those things underneath are really complex, and no mock will give you assurance that your code really works when interacting with those). I will always write a small bit of e2e tests that run on the real thing ( which is such a breeze with SST when I can create a full env on demand on pul request with GH actions and just get my code fully tested in isolation)