Comparing version 0.7.4 to 0.8.0
{ | ||
"name": "middy", | ||
"version": "0.7.4", | ||
"version": "0.8.0", | ||
"description": "The simple (but cool 😎) middleware engine for AWS lambda in Node.js", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -5,29 +5,121 @@ const middy = require('../../middy') | ||
describe('🥃 Do Not Wait For Empty Event Loop', () => { | ||
test(`It should set context's callbackWaitsForEmptyEventLoop property to false`, () => { | ||
const handler = middy((event, context, cb) => { | ||
cb() | ||
describe('👌 With Default Options', () => { | ||
test(`It should set callbackWaitsForEmptyEventLoop to false by default`, () => { | ||
const handler = middy((event, context, cb) => { | ||
cb() | ||
}) | ||
handler.use(doNotWaitForEmptyEventLoop()) | ||
const event = {} | ||
const context = {} | ||
handler(event, context, () => { | ||
expect(context.callbackWaitsForEmptyEventLoop).toEqual(false) | ||
}) | ||
}) | ||
handler.use(doNotWaitForEmptyEventLoop()) | ||
const event = {} | ||
const context = {} | ||
handler(event, context, (_, response) => { | ||
expect(context.callbackWaitsForEmptyEventLoop).toEqual(false) | ||
test(`callbackWaitsForEmptyEventLoop should remain true if was overridden by user in handler`, () => { | ||
const handler = middy((event, context, cb) => { | ||
context.callbackWaitsForEmptyEventLoop = true | ||
cb() | ||
}) | ||
handler.use(doNotWaitForEmptyEventLoop()) | ||
const event = {} | ||
const context = {} | ||
handler(event, context, () => { | ||
expect(context.callbackWaitsForEmptyEventLoop).toEqual(true) | ||
}) | ||
}) | ||
test(`callbackWaitsForEmptyEventLoop should stay false if handler has error`, () => { | ||
const handler = middy((event, context, cb) => { | ||
cb(new Error('!')) | ||
}) | ||
handler.use(doNotWaitForEmptyEventLoop()) | ||
const event = {} | ||
const context = {} | ||
handler(event, context, () => { | ||
expect(context.callbackWaitsForEmptyEventLoop).toEqual(false) | ||
}) | ||
}) | ||
}) | ||
test(`context.callbackWaitsForEmptyEventLoop should be false even if it's set to true before`, () => { | ||
const handler = middy((event, context, cb) => { | ||
context.callbackWaitsForEmptyEventLoop = true | ||
cb() | ||
describe('✍️ With Overridden Options', () => { | ||
test(`callbackWaitsForEmptyEventLoop should be false when runOnAfter is true in options`, () => { | ||
const handler = middy((event, context, cb) => { | ||
context.callbackWaitsForEmptyEventLoop = true | ||
cb() | ||
}) | ||
handler.use(doNotWaitForEmptyEventLoop({ | ||
runOnAfter: true | ||
})) | ||
const event = {} | ||
const context = {} | ||
handler(event, context, () => { | ||
expect(context.callbackWaitsForEmptyEventLoop).toEqual(false) | ||
}) | ||
}) | ||
handler.use(doNotWaitForEmptyEventLoop()) | ||
test(`callbackWaitsForEmptyEventLoop should remain true when error occurs even if runOnAfter is true`, () => { | ||
const handler = middy((event, context, cb) => { | ||
context.callbackWaitsForEmptyEventLoop = true | ||
cb(new Error('!')) | ||
}) | ||
const event = {} | ||
const context = {} | ||
handler(event, context, (_, response) => { | ||
expect(context.callbackWaitsForEmptyEventLoop).toEqual(false) | ||
handler.use(doNotWaitForEmptyEventLoop({ | ||
runOnAfter: true | ||
})) | ||
const event = {} | ||
const context = {} | ||
handler(event, context, () => { | ||
expect(context.callbackWaitsForEmptyEventLoop).toEqual(true) | ||
}) | ||
}) | ||
test(`callbackWaitsForEmptyEventLoop should be false when error occurs but runOnError is true`, () => { | ||
const handler = middy((event, context, cb) => { | ||
context.callbackWaitsForEmptyEventLoop = true | ||
cb(new Error('!')) | ||
}) | ||
handler.use(doNotWaitForEmptyEventLoop({ | ||
runOnAfter: true, | ||
runOnError: true | ||
})) | ||
const event = {} | ||
const context = {} | ||
handler(event, context, () => { | ||
expect(context.callbackWaitsForEmptyEventLoop).toEqual(false) | ||
}) | ||
}) | ||
test(`callbackWaitsForEmptyEventLoop should be false in handler but true after if set by options`, () => { | ||
expect.assertions(2) | ||
const handler = middy((event, context, cb) => { | ||
expect(context.callbackWaitsForEmptyEventLoop).toEqual(true) | ||
cb() | ||
}) | ||
handler.use(doNotWaitForEmptyEventLoop({ | ||
runOnBefore: false, | ||
runOnAfter: true | ||
})) | ||
const event = {} | ||
const context = { | ||
callbackWaitsForEmptyEventLoop: true | ||
} | ||
handler(event, context, () => { | ||
expect(context.callbackWaitsForEmptyEventLoop).toEqual(false) | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,6 +0,20 @@ | ||
module.exports = () => ({ | ||
after: (handler, next) => { | ||
module.exports = (opts) => { | ||
const defaults = { | ||
runOnBefore: true, | ||
runOnAfter: false, | ||
runOnError: false | ||
} | ||
const options = Object.assign({}, defaults, opts) | ||
const disableEmptyEventLoopWait = (handler, next) => { | ||
handler.context.callbackWaitsForEmptyEventLoop = false | ||
next() | ||
} | ||
}) | ||
return ({ | ||
before: options.runOnBefore ? disableEmptyEventLoopWait : undefined, | ||
after: options.runOnAfter ? disableEmptyEventLoopWait : undefined, | ||
onError: options.runOnError ? disableEmptyEventLoopWait : undefined | ||
}) | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
82461
1791