Skip to content

Commit eddfeab

Browse files
committed
test: use assert.strictEqual / notStrictEqual for === and !== checks
`assert.ok(x === y)` / `assert(x !== y)` reduce to "Expected true, got false" on failure — the actual operand values are lost, which makes flaky-test debugging painful. Switching the affected call sites to `assert.strictEqual(x, y)` / `assert.notStrictEqual(x, y)` preserves any existing message argument and includes both operands in the failure output automatically. Add two `no-restricted-syntax` selectors in `eslint.config.mjs` to prevent regressions, scoped to the existing `TEST_FILES` glob.
1 parent 54f0666 commit eddfeab

21 files changed

Lines changed: 61 additions & 45 deletions

File tree

eslint.config.mjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,22 @@ export default [
750750
'no-restricted-syntax': ['error', {
751751
selector: "CallExpression:matches([callee.name='doesNotThrow'], [callee.property.name='doesNotThrow'])",
752752
message: 'Do not use `assert.doesNotThrow()`. Execute the expression directly instead.',
753+
}, {
754+
// `assert(a === b)` / `assert.ok(a === b)` → `assert.strictEqual(a, b)`
755+
selector:
756+
'CallExpression[arguments.length<=2]' +
757+
':matches([callee.name="assert"], [callee.object.name="assert"][callee.property.name="ok"])' +
758+
' > BinaryExpression[operator="==="]:first-child',
759+
message: 'Use `assert.strictEqual(a, b)` instead of `assert(a === b)` / `assert.ok(a === b)`. ' +
760+
'The strict variant includes both values in the failure message automatically.',
761+
}, {
762+
// `assert(a !== b)` / `assert.ok(a !== b)` → `assert.notStrictEqual(a, b)`
763+
selector:
764+
'CallExpression[arguments.length<=2]' +
765+
':matches([callee.name="assert"], [callee.object.name="assert"][callee.property.name="ok"])' +
766+
' > BinaryExpression[operator="!=="]:first-child',
767+
message: 'Use `assert.notStrictEqual(a, b)` instead of `assert(a !== b)` / `assert.ok(a !== b)`. ' +
768+
'The strict variant includes both values in the failure message automatically.',
753769
}],
754770
'n/no-missing-require': 'off',
755771
'require-await': 'off',

integration-tests/helpers/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const defaultStopProcTimeoutMs = 2_000
4646
*/
4747
async function runAndCheckOutput (filename, cwd, expectedOut, expectedSource) {
4848
const proc = spawn(process.execPath, [filename], { cwd, stdio: 'pipe' })
49-
assert(proc.pid !== undefined, 'Process PID is not available')
49+
assert.notStrictEqual(proc.pid, undefined, 'Process PID is not available')
5050
const pid = proc.pid
5151
let out = await new Promise((resolve, reject) => {
5252
proc.once('error', reject)
@@ -162,9 +162,9 @@ function assertTelemetryPoints (pid, msgs, expectedTelemetryPoints) {
162162
}
163163

164164
// Validate result metadata is present and has valid values
165-
assert(typeof actualMetadata.result === 'string', 'result should be a string')
166-
assert(typeof actualMetadata.result_class === 'string', 'result_class should be a string')
167-
assert(typeof actualMetadata.result_reason === 'string', 'result_reason should be a string')
165+
assert.strictEqual(typeof actualMetadata.result, 'string', 'result should be a string')
166+
assert.strictEqual(typeof actualMetadata.result_class, 'string', 'result_class should be a string')
167+
assert.strictEqual(typeof actualMetadata.result_reason, 'string', 'result_reason should be a string')
168168
assert(actualMetadata.result, 'result field should be present')
169169
assert(actualMetadata.result_class, 'result_class field should be present')
170170
assert(actualMetadata.result_reason, 'result_reason field should be present')
@@ -555,7 +555,7 @@ async function createSandbox (
555555

556556
const match = dep.replaceAll(/['"]/g, '').match(/^(@?[^@]+)(@(.+))?$/)
557557

558-
assert(match !== null, `Invalid dependency format: ${dep}`)
558+
assert.notStrictEqual(match, null, `Invalid dependency format: ${dep}`)
559559

560560
const name = match[1]
561561
const range = match[3] || ''
@@ -1090,7 +1090,7 @@ function assertObjectContainsImpl (actual, expected, msg, useMatchers) {
10901090
} else if (useMatchers && val === ANY_NUMBER) {
10911091
assert.strictEqual(typeof actual[key], 'number', `Expected ${key} to be a number but got ${typeof actual[key]}`)
10921092
} else if (useMatchers && val === ANY_VALUE) {
1093-
assert.ok(actual[key] !== undefined, `Expected ${key} to be present but it was undefined`)
1093+
assert.notStrictEqual(actual[key], undefined, `Expected ${key} to be present but it was undefined`)
10941094
} else if (val !== null && typeof val === 'object') {
10951095
assertObjectContainsImpl(actual[key], val, msg, useMatchers)
10961096
} else {

integration-tests/profiler/profiler.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function expectProfileMessagePromise (agent, timeout,
6767
})
6868
}
6969
if (expectSeq) {
70-
assert(event.tags_profiler.indexOf(',profile_seq:') !== -1)
70+
assert.notStrictEqual(event.tags_profiler.indexOf(',profile_seq:'), -1)
7171
}
7272
} catch (e) {
7373
e.message += ` ${JSON.stringify({ headers, files, event })}`
@@ -162,7 +162,7 @@ class NetworkEventProcessor extends TimelineEventProcessor {
162162

163163
decorateEvent (ev, pl) {
164164
// Exactly one of these is defined
165-
assert.ok(!!pl.address !== !!pl.host, this.encoded)
165+
assert.notStrictEqual(!!pl.address, !!pl.host, this.encoded)
166166
if (pl.address) {
167167
ev.address = this.strings.strings[pl.address]
168168
} else {

packages/datadog-instrumentations/test/fs.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ describe('fs instrumentation', () => {
1313
it('require node:fs should work', () => {
1414
return agent.load('node:fs', undefined, { flushInterval: 1 }).then(() => {
1515
const fs = require('node:fs')
16-
assert(fs !== undefined)
16+
assert.notStrictEqual(fs, undefined)
1717
})
1818
})
1919

2020
it('require fs should work', () => {
2121
return agent.load('fs', undefined, { flushInterval: 1 }).then(() => {
2222
const fs = require('fs')
23-
assert(fs !== undefined)
23+
assert.notStrictEqual(fs, undefined)
2424
})
2525
})
2626
})

packages/datadog-instrumentations/test/http.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describe('client', () => {
8989

9090
sinon.assert.called(startChannelCb)
9191
const ctx = getContextFromStubByUrl(url, startChannelCb)
92-
assert(ctx !== null)
92+
assert.notStrictEqual(ctx, null)
9393
assert(ctx.abortController instanceof AbortController)
9494
})
9595

@@ -181,7 +181,7 @@ describe('client', () => {
181181
// Necessary because the tracer makes extra requests to the agent
182182
if (asyncStartChannelCb.called) {
183183
const ctx = getContextFromStubByUrl(url, asyncStartChannelCb)
184-
assert(ctx === null)
184+
assert.strictEqual(ctx, null)
185185
}
186186

187187
done()
@@ -292,7 +292,7 @@ describe('client', () => {
292292
res.on('end', () => {
293293
try {
294294
const payload = getResponseFinishPayload(url, responseFinishChannelCb)
295-
assert(typeof payload.body === 'string')
295+
assert.strictEqual(typeof payload.body, 'string')
296296

297297
const expectedBody = chunks.join('')
298298
assert.strictEqual(payload.body, expectedBody)
@@ -324,7 +324,7 @@ describe('client', () => {
324324
res.on('end', () => {
325325
try {
326326
const payload = getResponseFinishPayload(url, responseFinishChannelCb)
327-
assert(typeof payload.body === 'string')
327+
assert.strictEqual(typeof payload.body, 'string')
328328

329329
const expectedBody = chunks.join('')
330330
assert.strictEqual(payload.body, expectedBody)
@@ -358,7 +358,7 @@ describe('client', () => {
358358
res.on('end', () => {
359359
try {
360360
const payload = getResponseFinishPayload(url, responseFinishChannelCb)
361-
assert(typeof payload.body === 'string')
361+
assert.strictEqual(typeof payload.body, 'string')
362362
const expectedBody = chunks.join('')
363363
assert.strictEqual(payload.body, expectedBody)
364364

packages/datadog-plugin-ai/test/index.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ describe('Plugin', () => {
162162
})
163163

164164
assert.ok(result.text, 'Expected result to be truthy')
165-
assert.ok(experimentalTelemetry.tracer === myTracer, 'Tracer should be set when `isEnabled` is true')
165+
assert.strictEqual(experimentalTelemetry.tracer, myTracer, 'Tracer should be set when `isEnabled` is true')
166166

167167
await checkTraces
168168
})
@@ -203,7 +203,7 @@ describe('Plugin', () => {
203203
})
204204

205205
assert.ok(result.text, 'Expected result to be truthy')
206-
assert.ok(experimentalTelemetry.tracer === myTracer, 'Tracer should not override provided tracer')
206+
assert.strictEqual(experimentalTelemetry.tracer, myTracer, 'Tracer should not override provided tracer')
207207

208208
await checkTraces
209209
})

packages/datadog-plugin-anthropic/test/index.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ describe('Plugin', () => {
4040
temperature: 0.5,
4141
})
4242

43-
assert.ok(
44-
typeof promise.withResponse === 'function',
45-
'Expected custom Anthropic APIPromise to have a withResponse method'
43+
assert.strictEqual(
44+
typeof promise.withResponse, 'function',
45+
'Expected custom Anthropic APIPromise to have a withResponse method',
4646
)
4747

4848
const result = await promise
@@ -69,9 +69,9 @@ describe('Plugin', () => {
6969
stream: true,
7070
})
7171

72-
assert.ok(
73-
typeof promise.withResponse === 'function',
74-
'Expected custom Anthropic APIPromise to have a withResponse method'
72+
assert.strictEqual(
73+
typeof promise.withResponse, 'function',
74+
'Expected custom Anthropic APIPromise to have a withResponse method',
7575
)
7676

7777
const stream = await promise

packages/datadog-plugin-google-cloud-pubsub/test/pubsub-push-subscription.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ describe('Push Subscription Plugin', () => {
100100
})
101101

102102
// Verify delivery_duration_ms
103-
assert.ok(pubsubSpan.metrics['pubsub.delivery_duration_ms'] !== undefined)
104-
assert.ok(typeof pubsubSpan.metrics['pubsub.delivery_duration_ms'] === 'number')
103+
assert.notStrictEqual(pubsubSpan.metrics['pubsub.delivery_duration_ms'], undefined)
104+
assert.strictEqual(typeof pubsubSpan.metrics['pubsub.delivery_duration_ms'], 'number')
105105
assert.ok(pubsubSpan.metrics['pubsub.delivery_duration_ms'] >= 0)
106106
})
107107
.then(done)

packages/dd-trace/test/aiguard/index.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ describe('AIGuard SDK', () => {
487487
await aiguard.evaluate(prompt, { block: false })
488488
})
489489
await agent.assertSomeTraces(traces => {
490-
assert.ok(traces[0].length === 2, 'Trace should contain two spans root + ai_guard')
490+
assert.strictEqual(traces[0].length, 2, 'Trace should contain two spans root + ai_guard')
491491
for (const span of traces[0]) {
492492
if (span.name === 'root') {
493493
assert.strictEqual(span.meta[EVENT_TAG_KEY], 'true')

packages/dd-trace/test/appsec/reporter.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe('reporter', () => {
8484
it('should return empty object when providing no headers', () => {
8585
const result = Reporter.filterHeaders(null)
8686

87-
assert.ok(Object.keys(result).length === 0)
87+
assert.strictEqual(Object.keys(result).length, 0)
8888
})
8989

9090
it('should filter and format headers from passlist', () => {

0 commit comments

Comments
 (0)