Skip to content

Commit

Permalink
test: Migrated last group of unit tests to node:test
Browse files Browse the repository at this point in the history
  • Loading branch information
bizob2828 committed Oct 4, 2024
1 parent 25f2d21 commit 4857498
Show file tree
Hide file tree
Showing 11 changed files with 950 additions and 699 deletions.
115 changes: 115 additions & 0 deletions test/lib/promises/fixtures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'
const { tspl } = require('@matteo.collina/tspl')
const helper = require('../agent_helper')
const COUNT = 2
const { checkTransaction, end, runMultiple } = require('./helpers')

module.exports = function init({ t, agent, Promise }) {
return async function performTests(name, resolve, reject) {
const inTx = doPerformTests({ t, agent, Promise, name, resolve, reject, inTx: true })
const notInTx = doPerformTests({ t, agent, Promise, name, resolve, reject, inTx: false })
return Promise.all([inTx, notInTx])
}
}

async function doPerformTests({ t, agent, Promise, name, resolve, reject, inTx }) {
name += ' ' + (inTx ? 'with' : 'without') + ' transaction'

await t.test(name + ': does not cause JSON to crash', async function (t) {
const plan = tspl(t, { plan: 1 * COUNT + 1 })

runMultiple(
COUNT,
function (i, cb) {
if (inTx) {
helper.runInTransaction(agent, test)
} else {
test(null)
}

function test(transaction) {
const p = resolve(Promise).then(end(transaction, cb), end(transaction, cb))
const d = p.domain
delete p.domain
plan.doesNotThrow(function () {
JSON.stringify(p)
}, 'should not cause stringification to crash')
p.domain = d
}
},
function (err) {
plan.ok(!err, 'should not error')
}
)
await plan.completed
})

await t.test(name + ': preserves transaction in resolve callback', async function (t) {
const plan = tspl(t, { plan: 4 * COUNT + 1 })

runMultiple(
COUNT,
function (i, cb) {
if (inTx) {
helper.runInTransaction(agent, test)
} else {
test(null)
}

function test(transaction) {
resolve(Promise)
.then(function step() {
plan.ok(1, 'should not change execution profile')
return i
})
.then(function finalHandler(res) {
plan.equal(res, i, 'should be the correct value')
checkTransaction(plan, agent, transaction)
})
.then(end(transaction, cb), end(transaction, cb))
}
},
function (err) {
plan.ok(!err, 'should not error')
}
)
await plan.completed
})

await t.test(name + ': preserves transaction in reject callback', async function (t) {
const plan = tspl(t, { plan: 3 * COUNT + 1 })

runMultiple(
COUNT,
function (i, cb) {
if (inTx) {
helper.runInTransaction(agent, test)
} else {
test(null)
}

function test(transaction) {
const err = new Error('some error ' + i)
reject(Promise, err)
.then(function unusedStep() {
plan.ok(0, 'should not change execution profile')
})
.catch(function catchHandler(reason) {
plan.equal(reason, err, 'should be the same error')
checkTransaction(plan, agent, transaction)
})
.then(end(transaction, cb), end(transaction, cb))
}
},
function (err) {
plan.ok(!err, 'should not error')
}
)
await plan.completed
})
}
47 changes: 47 additions & 0 deletions test/lib/promises/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

function runMultiple(count, fn, cb) {
let finished = 0
for (let i = 0; i < count; ++i) {
fn(i, function runMultipleCallback() {
if (++finished >= count) {
cb()
}
})
}
}

function checkTransaction(plan, agent, transaction) {
const currentTransaction = agent.getTransaction()

if (transaction) {
plan.ok(currentTransaction, 'should be in a transaction')
if (!currentTransaction) {
return
}
plan.equal(currentTransaction.id, transaction.id, 'should be the same transaction')
} else {
plan.ok(!currentTransaction, 'should not be in a transaction')
plan.ok(1) // Make test count match for both branches.
}
}

function end(tx, cb) {
return function () {
if (tx) {
tx.end()
}
cb()
}
}

module.exports = {
checkTransaction,
end,
runMultiple
}
Loading

0 comments on commit 4857498

Please sign in to comment.