Skip to content

Commit ad6a77d

Browse files
committed
add unit test coverage to init lifecycle hook feature
1 parent a4596cd commit ad6a77d

File tree

5 files changed

+104
-8
lines changed

5 files changed

+104
-8
lines changed

src/utils/UserFunction.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,14 @@ function _loadUserApp(
112112

113113
async function _initializeFunction(userApp: any): Promise<void> {
114114
try {
115-
await userApp.initializeFunction();
115+
await userApp.initializeFunction();
116116
} catch (e) {
117-
if (e instanceof TypeError) {
118-
// initializeFunction lifecycle hook not implemented
119-
return;
120-
}
121-
else {
122-
throw e;
123-
}
117+
if (e instanceof TypeError) {
118+
// initializeFunction lifecycle hook not implemented
119+
return;
120+
} else {
121+
throw e;
122+
}
124123
}
125124
}
126125

test/unit/utils/UserFunction.test.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/** Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. */
2+
3+
"use strict";
4+
5+
require("should");
6+
import { load } from "../../../src/utils/UserFunction";
7+
8+
describe("Invoking the load function", async() => {
9+
it("should resolve promise when init hook function is present", async() => {
10+
const handler = "InitPresent.handler"
11+
const appRoot = "test/unit/utils/function";
12+
13+
const handlerFunc = (await load(
14+
appRoot,
15+
handler
16+
)) as Function;
17+
18+
handlerFunc.should.be.Function;
19+
handlerFunc().should.be.true;
20+
});
21+
it("should not fail when init hook function is absent", async() => {
22+
const handler = "InitAbsent.handler"
23+
const appRoot = "test/unit/utils/function";
24+
25+
const handlerFunc = (await load(
26+
appRoot,
27+
handler
28+
)) as Function;
29+
30+
handlerFunc.should.be.Function;
31+
});
32+
it("should catch TypeError exception", async() => {
33+
const handler = "InitThrowsTypeError.handler"
34+
const appRoot = "test/unit/utils/function";
35+
36+
const handlerFunc = (await load(
37+
appRoot,
38+
handler
39+
)) as Function;
40+
41+
handlerFunc.should.be.Function;
42+
handlerFunc().should.be.true;
43+
});
44+
});
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// console.log("******** enter the init block ********");
2+
3+
let resolved = false;
4+
5+
function sleep(ms) {
6+
return new Promise((resolve) => setTimeout(resolve, ms)).then(() => {resolved = true});
7+
}
8+
9+
async function init() {
10+
// console.log("******** enter initializeFunction hook ********");
11+
// console.log("******** Is promised resolved? " + resolved + " ********");
12+
// console.log("******** sleep for 20 ms... ********")
13+
let p = await sleep(20);
14+
// console.log("******** wake up ********");
15+
// console.log("******** Is promised resolved? " + resolved + " ********");
16+
}
17+
18+
init();
19+
20+
exports.handler = async (event, context) => {
21+
// console.log("******** enter the handler ********");
22+
// console.log("******** Is promised resolved? " + resolved + " ********");
23+
return ( resolved ? true: false );
24+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// console.log("******** enter the init block ********");
2+
3+
let resolved = false;
4+
5+
function sleep(ms) {
6+
return new Promise((resolve) => setTimeout(resolve, ms)).then(() => {resolved = true});
7+
}
8+
9+
exports.initializeFunction = async () => {
10+
// console.log("******** enter initializeFunction hook ********");
11+
// console.log("******** Is promised resolved? " + resolved + " ********");
12+
// console.log("******** sleep for 20 ms... ********")
13+
let p = await sleep(20);
14+
// console.log("******** wake up ********");
15+
// console.log("******** Is promised resolved? " + resolved + " ********");
16+
}
17+
18+
exports.handler = async (event, context) => {
19+
// console.log("******** enter the handler ********");
20+
// console.log("******** Is promised resolved? " + resolved + " ********");
21+
return ( resolved ? true: false );
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
exports.initializeFunction = async () => {
2+
throw new TypeError;
3+
}
4+
5+
exports.handler = async (event, context) => {
6+
return true;
7+
}

0 commit comments

Comments
 (0)