From 5d48d30c9cc44252b302d84f251d25758e6e0fe9 Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Thu, 10 Jun 2021 14:34:04 -0600 Subject: [PATCH 1/5] add unit test coverage to init lifecycle hook feature --- test/unit/utils/UserFunction.test.ts | 45 +++++++++++++++++++ test/unit/utils/function/InitAbsent.js | 3 ++ test/unit/utils/function/InitPresent.js | 23 ++++++++++ .../utils/function/InitThrowsTypeError.js | 7 +++ 4 files changed, 78 insertions(+) create mode 100644 test/unit/utils/UserFunction.test.ts create mode 100644 test/unit/utils/function/InitAbsent.js create mode 100644 test/unit/utils/function/InitPresent.js create mode 100644 test/unit/utils/function/InitThrowsTypeError.js diff --git a/test/unit/utils/UserFunction.test.ts b/test/unit/utils/UserFunction.test.ts new file mode 100644 index 0000000..5dd335c --- /dev/null +++ b/test/unit/utils/UserFunction.test.ts @@ -0,0 +1,45 @@ +/** 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 not fail 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.not.be.null; + }); + 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; + handlerFunc.should.not.be.null; + }); + 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.not.be.null; + }); +}); \ 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..1888fe0 --- /dev/null +++ b/test/unit/utils/function/InitAbsent.js @@ -0,0 +1,3 @@ +exports.handler = async (event, context) => { + return 'Function complete.'; +} diff --git a/test/unit/utils/function/InitPresent.js b/test/unit/utils/function/InitPresent.js new file mode 100644 index 0000000..248403f --- /dev/null +++ b/test/unit/utils/function/InitPresent.js @@ -0,0 +1,23 @@ +// console.log("******** enter the init block ********"); + +let resolved = "No"; + +function sleep(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)).then(() => {resolved = "Yes!"}); +} + +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 'Function complete.'; +} diff --git a/test/unit/utils/function/InitThrowsTypeError.js b/test/unit/utils/function/InitThrowsTypeError.js new file mode 100644 index 0000000..6510a17 --- /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 'Function complete.'; +} From 017be1fa90c6d63c89c8df11dd3817c69ad51101 Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Thu, 10 Jun 2021 14:48:00 -0600 Subject: [PATCH 2/5] improve unit tests --- test/unit/utils/UserFunction.test.ts | 11 +++++---- test/unit/utils/function/InitAbsent.js | 23 ++++++++++++++++++- test/unit/utils/function/InitPresent.js | 3 +-- .../utils/function/InitThrowsTypeError.js | 2 +- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/test/unit/utils/UserFunction.test.ts b/test/unit/utils/UserFunction.test.ts index 5dd335c..f51c7fb 100644 --- a/test/unit/utils/UserFunction.test.ts +++ b/test/unit/utils/UserFunction.test.ts @@ -3,10 +3,11 @@ "use strict"; require("should"); +import { HandlerNotFound } from "../../../src/Errors"; import { load } from "../../../src/utils/UserFunction"; describe("Invoking the load function", async() => { - it("should not fail when init hook function is present", async() => { + it("should resolve promise when init hook function is present", async() => { const handler = "InitPresent.handler" const appRoot = "test/unit/utils/function"; @@ -16,9 +17,9 @@ describe("Invoking the load function", async() => { )) as Function; handlerFunc.should.be.Function; - handlerFunc.should.not.be.null; + handlerFunc().should.be.true; }); - it("should not fail when init hook function is absent", async() => { + it("should not resolve the promise when init hook function is absent", async() => { const handler = "InitAbsent.handler" const appRoot = "test/unit/utils/function"; @@ -28,7 +29,7 @@ describe("Invoking the load function", async() => { )) as Function; handlerFunc.should.be.Function; - handlerFunc.should.not.be.null; + handlerFunc().should.be.false; }); it("should catch TypeError exception", async() => { const handler = "InitThrowsTypeError.handler" @@ -40,6 +41,6 @@ describe("Invoking the load function", async() => { )) as Function; handlerFunc.should.be.Function; - handlerFunc.should.not.be.null; + 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 index 1888fe0..ecba193 100644 --- a/test/unit/utils/function/InitAbsent.js +++ b/test/unit/utils/function/InitAbsent.js @@ -1,3 +1,24 @@ +// console.log("******** enter the init block ********"); + +let resolved = "No"; + +function sleep(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)).then(() => {resolved = "Yes!"}); +} + +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) => { - return 'Function complete.'; + // 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 index 248403f..0fc4074 100644 --- a/test/unit/utils/function/InitPresent.js +++ b/test/unit/utils/function/InitPresent.js @@ -18,6 +18,5 @@ exports.initializeFunction = async () => { exports.handler = async (event, context) => { // console.log("******** enter the handler ********"); // console.log("******** Is promised resolved? " + resolved + " ********"); - - return 'Function complete.'; + return ( resolved ? true: false ); } diff --git a/test/unit/utils/function/InitThrowsTypeError.js b/test/unit/utils/function/InitThrowsTypeError.js index 6510a17..0ecd925 100644 --- a/test/unit/utils/function/InitThrowsTypeError.js +++ b/test/unit/utils/function/InitThrowsTypeError.js @@ -3,5 +3,5 @@ exports.initializeFunction = async () => { } exports.handler = async (event, context) => { - return 'Function complete.'; + return true; } From 867344a49c6bc2f685c97fa1af9d91f9d2d62272 Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Thu, 10 Jun 2021 14:51:42 -0600 Subject: [PATCH 3/5] fix unit test: load function should not fail when init hook function is absent --- test/unit/utils/UserFunction.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/unit/utils/UserFunction.test.ts b/test/unit/utils/UserFunction.test.ts index f51c7fb..3024993 100644 --- a/test/unit/utils/UserFunction.test.ts +++ b/test/unit/utils/UserFunction.test.ts @@ -19,7 +19,7 @@ describe("Invoking the load function", async() => { handlerFunc.should.be.Function; handlerFunc().should.be.true; }); - it("should not resolve the promise when init hook function is absent", async() => { + it("should not fail when init hook function is absent", async() => { const handler = "InitAbsent.handler" const appRoot = "test/unit/utils/function"; @@ -29,7 +29,6 @@ describe("Invoking the load function", async() => { )) as Function; handlerFunc.should.be.Function; - handlerFunc().should.be.false; }); it("should catch TypeError exception", async() => { const handler = "InitThrowsTypeError.handler" From 683ba29af1c8505dba57436c92ecf0fdcc5ff4b0 Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Thu, 10 Jun 2021 14:54:14 -0600 Subject: [PATCH 4/5] remove import --- test/unit/utils/UserFunction.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unit/utils/UserFunction.test.ts b/test/unit/utils/UserFunction.test.ts index 3024993..aedd9a0 100644 --- a/test/unit/utils/UserFunction.test.ts +++ b/test/unit/utils/UserFunction.test.ts @@ -3,7 +3,6 @@ "use strict"; require("should"); -import { HandlerNotFound } from "../../../src/Errors"; import { load } from "../../../src/utils/UserFunction"; describe("Invoking the load function", async() => { From 34e5836564d5f27502d49763b0452cb9492dec6a Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Thu, 10 Jun 2021 15:01:29 -0600 Subject: [PATCH 5/5] bugfix boolean --- test/unit/utils/function/InitAbsent.js | 4 ++-- test/unit/utils/function/InitPresent.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/unit/utils/function/InitAbsent.js b/test/unit/utils/function/InitAbsent.js index ecba193..d585cb5 100644 --- a/test/unit/utils/function/InitAbsent.js +++ b/test/unit/utils/function/InitAbsent.js @@ -1,9 +1,9 @@ // console.log("******** enter the init block ********"); -let resolved = "No"; +let resolved = false; function sleep(ms) { - return new Promise((resolve) => setTimeout(resolve, ms)).then(() => {resolved = "Yes!"}); + return new Promise((resolve) => setTimeout(resolve, ms)).then(() => {resolved = true}); } async function init() { diff --git a/test/unit/utils/function/InitPresent.js b/test/unit/utils/function/InitPresent.js index 0fc4074..6336607 100644 --- a/test/unit/utils/function/InitPresent.js +++ b/test/unit/utils/function/InitPresent.js @@ -1,9 +1,9 @@ // console.log("******** enter the init block ********"); -let resolved = "No"; +let resolved = false; function sleep(ms) { - return new Promise((resolve) => setTimeout(resolve, ms)).then(() => {resolved = "Yes!"}); + return new Promise((resolve) => setTimeout(resolve, ms)).then(() => {resolved = true}); } exports.initializeFunction = async () => {