Skip to content
Myr

stream-mock.ts

This file

Path

adapters/aws-lambda/test/stream-mock.ts

41 lines · 1.3 KiB · TypeScript

Last changed by

test: move adapter runtime tests into `adapters/*` (#5466)Yusuke Wadabefore adoption645ddd6

History of this file

Take it

test: move adapter runtime tests into `adapters/*` (#5466)Yusuke Wadabefore adoption645ddd6

lines 1–41why, one step back
1import { Writable } from 'node:stream'
2import { vi } from 'vitest'3import type { APIGatewayProxyEvent, APIGatewayProxyEventV2 } from '../src/handler'4import type { LambdaContext } from '../src/types'56type StreamifyResponseHandler = (7 handlerFunc: (8 event: APIGatewayProxyEvent | APIGatewayProxyEventV2,9 responseStream: Writable,10 context: LambdaContext11 ) => Promise<void>12) => (event: APIGatewayProxyEvent, context: LambdaContext) => Promise<NodeJS.WritableStream>1314const mockStreamifyResponse: StreamifyResponseHandler = (handlerFunc) => {15 return async (event, context) => {16 const chunks: unknown[] = []17 const mockWritableStream = new Writable({18 write(chunk, _encoding, callback) {19 chunks.push(chunk)20 callback()21 },22 })23 // @ts-expect-error chunks property for testing24 mockWritableStream.chunks = chunks25 await handlerFunc(event, mockWritableStream, context)26 mockWritableStream.end()27 return mockWritableStream28 }29}3031const awslambda = {32 streamifyResponse: mockStreamifyResponse,33 HttpResponseStream: {34 from: (stream: Writable, httpResponseMetadata: unknown): Writable => {35 stream.write(Buffer.from(JSON.stringify(httpResponseMetadata)))36 return stream37 },38 },39}4041vi.stubGlobal('awslambda', awslambda)