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 unit tests to node:test #331

Merged
merged 2 commits into from
Nov 26, 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
30 changes: 30 additions & 0 deletions THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ code, the source code can be found at [https://github.com/newrelic/newrelic-node
* [@newrelic/newrelic-oss-cli](#newrelicnewrelic-oss-cli)
* [@newrelic/test-utilities](#newrelictest-utilities)
* [apollo-server](#apollo-server)
* [borp](#borp)
* [c8](#c8)
* [eslint-config-prettier](#eslint-config-prettier)
* [eslint-plugin-header](#eslint-plugin-header)
Expand Down Expand Up @@ -715,6 +716,35 @@ SOFTWARE.

```

### borp

This product includes source derived from [borp](https://github.com/mcollina/borp) ([v0.19.0](https://github.com/mcollina/borp/tree/v0.19.0)), distributed under the [MIT License](https://github.com/mcollina/borp/blob/v0.19.0/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.

```

### c8

This product includes source derived from [c8](https://github.com/bcoe/c8) ([v7.14.0](https://github.com/bcoe/c8/tree/v7.14.0)), distributed under the [ISC License](https://github.com/bcoe/c8/blob/v7.14.0/LICENSE.txt):
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"prepare": "husky install",
"test": "npm run unit && npm run integration && npm run versioned && npm run type-check",
"third-party-updates": "oss third-party manifest && oss third-party notices && git add THIRD_PARTY_NOTICES.md third_party_manifest.json",
"unit": "c8 -o ./coverage/unit tap --test-regex='(\\/|^tests\\/unit\\/.*\\.test\\.js)$' --no-coverage",
"unit": "c8 -o ./coverage/unit borp 'tests/unit/**/*.test.js'",
"type-check": "tsd",
"versioned": "NPM7=1 ./bin/run-versioned-tests.sh",
"versioned:folder": "versioned-tests --minor --all -i 2",
Expand Down Expand Up @@ -43,6 +43,7 @@
"@newrelic/newrelic-oss-cli": "^0.1.2",
"@newrelic/test-utilities": "^8.5.0",
"apollo-server": "^3.11.1",
"borp": "^0.19.0",
"c8": "^7.14.0",
"eslint": "^8.24.0",
"eslint-config-prettier": "^8.3.0",
Expand Down
42 changes: 21 additions & 21 deletions tests/unit/create-plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@
*/

'use strict'
const tap = require('tap')

const test = require('node:test')
const assert = require('node:assert')

const createPlugin = require('../../lib/create-plugin')
const sinon = require('sinon')

tap.test('createPlugin edge cases', (t) => {
t.autoend()
let operationSegment
let instrumentationApi

t.beforeEach(function () {
operationSegment = {
test('createPlugin edge cases', async (t) => {
t.beforeEach((ctx) => {
ctx.nr = {}
ctx.nr.operationSegment = {
start: sinon.stub(),
addAttribute: sinon.stub(),
transaction: { nameState: { setName: sinon.stub() } },
end: sinon.stub()
}

instrumentationApi = {
ctx.nr.instrumentationApi = {
shim: {
logger: {
child: sinon
Expand All @@ -35,8 +35,8 @@ tap.test('createPlugin edge cases', (t) => {
},
getActiveSegment: sinon.stub().returns({}),
createSegment: sinon.stub().callsFake((name) => {
operationSegment.name = name
return operationSegment
ctx.nr.operationSegment.name = name
return ctx.nr.operationSegment
}),
setActiveSegment: sinon.stub(),
isFunction: () => false
Expand All @@ -45,7 +45,8 @@ tap.test('createPlugin edge cases', (t) => {
}
})

t.test('should set deepest path to empty when not present', (t) => {
await t.test('should set deepest path to empty when not present', (t) => {
const { instrumentationApi, operationSegment } = t.nr
const responseContext = {
document: {
definitions: [
Expand All @@ -62,38 +63,37 @@ tap.test('createPlugin edge cases', (t) => {
const hooks = createPlugin(instrumentationApi)
const operationHooks = hooks.requestDidStart({})
operationHooks.willSendResponse(responseContext)
t.equal(
assert.equal(
operationSegment.name,
'GraphQL/operation/ApolloServer/undefined/<anonymous>',
'should set path to undefined'
)
t.end()
})

t.test('should not update operation name when document is null', (t) => {
await t.test('should not update operation name when document is null', (t) => {
const { instrumentationApi, operationSegment } = t.nr
const responseContext = {}
const hooks = createPlugin(instrumentationApi)
const operationHooks = hooks.requestDidStart({})
t.equal(
assert.equal(
operationSegment.name,
'GraphQL/operation/ApolloServer/<unknown>',
'should default operation name'
)
operationHooks.willSendResponse(responseContext)
t.equal(
assert.equal(
operationSegment.name,
'GraphQL/operation/ApolloServer/<unknown>',
'should not update operation name'
)
t.end()
})

t.test('should not crash when ctx.operation is undefined in didResolveOperation', (t) => {
await t.test('should not crash when ctx.operation is undefined in didResolveOperation', (t) => {
const { instrumentationApi } = t.nr
const hooks = createPlugin(instrumentationApi)
const operationHooks = hooks.requestDidStart({})
t.doesNotThrow(() => {
assert.doesNotThrow(() => {
operationHooks.didResolveOperation({})
})
t.end()
})
})
20 changes: 10 additions & 10 deletions tests/unit/error-helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
*/

'use strict'
const tap = require('tap')

const test = require('node:test')
const assert = require('node:assert')

const ErrorHelper = require('../../lib/error-helper')

Expand Down Expand Up @@ -33,18 +35,18 @@ class MockedInstrumentationApi {

const mockInstrumentationApi = new MockedInstrumentationApi()

tap.test('ErrorHelper tests', (t) => {
test('ErrorHelper tests', () => {
const errorHelper = new ErrorHelper()

const fixture1 = false
t.equal(
assert.equal(
false,
errorHelper.isValidRequestContext(mockInstrumentationApi, fixture1),
'returns false when requestContext is false'
)

const fixture2 = {}
t.equal(
assert.equal(
false,
errorHelper.isValidRequestContext(mockInstrumentationApi, fixture2),
'returns false when errors not set'
Expand All @@ -53,7 +55,7 @@ tap.test('ErrorHelper tests', (t) => {
const fixture3 = {
errors: null
}
t.equal(
assert.equal(
false,
errorHelper.isValidRequestContext(mockInstrumentationApi, fixture3),
'returns false when errors not an array'
Expand All @@ -62,7 +64,7 @@ tap.test('ErrorHelper tests', (t) => {
const fixture4 = {
errors: [new Error(), new Error()]
}
t.equal(
assert.equal(
true,
errorHelper.isValidRequestContext(mockInstrumentationApi, fixture4),
'returns true when errors array set'
Expand All @@ -72,13 +74,11 @@ tap.test('ErrorHelper tests', (t) => {
errorHelper.addErrorsFromApolloRequestContext(mockInstrumentationApi, fixture2)
errorHelper.addErrorsFromApolloRequestContext(mockInstrumentationApi, fixture3)
errorHelper.addErrorsFromApolloRequestContext(mockInstrumentationApi, fixture4)
t.equal(mockInstrumentationApi.mockedCollectedErrors.length, 2, 'captures only valid errors')
assert.equal(mockInstrumentationApi.mockedCollectedErrors.length, 2, 'captures only valid errors')

t.equal(
assert.equal(
mockInstrumentationApi.mockedLogsTrace.length,
6,
'six invaid calls mean six trace log messages'
)

t.end()
})
43 changes: 17 additions & 26 deletions tests/unit/query-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@

'use strict'

const tap = require('tap')
const test = require('node:test')
const assert = require('node:assert')

const cleanQuery = require('../../lib/query-utils')

tap.test('Obfuscate GraphQL query args tests', (t) => {
t.test('Should obfuscate query args', (t) => {
test('Obfuscate GraphQL query args tests', async (t) => {
await t.test('Should obfuscate query args', () => {
const query = `query logans(run: "(333") {
runner
}`
Expand All @@ -24,13 +25,11 @@ tap.test('Obfuscate GraphQL query args tests', (t) => {

const newQuery = cleanQuery(query, argLocations)

t.notOk(newQuery.includes('333'))
t.ok(newQuery.includes('logans(***)'))

t.end()
assert.equal(newQuery.includes('333'), false)
assert.ok(newQuery.includes('logans(***)'))
})

t.test('Should obfuscate aliased query args for aliased', (t) => {
await t.test('Should obfuscate aliased query args for aliased', () => {
const query = `query thing: logans(run: "(333") {
runner
}`
Expand All @@ -44,14 +43,12 @@ tap.test('Obfuscate GraphQL query args tests', (t) => {

const newQuery = cleanQuery(query, argLocations)

t.notOk(newQuery.includes('333'))
t.ok(newQuery.includes('thing: logans', 'alias is intact'))
t.ok(newQuery.includes('logans(***)', 'args obfuscated'))

t.end()
assert.equal(newQuery.includes('333'), false)
assert.ok(newQuery.includes('thing: logans', 'alias is intact'))
assert.ok(newQuery.includes('logans(***)', 'args obfuscated'))
})

t.test('Should obfuscate mutation args', (t) => {
await t.test('Should obfuscate mutation args', () => {
const argLocations = [
{
start: 14,
Expand All @@ -66,12 +63,10 @@ tap.test('Obfuscate GraphQL query args tests', (t) => {

const newQuery = cleanQuery(query, argLocations)

t.ok(newQuery.includes('corn(***)'))

t.end()
assert.ok(newQuery.includes('corn(***)'))
})

t.test('Should obfuscate multiple args', (t) => {
await t.test('Should obfuscate multiple args', () => {
const query = `query chickens(hens: 'yes') {
eggs(yolk: 'yes') {
yolk
Expand All @@ -91,13 +86,9 @@ tap.test('Obfuscate GraphQL query args tests', (t) => {

const newQuery = cleanQuery(query, argLocations)

t.notOk(newQuery.includes('hens'))
t.notOk(newQuery.includes('yolk:'))
t.ok(newQuery.includes('chickens(***)'))
t.ok(newQuery.includes('eggs(***)'))

t.end()
assert.equal(newQuery.includes('hens'), false)
assert.equal(newQuery.includes('yolk:'), false)
assert.ok(newQuery.includes('chickens(***)'))
assert.ok(newQuery.includes('eggs(***)'))
})

t.end()
})
15 changes: 14 additions & 1 deletion third_party_manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"lastUpdated": "Tue Apr 16 2024 07:41:31 GMT-0400 (Eastern Daylight Time)",
"lastUpdated": "Tue Nov 26 2024 12:17:31 GMT-0500 (Eastern Standard Time)",
"projectName": "New Relic Node Apollo Server Plugin",
"projectUrl": "https://github.com/newrelic/newrelic-node-apollo-server-plugin/",
"includeOptDeps": false,
Expand Down Expand Up @@ -71,6 +71,19 @@
"publisher": "Apollo",
"email": "[email protected]"
},
"[email protected]": {
"name": "borp",
"version": "0.19.0",
"range": "^0.19.0",
"licenses": "MIT",
"repoUrl": "https://github.com/mcollina/borp",
"versionedRepoUrl": "https://github.com/mcollina/borp/tree/v0.19.0",
"licenseFile": "node_modules/borp/LICENSE",
"licenseUrl": "https://github.com/mcollina/borp/blob/v0.19.0/LICENSE",
"licenseTextSource": "file",
"publisher": "Matteo Collina",
"email": "[email protected]"
},
"[email protected]": {
"name": "c8",
"version": "7.14.0",
Expand Down
Loading