Skip to content

Commit 6d66d8f

Browse files
committed
test: don't destroy lambda env vars until response stream finished
1 parent c6a8699 commit 6d66d8f

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

Diff for: tests/utils/fixture.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,24 @@ export async function invokeFunction(
364364
NETLIFY_BLOBS_CONTEXT: createBlobContext(ctx),
365365
...(env || {}),
366366
}
367+
368+
const envVarsToRestore = {}
369+
370+
// We are not using lambda-local's environment variable setting because it cleans up
371+
// environment vars to early (before stream is closed)
372+
Object.keys(environment).forEach(function (key) {
373+
if (typeof process.env[key] !== 'undefined') {
374+
envVarsToRestore[key] = process.env[key]
375+
}
376+
process.env[key] = environment[key]
377+
})
378+
367379
const response = (await execute({
368380
event: {
369381
headers: headers || {},
370382
httpMethod: httpMethod || 'GET',
371383
rawUrl: new URL(url || '/', 'https://example.netlify').href,
372384
},
373-
environment,
374-
envdestroy: true,
375385
lambdaFunc: { handler },
376386
timeoutMs: 4_000,
377387
})) as LambdaResponse
@@ -386,6 +396,14 @@ export async function invokeFunction(
386396

387397
const bodyBuffer = await streamToBuffer(response.body)
388398

399+
Object.keys(environment).forEach(function (key) {
400+
if (typeof envVarsToRestore[key] !== 'undefined') {
401+
process.env[key] = envVarsToRestore[key]
402+
} else {
403+
delete process.env[key]
404+
}
405+
})
406+
389407
return {
390408
statusCode: response.statusCode,
391409
bodyBuffer,

Diff for: tests/utils/sandbox-child.mjs

+17-2
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,21 @@ process.on('message', async (msg) => {
4848
...(env || {}),
4949
}
5050

51+
// We are not using lambda-local's environment variable setting because it cleans up
52+
// environment vars to early (before stream is closed)
53+
Object.keys(environment).forEach(function (key) {
54+
if (typeof process.env[key] !== 'undefined') {
55+
envVarsToRestore[key] = process.env[key]
56+
}
57+
process.env[key] = environment[key]
58+
})
59+
5160
const response = await execute({
5261
event: {
5362
headers: headers || {},
5463
httpMethod: httpMethod || 'GET',
5564
rawUrl: new URL(url || '/', 'https://example.netlify').href,
5665
},
57-
environment,
58-
envdestroy: true,
5966
lambdaFunc: { handler },
6067
timeoutMs: 4_000,
6168
})
@@ -70,6 +77,14 @@ process.on('message', async (msg) => {
7077

7178
const bodyBuffer = await streamToBuffer(response.body)
7279

80+
Object.keys(environment).forEach(function (key) {
81+
if (typeof envVarsToRestore[key] !== 'undefined') {
82+
process.env[key] = envVarsToRestore[key]
83+
} else {
84+
delete process.env[key]
85+
}
86+
})
87+
7388
const result = {
7489
statusCode: response.statusCode,
7590
bodyBuffer,

0 commit comments

Comments
 (0)