Skip to content

Commit

Permalink
test: cover conditions with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
floydspace committed Oct 25, 2020
1 parent 58d319d commit 7c8b29b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 16 deletions.
8 changes: 7 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as fs from 'fs';
import * as path from 'path';

/**
* Extracts the file name from handler string.
*/
export function extractFileName(cwd: string, handler: string): string {
const fnName = path.extname(handler);
const fnNameLastAppearanceIndex = handler.lastIndexOf(fnName);
Expand Down Expand Up @@ -40,7 +43,10 @@ export function findUp(name: string, directory: string = process.cwd()): string
return findUp(name, path.dirname(absoluteDirectory));
}

export function findProjectRoot(rootDir: string): string {
/**
* Forwards `rootDir` or finds project root folder.
*/
export function findProjectRoot(rootDir?: string): string | undefined {
return rootDir
?? findUp(`.git${path.sep}`)
?? findUp('yarn.lock')
Expand Down
82 changes: 67 additions & 15 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,84 @@ jest.mock('esbuild');

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

import { Runtime, RuntimeFamily } from '@aws-cdk/aws-lambda';
import { Stack } from '@aws-cdk/core';
import { buildSync } from 'esbuild';
import mockfs from 'mock-fs';
import path from 'path';

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


describe('NodejsFunction tests', () => {
beforeAll(() => {
mockfs({
'index.ts': '',
'package-lock.json': '',
'.build': {}
describe('with valid folder structure', () => {
beforeAll(() => {
mockfs({
'index.ts': '',
'source/index.ts': '',
'main.ts': '',
'a/b/c.ts': '',
'src': {
'index.ts': '',
'.build': {}
},
'package-lock.json': '',
'.build': {}
});
});
});

it('Should not class constructor be thrown', async () => {
const stack = new Stack();
const factory = () => new NodejsFunction(stack, 'lambda-function', {});
expect(factory).not.toThrow();
expect(buildSync).toBeCalledTimes(1);
expect(stack).toHaveResource('AWS::Lambda::Function', {
Handler: 'index.handler',
beforeEach(() => {
(buildSync as jest.Mock).mockReset();
});

it.each(Runtime.ALL.filter(r => r.family !== RuntimeFamily.NODEJS))('Should be thrown due to not supported runtime', (runtime) => {
const constructor = () => new NodejsFunction(new Stack(), 'lambda-function', { runtime });
expect(constructor).toThrowError(/^Only `NODEJS` runtimes are supported.$/);
});

it('Should not be thrown', () => {
const stack = new Stack();
const constructor = () => new NodejsFunction(stack, 'lambda-function');
expect(constructor).not.toThrow();
expect(buildSync).toBeCalledTimes(1);
expect(stack).toHaveResource('AWS::Lambda::Function', {
Handler: 'index.handler',
});
});

it.each`
handler | entry
${null} | ${'index.ts'}
${'source/index.handler'} | ${'source/index.ts'}
${'main.func'} | ${'main.ts'}
${'a/b/c.h'} | ${'a/b/c.ts'}
`('Should be valid entry with default rootDir', ({ handler, entry }) => {
new NodejsFunction(new Stack(), 'lambda-function', { handler });
expect(buildSync).toHaveBeenCalledWith(expect.objectContaining({ entryPoints: [entry] }));
});

it('Should be valid outdir with custom rootDir', () => {
new NodejsFunction(new Stack(), 'lambda-function', { rootDir: path.join(__dirname, '../src') });
expect(buildSync).toHaveBeenCalledWith(expect.objectContaining({ outdir: path.join(__dirname, '../src', '.build') }));
});

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

afterAll(() => {
mockfs.restore();
describe('with invalid folder structure', () => {
beforeAll(() => {
mockfs();
});

it('Should be thrown due to unrecognised root directory', () => {
const constructor = () => new NodejsFunction(new Stack(), 'lambda-function');
expect(constructor).toThrowError(/^Cannot find root directory./);
});

afterAll(() => {
mockfs.restore();
});
});
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"declaration": true,
"skipLibCheck": true,
"esModuleInterop": true,
"strictNullChecks": true,
"lib": ["ES2017"]
},
"exclude": [
Expand Down

0 comments on commit 7c8b29b

Please sign in to comment.