Skip to content

Commit bd9771b

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

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-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

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

51+
const envVarsToRestore = {}
52+
53+
// We are not using lambda-local's environment variable setting because it cleans up
54+
// environment vars to early (before stream is closed)
55+
Object.keys(environment).forEach(function (key) {
56+
if (typeof process.env[key] !== 'undefined') {
57+
envVarsToRestore[key] = process.env[key]
58+
}
59+
process.env[key] = environment[key]
60+
})
61+
5162
const response = await execute({
5263
event: {
5364
headers: headers || {},
5465
httpMethod: httpMethod || 'GET',
5566
rawUrl: new URL(url || '/', 'https://example.netlify').href,
5667
},
57-
environment,
58-
envdestroy: true,
5968
lambdaFunc: { handler },
6069
timeoutMs: 4_000,
6170
})
@@ -70,6 +79,14 @@ process.on('message', async (msg) => {
7079

7180
const bodyBuffer = await streamToBuffer(response.body)
7281

82+
Object.keys(environment).forEach(function (key) {
83+
if (typeof envVarsToRestore[key] !== 'undefined') {
84+
process.env[key] = envVarsToRestore[key]
85+
} else {
86+
delete process.env[key]
87+
}
88+
})
89+
7390
const result = {
7491
statusCode: response.statusCode,
7592
bodyBuffer,

0 commit comments

Comments
 (0)