From 960e8272ee65c060deb4c4e74d92cd7eb292d945 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/9] 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 ef9081f5f716c5d509b3f555d43f8827d2d54f3c 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/9] 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 d283bab4dff96ef594d37db020f2af7c9000c302 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/9] 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; }; From f469734333ffe6aa3122af187103cb3ca09cc370 Mon Sep 17 00:00:00 2001 From: Dominique Rau Date: Tue, 6 Aug 2019 13:40:10 +0200 Subject: [PATCH 4/9] chore(typings): add promise to typings --- index.d.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 2817d9c..5ac0142 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,4 @@ -import {Context} from 'aws-lambda'; +import { Context } from 'aws-lambda'; interface ContextOptions { region?: string; @@ -10,8 +10,12 @@ interface ContextOptions { timeout?: number; } +interface MockContext extends Context { + Promise: Promise; +} + declare var mockContext: { - (options?: ContextOptions): Context; + (options?: ContextOptions): MockContext; }; export = mockContext; From 15e081ae8281299127a5f47a8756946c58e195ff Mon Sep 17 00:00:00 2001 From: Liam Don Date: Fri, 27 Mar 2020 19:57:09 -0700 Subject: [PATCH 5/9] Upgrade packages and usage, fix for new lint rules --- .travis.yml | 3 +-- index.d.ts | 8 ++++---- index.js | 25 +++++++++++++------------ package.json | 18 ++++++++++++------ test.js | 22 +++++++++++----------- 5 files changed, 41 insertions(+), 35 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0a5ead6..f98fed0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: node_js node_js: + - '12' - '10' - '8' - - '6' - - '4' diff --git a/index.d.ts b/index.d.ts index 5ac0142..51070ed 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,4 @@ -import { Context } from 'aws-lambda'; +import {Context} from 'aws-lambda'; interface ContextOptions { region?: string; @@ -14,8 +14,8 @@ interface MockContext extends Context { Promise: Promise; } -declare var mockContext: { - (options?: ContextOptions): MockContext; -}; +type MockInitialize = (options?: ContextOptions) => MockContext; + +declare const mockContext: MockInitialize; export = mockContext; diff --git a/index.js b/index.js index e7542c1..7d5eafb 100644 --- a/index.js +++ b/index.js @@ -4,18 +4,18 @@ const moment = require('moment'); const defer = require('pinkie-defer'); const pkg = require('./package.json'); -module.exports = options => { +module.exports = userOptions => { const id = uuid.v1(); const stream = uuid.v4().replace(/-/g, ''); - const opts = Object.assign({ + const options = Object.assign({ region: 'us-west-1', account: '123456789012', functionName: pkg.name, functionVersion: '$LATEST', memoryLimitInMB: '128', timeout: 3 - }, options); + }, userOptions); const deferred = defer(); @@ -24,17 +24,17 @@ module.exports = options => { let timeout = null; const context = { callbackWaitsForEmptyEventLoop: true, - functionName: opts.functionName, - functionVersion: opts.functionVersion, - invokedFunctionArn: `arn:aws:lambda:${opts.region}:${opts.account}:function:${opts.functionName}:${opts.alias || opts.functionVersion}`, - memoryLimitInMB: opts.memoryLimitInMB, + functionName: options.functionName, + functionVersion: options.functionVersion, + invokedFunctionArn: `arn:aws:lambda:${options.region}:${options.account}:function:${options.functionName}:${options.alias || options.functionVersion}`, + memoryLimitInMB: options.memoryLimitInMB, awsRequestId: id, invokeid: id, - logGroupName: `/aws/lambda/${opts.functionName}`, - logStreamName: `${moment().format('YYYY/MM/DD')}/[${opts.functionVersion}]/${stream}`, + logGroupName: `/aws/lambda/${options.functionName}`, + logStreamName: `${moment().format('YYYY/MM/DD')}/[${options.functionVersion}]/${stream}`, getRemainingTimeInMillis: () => { const endTime = end || Date.now(); - const remainingTime = (opts.timeout * 1000) - (endTime - start); + const remainingTime = (options.timeout * 1000) - (endTime - start); return Math.max(0, remainingTime); }, @@ -56,6 +56,7 @@ module.exports = options => { if (timeout) { clearTimeout(timeout); } + if (err) { context.fail(err); return; @@ -68,9 +69,9 @@ module.exports = options => { timeout = setTimeout(() => { if (context.getRemainingTimeInMillis() === 0) { - context.fail(new Error(`Task timed out after ${opts.timeout}.00 seconds`)); + context.fail(new Error(`Task timed out after ${options.timeout}.00 seconds`)); } - }, opts.timeout * 1000); + }, options.timeout * 1000); return context; }; diff --git a/package.json b/package.json index 9037f61..d20fb61 100644 --- a/package.json +++ b/package.json @@ -9,11 +9,16 @@ "url": "https://github.com/SamVerschueren" }, "engines": { - "node": ">=4" + "node": ">=7.6" }, "scripts": { "test": "xo && ava" }, + "ava": { + "require": [ + "esm" + ] + }, "files": [ "index.js", "index.d.ts" @@ -34,11 +39,12 @@ "uuid": "^3.0.1" }, "devDependencies": { - "@types/aws-lambda": "^8.10.7", - "ava": "*", - "delay": "^2.0.0", - "in-range": "^1.0.0", - "xo": "^0.20.3" + "@types/aws-lambda": "^8.10.46", + "ava": "^3.5.1", + "delay": "^4.3.0", + "esm": "^3.2.25", + "in-range": "^2.0.0", + "xo": "^0.28.1" }, "types": "index.d.ts" } diff --git a/test.js b/test.js index 5dc2761..a9762be 100644 --- a/test.js +++ b/test.js @@ -3,14 +3,14 @@ import delay from 'delay'; import inRange from 'in-range'; import m from '.'; -function invokeAsync(method, result, opts) { - opts = Object.assign({ +function invokeAsync(method, result, options) { + options = Object.assign({ ms: 500, timeout: 3 - }, opts); + }, options); const ctx = m({ - timeout: opts.timeout + timeout: options.timeout }); setTimeout(() => { @@ -20,7 +20,7 @@ function invokeAsync(method, result, opts) { } ctx[method](result); - }, opts.ms); + }, options.ms); return ctx.Promise; } @@ -31,9 +31,9 @@ test('succeed', async t => { }); test('fail', async t => { - await t.throws(invokeAsync('fail', 'promise fail'), 'promise fail'); - await t.throws(invokeAsync('fail', new Error('promise fail')), 'promise fail'); - await t.throws(invokeAsync('done', new Error('promise fail')), 'promise fail'); + await t.throwsAsync(invokeAsync('fail', 'promise fail'), null, 'promise fail'); + await t.throwsAsync(invokeAsync('fail', new Error('promise fail')), null, 'promise fail'); + await t.throwsAsync(invokeAsync('done', new Error('promise fail')), null, 'promise fail'); }); test('result', t => { @@ -74,7 +74,7 @@ test('remaining time', async t => { const ms = ctx.getRemainingTimeInMillis(); - t.true(inRange(ctx.getRemainingTimeInMillis(), 1950, 2050)); + t.true(inRange(ctx.getRemainingTimeInMillis(), {start: 1950, end: 2050})); await delay(10); @@ -96,10 +96,10 @@ test('set function timeout', async t => { await delay(1000); - t.true(inRange(ctx.getRemainingTimeInMillis(), 8950, 9050)); + t.true(inRange(ctx.getRemainingTimeInMillis(), {start: 8950, end: 9050})); ctx.succeed(); }); test('timeout throws error', async t => { - await t.throws(invokeAsync('succeed', 'foo', {ms: 2000, timeout: 1}), 'Task timed out after 1.00 seconds'); + await t.throwsAsync(invokeAsync('succeed', 'foo', {ms: 2000, timeout: 1}), null, 'Task timed out after 1.00 seconds'); }); From a6430181c1ae722c6786e71970482223c37be422 Mon Sep 17 00:00:00 2001 From: Liam Don Date: Fri, 27 Mar 2020 20:47:35 -0700 Subject: [PATCH 6/9] Support only node LTS releases --- .travis.yml | 5 ++--- index.js | 7 ++++--- package.json | 2 +- test.js | 7 ++++--- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index f98fed0..59e3686 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,4 @@ language: node_js node_js: - - '12' - - '10' - - '8' + - 'lts/erbium' + - 'lts/dubnium' diff --git a/index.js b/index.js index 7d5eafb..978e3d5 100644 --- a/index.js +++ b/index.js @@ -8,14 +8,15 @@ module.exports = userOptions => { const id = uuid.v1(); const stream = uuid.v4().replace(/-/g, ''); - const options = Object.assign({ + const options = { region: 'us-west-1', account: '123456789012', functionName: pkg.name, functionVersion: '$LATEST', memoryLimitInMB: '128', - timeout: 3 - }, userOptions); + timeout: 3, + ...userOptions + }; const deferred = defer(); diff --git a/package.json b/package.json index d20fb61..9bac706 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "url": "https://github.com/SamVerschueren" }, "engines": { - "node": ">=7.6" + "node": ">=10.18.0" }, "scripts": { "test": "xo && ava" diff --git a/test.js b/test.js index a9762be..3e6d8eb 100644 --- a/test.js +++ b/test.js @@ -4,10 +4,11 @@ import inRange from 'in-range'; import m from '.'; function invokeAsync(method, result, options) { - options = Object.assign({ + options = { ms: 500, - timeout: 3 - }, options); + timeout: 3, + ...options + }; const ctx = m({ timeout: options.timeout From 1444328be83aab0c5af9d2f3c7b9d77f09b7d089 Mon Sep 17 00:00:00 2001 From: Liam Don Date: Fri, 27 Mar 2020 20:50:01 -0700 Subject: [PATCH 7/9] Bump to major package version due to support change --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9bac706..9dc5666 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aws-lambda-mock-context", - "version": "3.2.1", + "version": "4.0.0", "description": "AWS Lambda mock context object", "license": "MIT", "author": { From 2115c76e6d424f423a96f69369bd05c2930292ea Mon Sep 17 00:00:00 2001 From: Liam Don Date: Fri, 27 Mar 2020 21:15:24 -0700 Subject: [PATCH 8/9] Update more modules and syntax --- index.js | 23 ++++++++++++++--------- package.json | 4 +--- test.js | 4 ++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 978e3d5..b864087 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,11 @@ 'use strict'; -const uuid = require('uuid'); -const moment = require('moment'); -const defer = require('pinkie-defer'); -const pkg = require('./package.json'); +import {v1 as uuidv1, v4 as uuidv4} from 'uuid'; +import defer from 'pinkie-defer'; +import pkg from './package.json'; -module.exports = userOptions => { - const id = uuid.v1(); - const stream = uuid.v4().replace(/-/g, ''); +export default userOptions => { + const id = uuidv1(); + const stream = uuidv4().replace(/-/g, ''); const options = { region: 'us-west-1', @@ -20,7 +19,13 @@ module.exports = userOptions => { const deferred = defer(); - const start = Date.now(); + const d = new Date(); + const logDateString = [ + d.getFullYear(), + ('0' + (d.getMonth() + 1)).slice(-2), + ('0' + d.getDate()).slice(-2) + ].join('/'); + const start = d.getTime(); let end; let timeout = null; const context = { @@ -32,7 +37,7 @@ module.exports = userOptions => { awsRequestId: id, invokeid: id, logGroupName: `/aws/lambda/${options.functionName}`, - logStreamName: `${moment().format('YYYY/MM/DD')}/[${options.functionVersion}]/${stream}`, + logStreamName: `${logDateString}/[${options.functionVersion}]/${stream}`, getRemainingTimeInMillis: () => { const endTime = end || Date.now(); const remainingTime = (options.timeout * 1000) - (endTime - start); diff --git a/package.json b/package.json index 9dc5666..1a5859e 100644 --- a/package.json +++ b/package.json @@ -34,15 +34,13 @@ "promise" ], "dependencies": { - "moment": "^2.10.5", "pinkie-defer": "^1.0.0", - "uuid": "^3.0.1" + "uuid": "^7.0.2" }, "devDependencies": { "@types/aws-lambda": "^8.10.46", "ava": "^3.5.1", "delay": "^4.3.0", - "esm": "^3.2.25", "in-range": "^2.0.0", "xo": "^0.28.1" }, diff --git a/test.js b/test.js index 3e6d8eb..c2ae1c3 100644 --- a/test.js +++ b/test.js @@ -3,7 +3,7 @@ import delay from 'delay'; import inRange from 'in-range'; import m from '.'; -function invokeAsync(method, result, options) { +const invokeAsync = (method, result, options) => { options = { ms: 500, timeout: 3, @@ -24,7 +24,7 @@ function invokeAsync(method, result, options) { }, options.ms); return ctx.Promise; -} +}; test('succeed', async t => { t.is(await invokeAsync('succeed', 'baz'), 'baz'); From 192d230ffcb196411806e60eac2855cf9c8c8e32 Mon Sep 17 00:00:00 2001 From: Liam Don Date: Sat, 4 Apr 2020 17:49:00 -0700 Subject: [PATCH 9/9] Use regular require instead of import --- index.js | 12 +++++++----- package.json | 2 +- test.js | 8 ++++---- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index b864087..718aba3 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,9 @@ -'use strict'; -import {v1 as uuidv1, v4 as uuidv4} from 'uuid'; -import defer from 'pinkie-defer'; -import pkg from './package.json'; +const uuidv1 = require('uuid/v1'); +const uuidv4 = require('uuid/v4'); +const defer = require('pinkie-defer'); +const pkg = require('./package.json'); -export default userOptions => { +const mockContext = userOptions => { const id = uuidv1(); const stream = uuidv4().replace(/-/g, ''); @@ -81,3 +81,5 @@ export default userOptions => { return context; }; + +module.exports = mockContext; diff --git a/package.json b/package.json index 1a5859e..f8ee09b 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ ], "dependencies": { "pinkie-defer": "^1.0.0", - "uuid": "^7.0.2" + "uuid": "3.4.0" }, "devDependencies": { "@types/aws-lambda": "^8.10.46", diff --git a/test.js b/test.js index c2ae1c3..babebbd 100644 --- a/test.js +++ b/test.js @@ -1,7 +1,7 @@ -import test from 'ava'; -import delay from 'delay'; -import inRange from 'in-range'; -import m from '.'; +const test = require('ava'); +const delay = require('delay'); +const inRange = require('in-range'); +const m = require('.'); const invokeAsync = (method, result, options) => { options = {