Skip to content

Commit

Permalink
test: Migrated tests in test/unit/instrumentation to use node:test (
Browse files Browse the repository at this point in the history
  • Loading branch information
bizob2828 authored Sep 4, 2024
1 parent a0d1d18 commit 47b8398
Show file tree
Hide file tree
Showing 50 changed files with 2,562 additions and 2,650 deletions.
22 changes: 22 additions & 0 deletions test/lib/agent_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const https = require('https')
const semver = require('semver')
const crypto = require('crypto')
const util = require('util')
const cp = require('child_process')

const KEYPATH = path.join(__dirname, 'test-key.key')
const CERTPATH = path.join(__dirname, 'self-signed-test-certificate.crt')
Expand Down Expand Up @@ -644,3 +645,24 @@ helper.destroyProxyAgent = function destroyProxyAgent() {
helper.getShim = function getShim(pkg) {
return pkg?.[symbols.shim]
}

/**
* Executes a file in a child_process. This is intended to be
* used when you have to test destructive behavior that would be caught
* by `node:test`
*
* @param {object} params to function
* @param {string} params.cwd working directory of script
* @param {string} params.script script name
*/
helper.execSync = function execSync({ cwd, script }) {
try {
cp.execSync(`node ./${script}`, {
stdio: 'pipe',
encoding: 'utf8',
cwd
})
} catch (err) {
throw err.stderr
}
}
37 changes: 18 additions & 19 deletions test/unit/instrumentation/aws-sdk/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
*/

'use strict'
const tap = require('tap')
const test = require('node:test')
const assert = require('node:assert')
const {
grabLastUrlSegment,
setDynamoParameters
} = require('../../../../lib/instrumentation/aws-sdk/util')
const DatastoreParameters = require('../../../../lib/shim/specs/params/datastore')

tap.test('Utility Functions', (t) => {
t.ok(grabLastUrlSegment, 'imported function successfully')
test('Utility Functions', async () => {
assert.ok(grabLastUrlSegment, 'imported function successfully')

const fixtures = [
{
Expand All @@ -34,37 +36,34 @@ tap.test('Utility Functions', (t) => {

for (const [, fixture] of fixtures.entries()) {
const result = grabLastUrlSegment(fixture.input)
t.equal(result, fixture.output, `expecting ${result} to equal ${fixture.output}`)
assert.equal(result, fixture.output, `expecting ${result} to equal ${fixture.output}`)
}
t.end()
})

tap.test('DB parameters', (t) => {
t.autoend()

t.test('default values', (t) => {
test('DB parameters', async (t) => {
await t.test('default values', (t, end) => {
const input = {}
const endpoint = {}
const result = setDynamoParameters(endpoint, input)
t.same(
assert.deepEqual(
result,
{
new DatastoreParameters({
host: undefined,
database_name: null,
port_path_or_id: 443,
collection: 'Unknown'
},
}),
'should set default values for parameters'
)
t.end()
end()
})

// v2 uses host key
t.test('host, port, collection', (t) => {
await t.test('host, port, collection', (t, end) => {
const input = { TableName: 'unit-test' }
const endpoint = { host: 'unit-test-host', port: '123' }
const result = setDynamoParameters(endpoint, input)
t.same(
assert.deepEqual(
result,
{
host: endpoint.host,
Expand All @@ -74,15 +73,15 @@ tap.test('DB parameters', (t) => {
},
'should set appropriate parameters'
)
t.end()
end()
})

// v3 uses hostname key
t.test('hostname, port, collection', (t) => {
await t.test('hostname, port, collection', (t, end) => {
const input = { TableName: 'unit-test' }
const endpoint = { hostname: 'unit-test-host', port: '123' }
const result = setDynamoParameters(endpoint, input)
t.same(
assert.deepEqual(
result,
{
host: endpoint.hostname,
Expand All @@ -92,6 +91,6 @@ tap.test('DB parameters', (t) => {
},
'should set appropriate parameters'
)
t.end()
end()
})
})
Loading

0 comments on commit 47b8398

Please sign in to comment.