Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Updated serverless unit tests to node:test #2549

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 34 additions & 28 deletions THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ code, the source code can be found at [https://github.com/newrelic/node-newrelic
* [@aws-sdk/client-s3](#aws-sdkclient-s3)
* [@aws-sdk/s3-request-presigner](#aws-sdks3-request-presigner)
* [@koa/router](#koarouter)
* [@matteo.collina/tspl](#matteocollinatspl)
* [@newrelic/eslint-config](#newreliceslint-config)
* [@newrelic/newrelic-oss-cli](#newrelicnewrelic-oss-cli)
* [@newrelic/test-utilities](#newrelictest-utilities)
Expand Down Expand Up @@ -69,7 +70,6 @@ code, the source code can be found at [https://github.com/newrelic/node-newrelic
* [nock](#nock)
* [proxy](#proxy)
* [proxyquire](#proxyquire)
* [rfdc](#rfdc)
* [rimraf](#rimraf)
* [self-cert](#self-cert)
* [should](#should)
Expand Down Expand Up @@ -1076,7 +1076,7 @@ SOFTWARE.

### @aws-sdk/client-s3

This product includes source derived from [@aws-sdk/client-s3](https://github.com/aws/aws-sdk-js-v3) ([v3.637.0](https://github.com/aws/aws-sdk-js-v3/tree/v3.637.0)), distributed under the [Apache-2.0 License](https://github.com/aws/aws-sdk-js-v3/blob/v3.637.0/LICENSE):
This product includes source derived from [@aws-sdk/client-s3](https://github.com/aws/aws-sdk-js-v3) ([v3.632.0](https://github.com/aws/aws-sdk-js-v3/tree/v3.632.0)), distributed under the [Apache-2.0 License](https://github.com/aws/aws-sdk-js-v3/blob/v3.632.0/LICENSE):

```
Apache License
Expand Down Expand Up @@ -1285,7 +1285,7 @@ This product includes source derived from [@aws-sdk/client-s3](https://github.co

### @aws-sdk/s3-request-presigner

This product includes source derived from [@aws-sdk/s3-request-presigner](https://github.com/aws/aws-sdk-js-v3) ([v3.637.0](https://github.com/aws/aws-sdk-js-v3/tree/v3.637.0)), distributed under the [Apache-2.0 License](https://github.com/aws/aws-sdk-js-v3/blob/v3.637.0/LICENSE):
This product includes source derived from [@aws-sdk/s3-request-presigner](https://github.com/aws/aws-sdk-js-v3) ([v3.632.0](https://github.com/aws/aws-sdk-js-v3/tree/v3.632.0)), distributed under the [Apache-2.0 License](https://github.com/aws/aws-sdk-js-v3/blob/v3.632.0/LICENSE):

```
Apache License
Expand Down Expand Up @@ -1521,6 +1521,35 @@ THE SOFTWARE.

```

### @matteo.collina/tspl

This product includes source derived from [@matteo.collina/tspl](https://github.com/mcollina/tspl) ([v0.1.1](https://github.com/mcollina/tspl/tree/v0.1.1)), distributed under the [MIT License](https://github.com/mcollina/tspl/blob/v0.1.1/LICENSE):

```
MIT License

Copyright (c) 2023 Matteo Collina

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

```

### @newrelic/eslint-config

This product includes source derived from [@newrelic/eslint-config](https://github.com/newrelic/eslint-config-newrelic) ([v0.3.0](https://github.com/newrelic/eslint-config-newrelic/tree/v0.3.0)), distributed under the [Apache-2.0 License](https://github.com/newrelic/eslint-config-newrelic/blob/v0.3.0/LICENSE):
Expand Down Expand Up @@ -2656,7 +2685,7 @@ SOFTWARE.

### async

This product includes source derived from [async](https://github.com/caolan/async) ([v3.2.6](https://github.com/caolan/async/tree/v3.2.6)), distributed under the [MIT License](https://github.com/caolan/async/blob/v3.2.6/LICENSE):
This product includes source derived from [async](https://github.com/caolan/async) ([v3.2.5](https://github.com/caolan/async/tree/v3.2.5)), distributed under the [MIT License](https://github.com/caolan/async/blob/v3.2.5/LICENSE):

```
Copyright (c) 2010-2018 Caolan McMahon
Expand All @@ -2683,7 +2712,7 @@ THE SOFTWARE.

### aws-sdk

This product includes source derived from [aws-sdk](https://github.com/aws/aws-sdk-js) ([v2.1687.0](https://github.com/aws/aws-sdk-js/tree/v2.1687.0)), distributed under the [Apache-2.0 License](https://github.com/aws/aws-sdk-js/blob/v2.1687.0/LICENSE.txt):
This product includes source derived from [aws-sdk](https://github.com/aws/aws-sdk-js) ([v2.1677.0](https://github.com/aws/aws-sdk-js/tree/v2.1677.0)), distributed under the [Apache-2.0 License](https://github.com/aws/aws-sdk-js/blob/v2.1677.0/LICENSE.txt):

```

Expand Down Expand Up @@ -3978,29 +4007,6 @@ OTHER DEALINGS IN THE SOFTWARE.

```

### rfdc

This product includes source derived from [rfdc](https://github.com/davidmarkclements/rfdc) ([v1.4.1](https://github.com/davidmarkclements/rfdc/tree/v1.4.1)), distributed under the [MIT License](https://github.com/davidmarkclements/rfdc/blob/v1.4.1/LICENSE):

```
Copyright 2019 "David Mark Clements <david.mark.clements@gmail.com>"

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.

```

### rimraf

This product includes source derived from [rimraf](https://github.com/isaacs/rimraf) ([v2.7.1](https://github.com/isaacs/rimraf/tree/v2.7.1)), distributed under the [ISC License](https://github.com/isaacs/rimraf/blob/v2.7.1/LICENSE):
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@
"@aws-sdk/client-s3": "^3.556.0",
"@aws-sdk/s3-request-presigner": "^3.556.0",
"@koa/router": "^12.0.1",
"@matteo.collina/tspl": "^0.1.1",
"@newrelic/eslint-config": "^0.3.0",
"@newrelic/newrelic-oss-cli": "^0.1.2",
"@newrelic/test-utilities": "^9.1.0",
Expand Down Expand Up @@ -254,7 +255,6 @@
"nock": "11.8.0",
"proxy": "^2.1.1",
"proxyquire": "^1.8.0",
"rfdc": "^1.3.1",
"rimraf": "^2.6.3",
"self-cert": "^2.0.0",
"should": "*",
Expand Down
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
Loading