Skip to content

Commit 7c8b29b

Browse files
committed
test: cover conditions with unit tests
1 parent 58d319d commit 7c8b29b

File tree

3 files changed

+75
-16
lines changed

3 files changed

+75
-16
lines changed

src/utils.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
33

4+
/**
5+
* Extracts the file name from handler string.
6+
*/
47
export function extractFileName(cwd: string, handler: string): string {
58
const fnName = path.extname(handler);
69
const fnNameLastAppearanceIndex = handler.lastIndexOf(fnName);
@@ -40,7 +43,10 @@ export function findUp(name: string, directory: string = process.cwd()): string
4043
return findUp(name, path.dirname(absoluteDirectory));
4144
}
4245

43-
export function findProjectRoot(rootDir: string): string {
46+
/**
47+
* Forwards `rootDir` or finds project root folder.
48+
*/
49+
export function findProjectRoot(rootDir?: string): string | undefined {
4450
return rootDir
4551
?? findUp(`.git${path.sep}`)
4652
?? findUp('yarn.lock')

tests/index.test.ts

+67-15
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,84 @@ jest.mock('esbuild');
22

33
import '@aws-cdk/assert/jest';
44

5+
import { Runtime, RuntimeFamily } from '@aws-cdk/aws-lambda';
56
import { Stack } from '@aws-cdk/core';
67
import { buildSync } from 'esbuild';
78
import mockfs from 'mock-fs';
9+
import path from 'path';
810

911
import { NodejsFunction } from '../src';
1012

13+
1114
describe('NodejsFunction tests', () => {
12-
beforeAll(() => {
13-
mockfs({
14-
'index.ts': '',
15-
'package-lock.json': '',
16-
'.build': {}
15+
describe('with valid folder structure', () => {
16+
beforeAll(() => {
17+
mockfs({
18+
'index.ts': '',
19+
'source/index.ts': '',
20+
'main.ts': '',
21+
'a/b/c.ts': '',
22+
'src': {
23+
'index.ts': '',
24+
'.build': {}
25+
},
26+
'package-lock.json': '',
27+
'.build': {}
28+
});
1729
});
18-
});
1930

20-
it('Should not class constructor be thrown', async () => {
21-
const stack = new Stack();
22-
const factory = () => new NodejsFunction(stack, 'lambda-function', {});
23-
expect(factory).not.toThrow();
24-
expect(buildSync).toBeCalledTimes(1);
25-
expect(stack).toHaveResource('AWS::Lambda::Function', {
26-
Handler: 'index.handler',
31+
beforeEach(() => {
32+
(buildSync as jest.Mock).mockReset();
33+
});
34+
35+
it.each(Runtime.ALL.filter(r => r.family !== RuntimeFamily.NODEJS))('Should be thrown due to not supported runtime', (runtime) => {
36+
const constructor = () => new NodejsFunction(new Stack(), 'lambda-function', { runtime });
37+
expect(constructor).toThrowError(/^Only `NODEJS` runtimes are supported.$/);
38+
});
39+
40+
it('Should not be thrown', () => {
41+
const stack = new Stack();
42+
const constructor = () => new NodejsFunction(stack, 'lambda-function');
43+
expect(constructor).not.toThrow();
44+
expect(buildSync).toBeCalledTimes(1);
45+
expect(stack).toHaveResource('AWS::Lambda::Function', {
46+
Handler: 'index.handler',
47+
});
48+
});
49+
50+
it.each`
51+
handler | entry
52+
${null} | ${'index.ts'}
53+
${'source/index.handler'} | ${'source/index.ts'}
54+
${'main.func'} | ${'main.ts'}
55+
${'a/b/c.h'} | ${'a/b/c.ts'}
56+
`('Should be valid entry with default rootDir', ({ handler, entry }) => {
57+
new NodejsFunction(new Stack(), 'lambda-function', { handler });
58+
expect(buildSync).toHaveBeenCalledWith(expect.objectContaining({ entryPoints: [entry] }));
59+
});
60+
61+
it('Should be valid outdir with custom rootDir', () => {
62+
new NodejsFunction(new Stack(), 'lambda-function', { rootDir: path.join(__dirname, '../src') });
63+
expect(buildSync).toHaveBeenCalledWith(expect.objectContaining({ outdir: path.join(__dirname, '../src', '.build') }));
64+
});
65+
66+
afterAll(() => {
67+
mockfs.restore();
2768
});
2869
});
2970

30-
afterAll(() => {
31-
mockfs.restore();
71+
describe('with invalid folder structure', () => {
72+
beforeAll(() => {
73+
mockfs();
74+
});
75+
76+
it('Should be thrown due to unrecognised root directory', () => {
77+
const constructor = () => new NodejsFunction(new Stack(), 'lambda-function');
78+
expect(constructor).toThrowError(/^Cannot find root directory./);
79+
});
80+
81+
afterAll(() => {
82+
mockfs.restore();
83+
});
3284
});
3385
});

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"declaration": true,
77
"skipLibCheck": true,
88
"esModuleInterop": true,
9+
"strictNullChecks": true,
910
"lib": ["ES2017"]
1011
},
1112
"exclude": [

0 commit comments

Comments
 (0)