Skip to content

Commit

Permalink
test: Migrated elasticsearch and esm-package versioned tests to `node…
Browse files Browse the repository at this point in the history
…:test` (newrelic#2680)
  • Loading branch information
bizob2828 authored Nov 4, 2024
1 parent d8dfe84 commit 0e0c2b2
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 346 deletions.

Large diffs are not rendered by default.

61 changes: 0 additions & 61 deletions test/versioned/elastic/elasticsearchNoop.tap.js

This file was deleted.

59 changes: 59 additions & 0 deletions test/versioned/elastic/elasticsearchNoop.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'
const test = require('node:test')
const assert = require('node:assert')
const helper = require('../../lib/agent_helper')
const params = require('../../lib/params')
const crypto = require('crypto')
const DB_INDEX = `test-${randomString()}`

function randomString() {
return crypto.randomBytes(5).toString('hex')
}

test('Elasticsearch instrumentation', async (t) => {
t.beforeEach(async (ctx) => {
const agent = helper.instrumentMockedAgent()

// need to capture attributes
agent.config.attributes.enabled = true
const { Client } = require('@elastic/elasticsearch')

const client = new Client({
node: `http://${params.elastic_host}:${params.elastic_port}`
})
ctx.nr = {
agent,
client
}
})

t.afterEach((ctx) => {
helper.unloadAgent(ctx.nr.agent)
})

await t.test(
'unsupported version should noop db tracing, but record web transaction',
async (t) => {
const { agent, client } = t.nr
await helper.runInTransaction(agent, async function transactionInScope(transaction) {
try {
await client.indices.create({ index: DB_INDEX })
} catch (e) {
assert.ok(!e, 'should not error')
}
const firstChild = transaction?.trace?.root?.children[0]
assert.equal(
firstChild.name,
`External/localhost:9200/${DB_INDEX}`,
'should record index creation as an external transaction'
)
await client.indices.delete({ index: DB_INDEX })
})
}
)
})
4 changes: 2 additions & 2 deletions test/versioned/elastic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@elastic/elasticsearch": "7.13.0"
},
"files": [
"elasticsearchNoop.tap.js"
"elasticsearchNoop.test.js"
]
},
{
Expand All @@ -28,7 +28,7 @@
"@elastic/elasticsearch": ">=7.16.0"
},
"files": [
"elasticsearch.tap.js"
"elasticsearch.test.js"
]
}
]
Expand Down
2 changes: 1 addition & 1 deletion test/versioned/esm-package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"parse-json": "6.0.2"
},
"files": [
"parse-json.tap.mjs"
"parse-json.test.mjs"
]
}
],
Expand Down
45 changes: 0 additions & 45 deletions test/versioned/esm-package/parse-json.tap.mjs

This file was deleted.

36 changes: 36 additions & 0 deletions test/versioned/esm-package/parse-json.test.mjs
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
*/

import test from 'node:test'
import assert from 'node:assert'
import helper from '../../lib/agent_helper.js'
import shimmer from '../../../lib/shimmer.js'
import parseJsonInstrumentation from './parse-json-instrumentation.mjs'

shimmer.registerInstrumentation({
moduleName: 'parse-json',
type: 'generic',
isEsm: true,
onRequire: parseJsonInstrumentation
})

test('ESM Package Instrumentation', async (t) => {
const agent = helper.instrumentMockedAgent()
const { default: parseJson, JSONError } = await import('parse-json')

t.after(() => {
helper.unloadAgent(agent)
})

await t.test('should register instrumentation on default exports', () => {
const output = parseJson(JSON.stringify({ foo: 'bar' }))
assert.ok(output.isInstrumented, 'should have the field we add in our test instrumentation')
})

await t.test('should register instrumentation on named exports', () => {
const err = new JSONError('test me')
assert.ok(err.isInstrumented, 'JSONError should be instrumented')
})
})

0 comments on commit 0e0c2b2

Please sign in to comment.