Skip to content

Commit

Permalink
nice testsss
Browse files Browse the repository at this point in the history
  • Loading branch information
bergjaak committed May 15, 2024
1 parent 63a1959 commit 12134f9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{
"type": "node",
"request": "launch",
"args": ["jest", "--runTestsByPath", "/Users/bergjak/workplace/CDK/aws-cdk/packages/aws-cdk-lib/aws-lambda/test/code.test.ts"],
"args": ["jest", "--runTestsByPath", "/Users/bergjak/workplace/CDK/aws-cdk/packages/aws-cdk-lib/aws-lambda-nodejs/test/function.test.ts"],
"name": "debug unit test",
"program": "/opt/homebrew/bin/yarn",
"cwd": "/Users/bergjak/workplace/CDK/aws-cdk/packages/aws-cdk-lib",
Expand Down
47 changes: 46 additions & 1 deletion packages/aws-cdk-lib/aws-lambda-nodejs/test/function.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as child_process from 'child_process';
import { bockfs } from '@aws-cdk/cdk-build-tools';
import { Template, Match } from '../../assertions';
import { Vpc } from '../../aws-ec2';
import { CodeConfig, Runtime } from '../../aws-lambda';
import { Code, CodeConfig, Runtime } from '../../aws-lambda';
import { App, Stack } from '../../core';
import { LAMBDA_NODEJS_USE_LATEST_RUNTIME } from '../../cx-api';
import { NodejsFunction } from '../lib';
Expand Down Expand Up @@ -50,6 +51,7 @@ bockfs({
'/home/project/function.test.handler4.mts': '// nothing',
'/home/project/function.test.handler5.cts': '// nothing',
'/home/project/function.test.handler6.cjs': '// nothing',
'/home/project/function.test.handler7.zip': '// nothing',
'/home/project/aws-lambda-nodejs/lib/index.ts': '// nothing',
});
const bockPath = bockfs.workingDirectory('/home/project');
Expand Down Expand Up @@ -78,6 +80,49 @@ test('NodejsFunction with .ts handler', () => {
});
});

describe('lambda.Code.fromCustomCommand', () => {
// GIVEN
beforeEach(() => {
jest.spyOn(child_process, 'spawnSync').mockReturnValue({
status: 0,
stderr: Buffer.from('stderr'),
stdout: Buffer.from('stdout'),
pid: 123,
output: ['stdout', 'stderr'],
signal: null,
});
});
afterEach(() => {
jest.restoreAllMocks();
});

test('if code property is included without handler property, then error is thrown', () => {
// WHEN
const handlerName = undefined;

// THEN
expect(() => new NodejsFunction(stack, 'handler1', {
handler: handlerName,
code: Code.fromCustomCommand('function.test.handler7.zip', ['node'], undefined),
})).toThrow('Cannot determine handler when `code` property is specified. Use `handler` property to specify a handler.');
});

test('if code and handler properties are included, the template can be synthesized', () => {
// WHEN
new NodejsFunction(stack, 'handler1', {
handler: 'Random.Name',
runtime: Runtime.NODEJS_18_X,
code: Code.fromCustomCommand('function.test.handler7.zip', ['node'], undefined),
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
Handler: 'Random.Name',
Runtime: 'nodejs18.x',
});
});
});

test('NodejsFunction with overridden handler - no dots', () => {
// WHEN
new NodejsFunction(stack, 'handler1', {
Expand Down

0 comments on commit 12134f9

Please sign in to comment.