Skip to content

Commit

Permalink
add unit test coverage to init lifecycle hook feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dancfox committed Jun 10, 2021
1 parent a4596cd commit ad6a77d
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/utils/UserFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,14 @@ function _loadUserApp(

async function _initializeFunction(userApp: any): Promise<void> {
try {
await userApp.initializeFunction();
await userApp.initializeFunction();
} catch (e) {
if (e instanceof TypeError) {
// initializeFunction lifecycle hook not implemented
return;
}
else {
throw e;
}
if (e instanceof TypeError) {
// initializeFunction lifecycle hook not implemented
return;
} else {
throw e;
}
}
}

Expand Down
44 changes: 44 additions & 0 deletions test/unit/utils/UserFunction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/** Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. */

"use strict";

require("should");
import { load } from "../../../src/utils/UserFunction";

describe("Invoking the load function", async() => {
it("should resolve promise when init hook function is present", async() => {
const handler = "InitPresent.handler"
const appRoot = "test/unit/utils/function";

const handlerFunc = (await load(
appRoot,
handler
)) as Function;

handlerFunc.should.be.Function;
handlerFunc().should.be.true;
});
it("should not fail when init hook function is absent", async() => {
const handler = "InitAbsent.handler"
const appRoot = "test/unit/utils/function";

const handlerFunc = (await load(
appRoot,
handler
)) as Function;

handlerFunc.should.be.Function;
});
it("should catch TypeError exception", async() => {
const handler = "InitThrowsTypeError.handler"
const appRoot = "test/unit/utils/function";

const handlerFunc = (await load(
appRoot,
handler
)) as Function;

handlerFunc.should.be.Function;
handlerFunc().should.be.true;
});
});
24 changes: 24 additions & 0 deletions test/unit/utils/function/InitAbsent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// console.log("******** enter the init block ********");

let resolved = false;

function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms)).then(() => {resolved = true});
}

async function init() {
// console.log("******** enter initializeFunction hook ********");
// console.log("******** Is promised resolved? " + resolved + " ********");
// console.log("******** sleep for 20 ms... ********")
let p = await sleep(20);
// console.log("******** wake up ********");
// console.log("******** Is promised resolved? " + resolved + " ********");
}

init();

exports.handler = async (event, context) => {
// console.log("******** enter the handler ********");
// console.log("******** Is promised resolved? " + resolved + " ********");
return ( resolved ? true: false );
}
22 changes: 22 additions & 0 deletions test/unit/utils/function/InitPresent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// console.log("******** enter the init block ********");

let resolved = false;

function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms)).then(() => {resolved = true});
}

exports.initializeFunction = async () => {
// console.log("******** enter initializeFunction hook ********");
// console.log("******** Is promised resolved? " + resolved + " ********");
// console.log("******** sleep for 20 ms... ********")
let p = await sleep(20);
// console.log("******** wake up ********");
// console.log("******** Is promised resolved? " + resolved + " ********");
}

exports.handler = async (event, context) => {
// console.log("******** enter the handler ********");
// console.log("******** Is promised resolved? " + resolved + " ********");
return ( resolved ? true: false );
}
7 changes: 7 additions & 0 deletions test/unit/utils/function/InitThrowsTypeError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
exports.initializeFunction = async () => {
throw new TypeError;
}

exports.handler = async (event, context) => {
return true;
}

0 comments on commit ad6a77d

Please sign in to comment.