Skip to content

Commit

Permalink
chore: fix aws-lambda-nodejs test flakiness (aws#26448)
Browse files Browse the repository at this point in the history
Uses a fake file system in `aws-lambda-nodejs` tests to avoid intermittent test failures when attempting to access the real file system.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
mrgrain authored and bmoffatt committed Jul 28, 2023
1 parent 1202a60 commit 16ad440
Show file tree
Hide file tree
Showing 29 changed files with 123 additions and 265 deletions.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`esbuild bundling with esbuild options 1`] = `
""use strict";
(() => {
// aws-lambda-nodejs/test/integ-handlers/define.ts
// aws-lambda-nodejs/test/handlers/define.ts
function handler() {
return [
"VALUE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ test('esbuild bundling with esbuild options', () => {
});

// Make sure that the define instructions are working as expected with the esbuild CLI
const bundleProcess = util.exec('bash', ['-c', `npx esbuild --bundle ${`${__dirname}/integ-handlers/define.ts`} ${defineInstructions}`]);
const bundleProcess = util.exec('bash', ['-c', `npx esbuild --bundle ${`${__dirname}/handlers/define.ts`} ${defineInstructions}`]);
expect(bundleProcess.stdout.toString()).toMatchSnapshot();
});

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

46 changes: 37 additions & 9 deletions packages/aws-cdk-lib/aws-lambda-nodejs/test/function.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as fs from 'fs';
import * as path from 'path';
import { bockfs } from '@aws-cdk/cdk-build-tools';
import { Template, Match } from '../../assertions';
import { Vpc } from '../../aws-ec2';
import { CodeConfig, Runtime } from '../../aws-lambda';
Expand All @@ -25,12 +24,45 @@ jest.mock('../lib/bundling', () => {
};
});

const mockCallsites = jest.fn();
jest.mock('../lib/util', () => ({
...jest.requireActual('../lib/util'),
callsites: () => mockCallsites(),
}));

let stack: Stack;
beforeEach(() => {
stack = new Stack();
jest.clearAllMocks();
});

// We MUST use a fake file system here.
// Using the real filesystem causes the tests to be flaky and fail at random.
// This way we are guaranteed to have the fake files setup on each test run.
bockfs({
'/home/project/package.json': '{}',
'/home/project/package-lock.json': '{}',
'/home/project/handler.tsx': '// nothing',
'/home/project/function.test.handler1.ts': '// nothing',
'/home/project/function.test.handler2.js': '// nothing',
'/home/project/function.test.handler3.mjs': '// nothing',
'/home/project/function.test.handler4.mts': '// nothing',
'/home/project/function.test.handler5.cts': '// nothing',
'/home/project/function.test.handler6.cjs': '// nothing',
'/home/project/aws-lambda-nodejs/lib/index.ts': '// nothing',
});
const bockPath = bockfs.workingDirectory('/home/project');

// pretend the calling file is in a fake file path
mockCallsites.mockImplementation(() => [
{ getFunctionName: () => 'NodejsFunction' },
{ getFileName: () => bockPath`function.test.ts` },
]);

afterAll(() => {
bockfs.restore();
});

test('NodejsFunction with .ts handler', () => {
// WHEN
new NodejsFunction(stack, 'handler1');
Expand Down Expand Up @@ -151,15 +183,11 @@ test('throws when entry is not js/ts', () => {
});

test('accepts tsx', () => {
const entry = path.join(__dirname, 'handler.tsx');

fs.symlinkSync(path.join(__dirname, 'function.test.handler1.ts'), entry);
const entry = bockPath`handler.tsx`;

expect(() => new NodejsFunction(stack, 'Fn', {
entry,
})).not.toThrow();

fs.unlinkSync(entry);
});

test('throws when entry does not exist', () => {
Expand Down Expand Up @@ -196,7 +224,7 @@ test('resolves depsLockFilePath to an absolute path', () => {
});

expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({
depsLockFilePath: expect.stringMatching(/aws-cdk-lib\/package.json$/),
depsLockFilePath: bockPath`/home/project/package.json`,
}));
});

Expand All @@ -207,7 +235,7 @@ test('resolves entry to an absolute path', () => {
});

expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({
entry: expect.stringMatching(/aws-cdk-lib\/aws-lambda-nodejs\/lib\/index.ts$/),
entry: bockPath`/home/project/aws-lambda-nodejs/lib/index.ts`,
}));
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export function handler() {
process.env.NUMBER,
process.env.STRING,
];
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 16ad440

Please sign in to comment.