-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Updated serverless unit tests to node:test (#2549)
- Loading branch information
1 parent
a30aed5
commit 619f23c
Showing
7 changed files
with
744 additions
and
602 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
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,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 } | ||
}) |
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,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) | ||
} | ||
}) | ||
} |
Oops, something went wrong.