Skip to content

Commit

Permalink
chore: Updated serverless unit tests to node:test (newrelic#2549)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr authored and sumitsuthar committed Sep 20, 2024
1 parent ff6f6fc commit 847dd58
Show file tree
Hide file tree
Showing 7 changed files with 744 additions and 602 deletions.
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 <[email protected]>"
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

0 comments on commit 847dd58

Please sign in to comment.