Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix aws-lambda-nodejs test flakiness #26448

Merged
merged 5 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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