Skip to content

Commit

Permalink
update aws-lambda tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr committed Sep 11, 2024
1 parent 1194b87 commit bb07496
Show file tree
Hide file tree
Showing 3 changed files with 616 additions and 479 deletions.
58 changes: 58 additions & 0 deletions test/lib/temp-override-uncaught.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

const EXCEPTION = 'uncaughtException'
const REJECTION = 'unhandledRejection'

module.exports = tempOverrideUncaught

const oldListeners = {
EXCEPTION: [],
REJECTION: []
}

/**
* Temporarily removes all listeners for the target exception handler,
* either `uncaughtException` (default) or `unhandledRejection`, subsequently
* restoring the original listeners upon test completion.
*
* @param {object} params
* @param {TestContext} t A `node:test` context object.
* @param {function} handler An error handler function that will replace all
* current listeners.
* @param {string} [type='uncaughtException'] The kind of uncaught event to
* override.
* @property {string} EXCEPTION Constant value usable for `type`.
* @property {string} REJECTION Constant value usable for `type`.
*/
function tempOverrideUncaught({ t, handler, type = EXCEPTION }) {
if (!handler) {
handler = function uncaughtTestHandler() {
t.diagnostic('uncaught handler not defined')
}
}

oldListeners[type] = process.listeners(type)
process.removeAllListeners(type)
process.once(type, (error) => {
handler(error)
})

// We probably shouldn't be adding a `t.after` in this helper. There can only
// be one `t.after` handler per test, and putting in here obscures the fact
// that it has been added.
t.after(() => {
for (const l of oldListeners[type]) {
process.on(type, l)
}
})
}

Object.defineProperties(tempOverrideUncaught, {
EXCEPTION: { value: EXCEPTION },
REJECTION: { value: REJECTION }
})
36 changes: 36 additions & 0 deletions test/lib/temp-remove-listeners.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

/**
* Temporarily removes all event listeners on an emitter for a specific event
* and re-adds them subsequent to a test completing.
*
* @param {object} params
* @param {TestContext} t A `node:test` test context.
* @param {EventEmitter} emitter The emitter to manipulate.
* @param {string} event The event name to target.
*/
module.exports = function tempRemoveListeners({ t, emitter, event }) {
if (!emitter) {
t.diagnostic(`Not removing ${event} listeners, emitter does not exist`)
return
}

t.diagnostic(`Removing listeners for ${event}`)
const listeners = emitter.listeners(event)
emitter.removeAllListeners(event)

// We probably shouldn't be adding a `t.after` in this helper. There can only
// be one `t.after` handler per test, and putting in here obscures the fact
// that it has been added.
t.after(() => {
t.diagnostic(`Re-adding listeners for ${event}`)
for (const l of listeners) {
emitter.on(event, l)
}
})
}
Loading

0 comments on commit bb07496

Please sign in to comment.