Skip to content

Commit 6ff7588

Browse files
committed
test(jest): prepare unit tests
1 parent 7307fcc commit 6ff7588

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"typescript"
3939
],
4040
"devDependencies": {
41+
"@aws-cdk/assert": "^1.70.0",
4142
"@aws-cdk/aws-lambda": "^1.70.0",
4243
"@aws-cdk/core": "^1.70.0",
4344
"@commitlint/cli": "^11.0.0",

src/index.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
11
import * as lambda from '@aws-cdk/aws-lambda';
2+
import * as cdk from '@aws-cdk/core';
3+
4+
import { nodeMajorVersion } from './utils';
5+
6+
export interface NodejsFunctionProps extends lambda.FunctionOptions {
7+
/**
8+
* The name of the exported handler in the entry file.
9+
*
10+
* @default "handler"
11+
*/
12+
readonly handler?: string;
13+
/**
14+
* The runtime environment. Only runtimes of the Node.js family are
15+
* supported.
16+
*
17+
* @default - `NODEJS_12_X` if `process.versions.node` >= '12.0.0',
18+
* `NODEJS_10_X` otherwise.
19+
*/
20+
readonly runtime?: lambda.Runtime;
21+
}
222

323
export class NodejsFunction extends lambda.Function {
24+
constructor(scope: cdk.Construct, id: string, props: NodejsFunctionProps = {}) {
25+
if (props.runtime && props.runtime.family !== lambda.RuntimeFamily.NODEJS) {
26+
throw new Error('Only `NODEJS` runtimes are supported.');
27+
}
28+
29+
const handler = props.handler ?? 'handler';
30+
const defaultRunTime = nodeMajorVersion() >= 12
31+
? lambda.Runtime.NODEJS_12_X
32+
: lambda.Runtime.NODEJS_10_X;
33+
const runtime = props.runtime ?? defaultRunTime;
434

35+
super(scope, id, {
36+
...props,
37+
runtime,
38+
code: lambda.Code.fromInline('TODO'),
39+
handler: `index.${handler}`,
40+
});
41+
}
542
}

src/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Returns the major version of node installation
3+
*/
4+
export function nodeMajorVersion(): number {
5+
return parseInt(process.versions.node.split('.')[0], 10);
6+
}

tests/index.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import '@aws-cdk/assert/jest';
2+
import { Stack } from '@aws-cdk/core';
3+
import { NodejsFunction } from '../src';
4+
5+
describe('NodejsFunction tests', () => {
6+
it('Should not class constructor be thrown', async () => {
7+
const stack = new Stack();
8+
const factory = () => new NodejsFunction(stack, 'lambda-function', {});
9+
expect(factory).not.toThrow();
10+
expect(stack).toHaveResource('AWS::Lambda::Function', {
11+
Handler: 'index.handler',
12+
});
13+
});
14+
});

0 commit comments

Comments
 (0)