-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add unit test coverage to init lifecycle hook feature
- Loading branch information
Showing
5 changed files
with
104 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |