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 fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142import { Writable } from 'node:stream'import { vi } from 'vitest'import type { APIGatewayProxyEvent, APIGatewayProxyEventV2 } from '../src/handler'import type { LambdaContext } from '../src/types'
type StreamifyResponseHandler = ( handlerFunc: ( event: APIGatewayProxyEvent | APIGatewayProxyEventV2, responseStream: Writable, context: LambdaContext ) => Promise<void>) => (event: APIGatewayProxyEvent, context: LambdaContext) => Promise<NodeJS.WritableStream>
const mockStreamifyResponse: StreamifyResponseHandler = (handlerFunc) => { return async (event, context) => { const chunks: unknown[] = [] const mockWritableStream = new Writable({ write(chunk, _encoding, callback) { chunks.push(chunk) callback() }, }) // @ts-expect-error chunks property for testing mockWritableStream.chunks = chunks await handlerFunc(event, mockWritableStream, context) mockWritableStream.end() return mockWritableStream }}
const awslambda = { streamifyResponse: mockStreamifyResponse, HttpResponseStream: { from: (stream: Writable, httpResponseMetadata: unknown): Writable => { stream.write(Buffer.from(JSON.stringify(httpResponseMetadata))) return stream }, },}
vi.stubGlobal('awslambda', awslambda)