Skip to content

Commit

Permalink
wip: started migrating shim unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bizob2828 committed Sep 12, 2024
1 parent 619f23c commit ff701d2
Show file tree
Hide file tree
Showing 8 changed files with 2,581 additions and 2,568 deletions.
22 changes: 21 additions & 1 deletion test/lib/custom-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function isNonWritable({ obj, key, value }) {
if (value) {
assert.equal(obj[key], value)
} else {
assert.ok(!obj[key], 'testNonWritable test value', 'should not set value when non-writable')
assert.notEqual(obj[key], 'testNonWritable test value', 'should not set value when non-writable')

Check failure on line 66 in test/lib/custom-assertions.js

View workflow job for this annotation

GitHub Actions / lint (lts/*)

Replace `obj[key],·'testNonWritable·test·value',·'should·not·set·value·when·non-writable'` with `⏎······obj[key],⏎······'testNonWritable·test·value',⏎······'should·not·set·value·when·non-writable'⏎····`
}
}

Expand Down Expand Up @@ -323,12 +323,32 @@ function assertMetricValues(transaction, expected, exact) {
}
}

/**
* Asserts the wrapped callback is wrapped and the unwrapped version is the original.
* It also verifies it does not throw an error
*
* @param {object} shim shim lib
* @param {Function} original callback
*/
function checkWrappedCb(shim, cb) {
// The wrapped calledback is always the last argument
const wrappedCB = arguments[arguments.length - 1]
assert.notStrictEqual(wrappedCB, cb)
assert.ok(shim.isWrapped(wrappedCB))
assert.equal(shim.unwrap(wrappedCB), cb)

assert.doesNotThrow(function () {
wrappedCB()
})
}

module.exports = {
assertCLMAttrs,
assertExactClmAttrs,
assertMetrics,
assertMetricValues,
assertSegments,
checkWrappedCb,
compareSegments,
isNonWritable,
match
Expand Down
93 changes: 44 additions & 49 deletions test/unit/shim/conglomerate-shim.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*/

'use strict'

const { test } = require('tap')
const assert = require('node:assert')
const test = require('node:test')
const ConglomerateShim = require('../../../lib/shim/conglomerate-shim')
const DatastoreShim = require('../../../lib/shim/datastore-shim')
const helper = require('../../lib/agent_helper')
Expand All @@ -15,72 +15,67 @@ const Shim = require('../../../lib/shim/shim')
const TransactionShim = require('../../../lib/shim/transaction-shim')
const WebFrameworkShim = require('../../../lib/shim/webframework-shim')

test('ConglomerateShim', (t) => {
t.autoend()
let agent = null
let shim = null

t.beforeEach(() => {
agent = helper.loadMockedAgent()
shim = new ConglomerateShim(agent, 'test-module')
test('ConglomerateShim', async (t) => {
t.beforeEach((ctx) => {
ctx.nr = {}
const agent = helper.loadMockedAgent()
ctx.nr.shim = new ConglomerateShim(agent, 'test-module')
ctx.nr.agent = agent
})

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

t.test('should require an agent parameter', (t) => {
t.throws(() => new ConglomerateShim(), /^Shim must be initialized with .*? agent/)
t.end()
await t.test('should require an agent parameter', (t) => {

Check failure on line 30 in test/unit/shim/conglomerate-shim.test.js

View workflow job for this annotation

GitHub Actions / lint (lts/*)

't' is defined but never used
assert.throws(() => new ConglomerateShim(), 'Error: Shim must be initialized with an agent and module name.')

Check failure on line 31 in test/unit/shim/conglomerate-shim.test.js

View workflow job for this annotation

GitHub Actions / lint (lts/*)

Replace `()·=>·new·ConglomerateShim(),·'Error:·Shim·must·be·initialized·with·an·agent·and·module·name.'` with `⏎······()·=>·new·ConglomerateShim(),⏎······'Error:·Shim·must·be·initialized·with·an·agent·and·module·name.'⏎····`
})
t.test('should require a module name parameter', (t) => {
t.throws(() => new ConglomerateShim(agent), /^Shim must be initialized with .*? module name/)
t.end()
await t.test('should require a module name parameter', (t) => {
const { agent } = t.nr
assert.throws(() => new ConglomerateShim(agent), 'Error: Shim must be initialized with an agent and module name.')

Check failure on line 35 in test/unit/shim/conglomerate-shim.test.js

View workflow job for this annotation

GitHub Actions / lint (lts/*)

Replace `()·=>·new·ConglomerateShim(agent),·'Error:·Shim·must·be·initialized·with·an·agent·and·module·name.'` with `⏎······()·=>·new·ConglomerateShim(agent),⏎······'Error:·Shim·must·be·initialized·with·an·agent·and·module·name.'⏎····`
})

t.test('should exist for each shim type', (t) => {
t.ok(shim.GENERIC, 'generic')
t.ok(shim.DATASTORE, 'datastore')
t.ok(shim.MESSAGE, 'message')
t.ok(shim.PROMISE, 'promise')
t.ok(shim.TRANSACTION, 'transaction')
t.ok(shim.WEB_FRAMEWORK, 'web-framework')
t.end()
await t.test('should exist for each shim type', (t) => {
const { shim } = t.nr
assert.equal(shim.GENERIC, 'generic')
assert.equal(shim.DATASTORE, 'datastore')
assert.equal(shim.MESSAGE, 'message')
assert.equal(shim.PROMISE, 'promise')
assert.equal(shim.TRANSACTION, 'transaction')
assert.equal(shim.WEB_FRAMEWORK, 'web-framework')
})

t.test('should construct a new shim', (t) => {
await t.test('should construct a new shim', (t) => {
const { shim } = t.nr
const specialShim = shim.makeSpecializedShim(shim.GENERIC, 'foobar')
t.ok(specialShim instanceof Shim)
t.not(specialShim, shim)
t.end()
assert.ok(specialShim instanceof Shim)
assert.notEqual(specialShim, shim)
})

t.test('should be an instance of the correct class', (t) => {
t.ok(shim.makeSpecializedShim(shim.GENERIC, 'foobar') instanceof Shim)
t.ok(shim.makeSpecializedShim(shim.DATASTORE, 'foobar') instanceof DatastoreShim)
t.ok(shim.makeSpecializedShim(shim.MESSAGE, 'foobar') instanceof MessageShim)
t.ok(shim.makeSpecializedShim(shim.PROMISE, 'foobar') instanceof PromiseShim)
t.ok(shim.makeSpecializedShim(shim.TRANSACTION, 'foobar') instanceof TransactionShim)
t.ok(shim.makeSpecializedShim(shim.WEB_FRAMEWORK, 'foobar') instanceof WebFrameworkShim)
t.end()
await t.test('should be an instance of the correct class', (t) => {
const { shim } = t.nr
assert.ok(shim.makeSpecializedShim(shim.GENERIC, 'foobar') instanceof Shim)
assert.ok(shim.makeSpecializedShim(shim.DATASTORE, 'foobar') instanceof DatastoreShim)
assert.ok(shim.makeSpecializedShim(shim.MESSAGE, 'foobar') instanceof MessageShim)
assert.ok(shim.makeSpecializedShim(shim.PROMISE, 'foobar') instanceof PromiseShim)
assert.ok(shim.makeSpecializedShim(shim.TRANSACTION, 'foobar') instanceof TransactionShim)
assert.ok(shim.makeSpecializedShim(shim.WEB_FRAMEWORK, 'foobar') instanceof WebFrameworkShim)
})

t.test('should assign properties from parent', (t) => {
await t.test('should assign properties from parent', (t) => {
const { agent } = t.nr
const mod = 'test-mod'
const name = mod
const version = '1.0.0'
const shim = new ConglomerateShim(agent, mod, mod, name, version)
t.equal(shim.moduleName, mod)
t.equal(agent, shim._agent)
t.equal(shim.pkgVersion, version)
assert.equal(shim.moduleName, mod)
assert.equal(agent, shim._agent)
assert.equal(shim.pkgVersion, version)
function childFn() {}
const childShim = shim.makeSpecializedShim(shim.DATASTORE, childFn)
t.same(shim._agent, childShim._agent)
t.equal(shim.moduleName, childShim.moduleName)
t.equal(shim.pkgVersion, childShim.pkgVersion)
t.equal(shim.id, childShim.id)
t.end()
assert.deepEqual(shim._agent, childShim._agent)
assert.equal(shim.moduleName, childShim.moduleName)
assert.equal(shim.pkgVersion, childShim.pkgVersion)
assert.equal(shim.id, childShim.id)
})
})
Loading

0 comments on commit ff701d2

Please sign in to comment.