diff --git a/src/utils/UserFunction.ts b/src/utils/UserFunction.ts index bf6d2d5..8a2e97c 100644 --- a/src/utils/UserFunction.ts +++ b/src/utils/UserFunction.ts @@ -112,15 +112,14 @@ function _loadUserApp( async function _initializeFunction(userApp: any): Promise { 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; + } } } diff --git a/test/unit/utils/UserFunction.test.ts b/test/unit/utils/UserFunction.test.ts new file mode 100644 index 0000000..aedd9a0 --- /dev/null +++ b/test/unit/utils/UserFunction.test.ts @@ -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; + }); +}); \ No newline at end of file diff --git a/test/unit/utils/function/InitAbsent.js b/test/unit/utils/function/InitAbsent.js new file mode 100644 index 0000000..d585cb5 --- /dev/null +++ b/test/unit/utils/function/InitAbsent.js @@ -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 ); +} diff --git a/test/unit/utils/function/InitPresent.js b/test/unit/utils/function/InitPresent.js new file mode 100644 index 0000000..6336607 --- /dev/null +++ b/test/unit/utils/function/InitPresent.js @@ -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 ); +} diff --git a/test/unit/utils/function/InitThrowsTypeError.js b/test/unit/utils/function/InitThrowsTypeError.js new file mode 100644 index 0000000..0ecd925 --- /dev/null +++ b/test/unit/utils/function/InitThrowsTypeError.js @@ -0,0 +1,7 @@ +exports.initializeFunction = async () => { + throw new TypeError; +} + +exports.handler = async (event, context) => { + return true; +}