From 514dfc8fc7fb59ee05023f72edaa501a717bd67b Mon Sep 17 00:00:00 2001 From: sinan-guclu-pupil <39489644+sinan-guclu-pupil@users.noreply.github.com> Date: Thu, 15 Aug 2019 14:36:41 +0100 Subject: [PATCH 1/3] Clears timeout on calling done() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prevents: ``` est has detected the following 1 open handle potentially keeping Jest from exiting: ● Timeout 74 | 75 | floorplan = Property.floorplans.find(({type}) => type === 'MANUAL'); > 76 | mockContext = AWSMockLambdaContext({ | ^ 77 | timeout: 3 /* 5 minutes */ 78 | }); 79 | at Object..module.exports.options [as default] (node_modules/aws-lambda-mock-context/index.js:66:2) ``` from Jest. --- index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 8624a89..4f92485 100644 --- a/index.js +++ b/index.js @@ -21,7 +21,7 @@ module.exports = options => { const start = Date.now(); let end; - + let timeout; const context = { callbackWaitsForEmptyEventLoop: true, functionName: opts.functionName, @@ -53,6 +53,9 @@ module.exports = options => { deferred.reject(err); }, done: (err, result) => { + if (timeout) { + clearTimeout(timeout); + } if (err) { context.fail(err); return; @@ -63,7 +66,7 @@ module.exports = options => { Promise: new Promise(deferred) }; - setTimeout(() => { + timeout = setTimeout(() => { if (context.getRemainingTimeInMillis() === 0) { context.fail(new Error(`Task timed out after ${opts.timeout}.00 seconds`)); } From dedc26d7a2449123209dfd67cb44631b32da7ac5 Mon Sep 17 00:00:00 2001 From: sinan-guclu-pupil <39489644+sinan-guclu-pupil@users.noreply.github.com> Date: Thu, 15 Aug 2019 14:56:20 +0100 Subject: [PATCH 2/3] Addresses CI errors --- index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 4f92485..0595d6e 100644 --- a/index.js +++ b/index.js @@ -21,7 +21,13 @@ module.exports = options => { const start = Date.now(); let end; - let timeout; + + const timeout = setTimeout(() => { + if (context.getRemainingTimeInMillis() === 0) { + context.fail(new Error(`Task timed out after ${opts.timeout}.00 seconds`)); + } + }, opts.timeout * 1000); + const context = { callbackWaitsForEmptyEventLoop: true, functionName: opts.functionName, @@ -66,11 +72,5 @@ module.exports = options => { Promise: new Promise(deferred) }; - timeout = setTimeout(() => { - if (context.getRemainingTimeInMillis() === 0) { - context.fail(new Error(`Task timed out after ${opts.timeout}.00 seconds`)); - } - }, opts.timeout * 1000); - return context; }; From b7bd42cda6743ea56871bd12c503e91664e47539 Mon Sep 17 00:00:00 2001 From: sinan-guclu-pupil <39489644+sinan-guclu-pupil@users.noreply.github.com> Date: Thu, 15 Aug 2019 15:14:39 +0100 Subject: [PATCH 3/3] Lint errors --- index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 0595d6e..e7542c1 100644 --- a/index.js +++ b/index.js @@ -21,13 +21,7 @@ module.exports = options => { const start = Date.now(); let end; - - const timeout = setTimeout(() => { - if (context.getRemainingTimeInMillis() === 0) { - context.fail(new Error(`Task timed out after ${opts.timeout}.00 seconds`)); - } - }, opts.timeout * 1000); - + let timeout = null; const context = { callbackWaitsForEmptyEventLoop: true, functionName: opts.functionName, @@ -72,5 +66,11 @@ module.exports = options => { Promise: new Promise(deferred) }; + timeout = setTimeout(() => { + if (context.getRemainingTimeInMillis() === 0) { + context.fail(new Error(`Task timed out after ${opts.timeout}.00 seconds`)); + } + }, opts.timeout * 1000); + return context; };