From 7c8b29b92cc0feb41f094de8f2a93e6c1728e42b Mon Sep 17 00:00:00 2001 From: Victor Korzunin Date: Sun, 25 Oct 2020 17:23:29 +0100 Subject: [PATCH] test: cover conditions with unit tests --- src/utils.ts | 8 ++++- tests/index.test.ts | 82 ++++++++++++++++++++++++++++++++++++--------- tsconfig.json | 1 + 3 files changed, 75 insertions(+), 16 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 89f6253..abfc319 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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); @@ -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') diff --git a/tests/index.test.ts b/tests/index.test.ts index 8895d95..9537d6f 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -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(); + }); }); }); diff --git a/tsconfig.json b/tsconfig.json index c83ec13..b28c81e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,6 +6,7 @@ "declaration": true, "skipLibCheck": true, "esModuleInterop": true, + "strictNullChecks": true, "lib": ["ES2017"] }, "exclude": [