Skip to content

Commit

Permalink
test: Migrated unit tests to node:test (#2623)
Browse files Browse the repository at this point in the history
  • Loading branch information
bizob2828 authored Oct 2, 2024
1 parent 386f546 commit 86231b7
Show file tree
Hide file tree
Showing 6 changed files with 679 additions and 784 deletions.
170 changes: 83 additions & 87 deletions test/unit/rum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
*/

'use strict'
const tap = require('tap')
const assert = require('node:assert')
const test = require('node:test')
const helper = require('../lib/agent_helper')
const API = require('../../api')

const hashes = require('../../lib/util/hashes')

tap.test('the RUM API', function (t) {
t.autoend()
t.beforeEach(function (t) {
test('the RUM API', async function (t) {
t.beforeEach(function (ctx) {
ctx.nr = {}
const agent = helper.loadMockedAgent({
license_key: 'license key here',
browser_monitoring: {
Expand All @@ -27,213 +28,208 @@ tap.test('the RUM API', function (t) {
agent.config.application_id = 12345
agent.config.browser_monitoring.browser_key = 1234
agent.config.browser_monitoring.js_agent_loader = 'function() {}'
t.context.api = new API(agent)
t.context.agent = agent
ctx.nr.api = new API(agent)
ctx.nr.agent = agent
})

t.afterEach(function (t) {
helper.unloadAgent(t.context.agent)
t.afterEach(function (ctx) {
helper.unloadAgent(ctx.nr.agent)
})

t.test('should not generate header when disabled', function (t) {
const { agent, api } = t.context
await t.test('should not generate header when disabled', function (t) {
const { agent, api } = t.nr
agent.config.browser_monitoring.enable = false
t.equal(api.getBrowserTimingHeader(), '<!-- NREUM: (0) -->')
t.end()
assert.equal(api.getBrowserTimingHeader(), '<!-- NREUM: (0) -->')
})

t.test('should issue a warning outside a transaction by default', function (t) {
const { api } = t.context
t.equal(api.getBrowserTimingHeader(), '<!-- NREUM: (1) -->')
t.end()
await t.test('should issue a warning outside a transaction by default', function (t) {
const { api } = t.nr
assert.equal(api.getBrowserTimingHeader(), '<!-- NREUM: (1) -->')
})

t.test(
await t.test(
'should issue a warning outside a transaction and allowTransactionlessInjection is false',
function (t) {
const { api } = t.context
t.equal(
const { api } = t.nr
assert.equal(
api.getBrowserTimingHeader({ allowTransactionlessInjection: false }),
'<!-- NREUM: (1) -->'
)
t.end()
}
)

t.test('should issue a warning if the transaction was ignored', function (t) {
const { agent, api } = t.context
await t.test('should issue a warning if the transaction was ignored', function (t, end) {
const { agent, api } = t.nr
helper.runInTransaction(agent, function (tx) {
tx.ignore = true
t.equal(api.getBrowserTimingHeader(), '<!-- NREUM: (1) -->')
t.end()
assert.equal(api.getBrowserTimingHeader(), '<!-- NREUM: (1) -->')
end()
})
})

t.test('should not generate header config is missing', function (t) {
const { agent, api } = t.context
await t.test('should not generate header config is missing', function (t) {
const { agent, api } = t.nr
agent.config.browser_monitoring = undefined
t.equal(api.getBrowserTimingHeader(), '<!-- NREUM: (2) -->')
t.end()
assert.equal(api.getBrowserTimingHeader(), '<!-- NREUM: (2) -->')
})

t.test('should issue a warning if transaction has no name', function (t) {
const { agent, api } = t.context
await t.test('should issue a warning if transaction has no name', function (t, end) {
const { agent, api } = t.nr
helper.runInTransaction(agent, function () {
t.equal(api.getBrowserTimingHeader(), '<!-- NREUM: (3) -->')
t.end()
assert.equal(api.getBrowserTimingHeader(), '<!-- NREUM: (3) -->')
end()
})
})

t.test('should issue a warning without an application_id', function (t) {
const { agent, api } = t.context
await t.test('should issue a warning without an application_id', function (t, end) {
const { agent, api } = t.nr
agent.config.application_id = undefined
helper.runInTransaction(agent, function (tx) {
tx.finalizeNameFromUri('hello')
t.equal(api.getBrowserTimingHeader(), '<!-- NREUM: (4) -->')
t.end()
assert.equal(api.getBrowserTimingHeader(), '<!-- NREUM: (4) -->')
end()
})
})

t.test('should return the rum headers when in a named transaction', function (t) {
const { agent, api } = t.context
await t.test('should return the rum headers when in a named transaction', function (t, end) {
const { agent, api } = t.nr
helper.runInTransaction(agent, function (tx) {
tx.finalizeNameFromUri('hello')
t.equal(api.getBrowserTimingHeader().indexOf('<script'), 0)
t.end()
assert.equal(api.getBrowserTimingHeader().indexOf('<script'), 0)
end()
})
})

t.test('should return pretty print when debugging', function (t) {
const { agent, api } = t.context
await t.test('should return pretty print when debugging', function (t, end) {
const { agent, api } = t.nr
agent.config.browser_monitoring.debug = true
helper.runInTransaction(agent, function (tx) {
tx.finalizeNameFromUri('hello')
// there should be about 5 new lines here, this is a really *rough*
// estimate if it's being pretty printed
t.ok(api.getBrowserTimingHeader().split('\n').length > 5)
t.end()
assert.ok(api.getBrowserTimingHeader().split('\n').length > 5)
end()
})
})

t.test('should be compact when not debugging', function (t) {
const { agent, api } = t.context
await t.test('should be compact when not debugging', function (t, end) {
const { agent, api } = t.nr
helper.runInTransaction(agent, function (tx) {
tx.finalizeNameFromUri('hello')
const l = api.getBrowserTimingHeader().split('\n').length
t.equal(l, 1)
t.end()
assert.equal(l, 1)
end()
})
})

t.test('should return empty headers when missing browser_key', function (t) {
const { agent, api } = t.context
await t.test('should return empty headers when missing browser_key', function (t, end) {
const { agent, api } = t.nr
agent.config.browser_monitoring.browser_key = undefined
helper.runInTransaction(agent, function (tx) {
tx.finalizeNameFromUri('hello')
t.equal(api.getBrowserTimingHeader(), '<!-- NREUM: (5) -->')
t.end()
assert.equal(api.getBrowserTimingHeader(), '<!-- NREUM: (5) -->')
end()
})
})

t.test('should return empty headers when missing js_agent_loader', function (t) {
const { agent, api } = t.context
await t.test('should return empty headers when missing js_agent_loader', function (t, end) {
const { agent, api } = t.nr
agent.config.browser_monitoring.js_agent_loader = ''
helper.runInTransaction(agent, function (tx) {
tx.finalizeNameFromUri('hello')
t.equal(api.getBrowserTimingHeader(), '<!-- NREUM: (6) -->')
t.end()
assert.equal(api.getBrowserTimingHeader(), '<!-- NREUM: (6) -->')
end()
})
})

t.test('should be empty headers when loader is none', function (t) {
const { agent, api } = t.context
await t.test('should be empty headers when loader is none', function (t, end) {
const { agent, api } = t.nr
agent.config.browser_monitoring.loader = 'none'
helper.runInTransaction(agent, function (tx) {
tx.finalizeNameFromUri('hello')
t.equal(api.getBrowserTimingHeader(), '<!-- NREUM: (7) -->')
t.end()
assert.equal(api.getBrowserTimingHeader(), '<!-- NREUM: (7) -->')
end()
})
})

t.test('should get browser agent script with wrapping tag', function (t) {
const { agent, api } = t.context
await t.test('should get browser agent script with wrapping tag', function (t, end) {
const { agent, api } = t.nr
helper.runInTransaction(agent, function (tx) {
tx.finalizeNameFromUri('hello')
const timingHeader = api.getBrowserTimingHeader()
t.ok(
assert.ok(
timingHeader.startsWith(
`<script type=\'text/javascript\'>window.NREUM||(NREUM={});NREUM.info = {"licenseKey":1234,"applicationID":12345,`
)
)
t.ok(timingHeader.endsWith(`}; function() {}</script>`))
t.end()
assert.ok(timingHeader.endsWith(`}; function() {}</script>`))
end()
})
})

t.test(
await t.test(
'should get the browser agent script when outside a transaction and allowTransactionlessInjection is true',
function (t) {
const { api } = t.context
const { api } = t.nr
const timingHeader = api.getBrowserTimingHeader({ allowTransactionlessInjection: true })
t.ok(
assert.ok(
timingHeader.startsWith(
`<script type=\'text/javascript\'>window.NREUM||(NREUM={});NREUM.info = {"licenseKey":1234,"applicationID":12345,`
)
)
t.ok(timingHeader.endsWith(`}; function() {}</script>`))
t.end()
assert.ok(timingHeader.endsWith(`}; function() {}</script>`))
}
)

t.test(
await t.test(
'should get browser agent script with wrapping tag and add nonce attribute to script if passed in options',
function (t) {
const { agent, api } = t.context
function (t, end) {
const { agent, api } = t.nr
helper.runInTransaction(agent, function (tx) {
tx.finalizeNameFromUri('hello')
const timingHeader = api.getBrowserTimingHeader({ nonce: '12345' })
t.ok(
assert.ok(
timingHeader.startsWith(
`<script type=\'text/javascript\' nonce="12345">window.NREUM||(NREUM={});NREUM.info = {"licenseKey":1234,"applicationID":12345,`
)
)
t.ok(timingHeader.endsWith(`}; function() {}</script>`))
t.end()
assert.ok(timingHeader.endsWith(`}; function() {}</script>`))
end()
})
}
)

t.test(
await t.test(
'should get browser agent script without wrapping tag if hasToRemoveScriptWrapper passed in options',
function (t) {
const { agent, api } = t.context
function (t, end) {
const { agent, api } = t.nr
helper.runInTransaction(agent, function (tx) {
tx.finalizeNameFromUri('hello')
const timingHeader = api.getBrowserTimingHeader({ hasToRemoveScriptWrapper: true })
t.ok(
assert.ok(
timingHeader.startsWith(
'window.NREUM||(NREUM={});NREUM.info = {"licenseKey":1234,"applicationID":12345,'
)
)
t.ok(timingHeader.endsWith(`}; function() {}`))
t.end()
assert.ok(timingHeader.endsWith(`}; function() {}`))
end()
})
}
)

t.test('should add custom attributes', function (t) {
const { agent, api } = t.context
await t.test('should add custom attributes', function (t, end) {
const { agent, api } = t.nr
helper.runInTransaction(agent, function (tx) {
api.addCustomAttribute('hello', 1)
tx.finalizeNameFromUri('hello')
const payload = /"atts":"(.*)"/.exec(api.getBrowserTimingHeader())
t.ok(payload)
assert.ok(payload)
const deobf = hashes.deobfuscateNameUsingKey(
payload[1],
agent.config.license_key.substring(0, 13)
)
t.equal(JSON.parse(deobf).u.hello, 1)
t.end()
assert.equal(JSON.parse(deobf).u.hello, 1)
end()
})
})
})
Loading

0 comments on commit 86231b7

Please sign in to comment.