Mocking with Serverless Offline and Integration Tests

808 views Asked by At

I have a Serverless stack (AWS) using API Gateway authentication and a Lambda, implementing a restful API using NestJS.

I'm using Serverless-offline to simulate the stack in my local environment. This allows me to simulate the API Gateway authentication (simple keys, not custom authorizer) and lambda execution from an end to end API call perspective.

I can use the NestJS test helpers to perform e2e tests, which allows me to inject mocks for other services not available in the stack.

What I'd like to do is use serverless-offline to run the tests - hence allowing me to test authentication via its simulated API Gateway. I can see how I can do this by launching serverless-offline in my tests (e.g. https://dev.to/didil/serverless-testing-strategies-4g92).

BUT, if I use serverless-offline (as link) then I can't see how it would be possible to inject mocks for other services not available in the stack.

Is there another solution for e2e testing that allows me to simulate the api gateway AND inject mocks?

Any help much appreciated!

1

There are 1 answers

0
Corey On

With the testing strategy you linked you can mock your requests and responses to external services to test different scenarios. Nock is a library that can simplify mocking external requests in your tests.

Although, it seems that this may not be work as a I imagine with serverless-offline. I found this answer that outlines a strategy for replacing endpoints that access external services when running tests.