-
Notifications
You must be signed in to change notification settings - Fork 185
Description
Summary
Currently, to test the stream functionality we create we need to implement a mock streaming class in our tests. However, this helper mock file will not be available to users who wish to test their streaming functions so they will have to implement this mock themselves. We should create a ResponseStream class that is used by the fallback version of streamifyHandler that can be used in our tests in utils module. Customers will also be able to use this class in their own tests too.
Why is this needed?
This simplifies testing as we are using the same implementation in the fallabck and the tests.
Which area does this relate to?
Event Handler
Solution
Unfortunately, the types in aws-lambda package are not correct so we will have to do some workarounds to make everything type check. But ultimately we should be able to do this:
import {ResponseStream} from "../../../../src/rest/utils.js";
it('streams a simple JSON response', async () => {
// Prepare
const app = new Router();
app.get('/test', async () => ({ message: 'Hello, World!' }));
const handler = streamify(app);
const responseStream = new ResponseStream();
// Act
const result = await handler(
createEvent('/test', 'GET'),
responseStream,
context
);
// Assess
expect(result.statusCode).toBe(200);
expect(JSON.parse(result.body)).toEqual({ message: 'Hello, World!' });
});Acknowledgment
- This request meets Powertools for AWS Lambda (TypeScript) Tenets
- Should this be considered in other Powertools for AWS Lambda languages? i.e. Python, Java, and .NET
Future readers
Please react with 👍 and your use case to help us understand customer demand.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status