Skip to content

Commit e137afe

Browse files
authored
test: rewrite assertion to use Node.js assert (#6951)
Most of these changes are done by a script including a lint:fix run afterwards. The script used was listed for visibility in the PR as separate commit that was reverted.
1 parent 603c6dc commit e137afe

File tree

324 files changed

+9537
-9074
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

324 files changed

+9537
-9074
lines changed

integration-tests/aiguard/index.spec.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
'use strict'
22

3-
const { describe, it, before, after } = require('mocha')
3+
const assert = require('node:assert/strict')
44
const path = require('path')
5+
6+
const { expect } = require('chai')
7+
const { after, afterEach, before, beforeEach, describe, it } = require('mocha')
8+
59
const { sandboxCwd, useSandbox, FakeAgent, spawnProc } = require('../helpers')
610
const startApiMock = require('./api-mock')
7-
const { expect } = require('chai')
811
const { executeRequest } = require('./util')
912

1013
describe('AIGuard SDK integration tests', () => {
@@ -59,16 +62,16 @@ describe('AIGuard SDK integration tests', () => {
5962
const headers = blocking ? { 'x-blocking-enabled': true } : null
6063
const response = await executeRequest(`${url}${endpoint}`, 'GET', headers)
6164
if (blocking && action !== 'ALLOW') {
62-
expect(response.status).to.equal(403)
65+
assert.strictEqual(response.status, 403)
6366
expect(response.body).to.contain(reason)
6467
} else {
65-
expect(response.status).to.equal(200)
68+
assert.strictEqual(response.status, 200)
6669
expect(response.body).to.have.nested.property('action', action)
6770
expect(response.body).to.have.nested.property('reason', reason)
6871
}
6972
await agent.assertMessageReceived(({ headers, payload }) => {
7073
const span = payload[0].find(span => span.name === 'ai_guard')
71-
expect(span).not.to.be.null
74+
assert.notStrictEqual(span, null)
7275
})
7376
})
7477
}

integration-tests/appsec/endpoints-collection.spec.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict'
22

3-
const { expect } = require('chai')
4-
const { describe, before, it } = require('mocha')
5-
3+
const assert = require('node:assert/strict')
64
const path = require('node:path')
75

6+
const { before, describe, it } = require('mocha')
7+
88
const { sandboxCwd, useSandbox, FakeAgent, spawnProc } = require('../helpers')
99

1010
describe('Endpoints collection', () => {
@@ -166,30 +166,30 @@ describe('Endpoints collection', () => {
166166
await telemetryPromise
167167

168168
const trueCount = isFirstFlags.filter(v => v === true).length
169-
expect(trueCount).to.equal(1)
169+
assert.strictEqual(trueCount, 1)
170170

171171
// Check that all expected endpoints were found
172172
expectedEndpoints.forEach(expected => {
173173
const found = endpointsFound.find(e =>
174174
e.method === expected.method && e.path === expected.path
175175
)
176176

177-
expect(found).to.exist
178-
expect(found.type).to.equal('REST')
179-
expect(found.operation_name).to.equal('http.request')
180-
expect(found.resource_name).to.equal(`${expected.method} ${expected.path}`)
177+
assert.ok(found != null)
178+
assert.strictEqual(found.type, 'REST')
179+
assert.strictEqual(found.operation_name, 'http.request')
180+
assert.strictEqual(found.resource_name, `${expected.method} ${expected.path}`)
181181
})
182182

183183
// check that no additional endpoints were found
184-
expect(endpointsFound.length).to.equal(expectedEndpoints.length)
184+
assert.strictEqual(endpointsFound.length, expectedEndpoints.length)
185185

186186
// Explicitly verify invalid endpoints are NOT reported
187187
if (framework === 'express') {
188188
const cycleEndpoints = endpointsFound.filter(e => e.path.includes('cycle'))
189-
expect(cycleEndpoints).to.have.lengthOf(0, 'Cycle router endpoints should not be collected')
189+
assert.strictEqual(cycleEndpoints.length, 0, 'Cycle router endpoints should not be collected')
190190

191191
const invalidEndpoints = endpointsFound.filter(e => e.path.includes('invalid'))
192-
expect(invalidEndpoints).to.have.lengthOf(0, 'Invalid router paths should not be collected')
192+
assert.strictEqual(invalidEndpoints.length, 0, 'Invalid router paths should not be collected')
193193
}
194194
} finally {
195195
proc?.kill()

integration-tests/telemetry.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict'
22

3-
const { expect } = require('chai')
4-
const { describe, before, it, beforeEach, afterEach } = require('mocha')
5-
3+
const assert = require('node:assert/strict')
64
const path = require('node:path')
75

6+
const { afterEach, before, beforeEach, describe, it } = require('mocha')
7+
88
const { sandboxCwd, useSandbox, FakeAgent, spawnProc, assertObjectContains } = require('./helpers')
99

1010
describe('telemetry', () => {
@@ -58,8 +58,8 @@ describe('telemetry', () => {
5858
}
5959
}, 'app-dependencies-loaded', 5_000, 1)
6060

61-
expect(ddTraceFound).to.be.true
62-
expect(importInTheMiddleFound).to.be.true
61+
assert.strictEqual(ddTraceFound, true)
62+
assert.strictEqual(importInTheMiddleFound, true)
6363
})
6464

6565
it('Assert configuration chaining data is sent', async () => {

packages/datadog-core/test/storage.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { expect } = require('chai')
3+
const assert = require('node:assert/strict')
44
const { describe, it, beforeEach, afterEach } = require('tap').mocha
55
const { executionAsyncResource } = require('async_hooks')
66

@@ -28,7 +28,7 @@ describe('storage', () => {
2828
testStorage.enterWith(store)
2929

3030
setImmediate(() => {
31-
expect(testStorage.getStore()).to.equal(store)
31+
assert.strictEqual(testStorage.getStore(), store)
3232
done()
3333
})
3434
})
@@ -41,14 +41,14 @@ describe('storage', () => {
4141
testStorage2.enterWith(store2)
4242

4343
setImmediate(() => {
44-
expect(testStorage.getStore()).to.equal(store)
45-
expect(testStorage2.getStore()).to.equal(store2)
44+
assert.strictEqual(testStorage.getStore(), store)
45+
assert.strictEqual(testStorage2.getStore(), store2)
4646
done()
4747
})
4848
})
4949

5050
it('should return the same storage for a namespace', () => {
51-
expect(storage('test')).to.equal(testStorage)
51+
assert.strictEqual(storage('test'), testStorage)
5252
})
5353

5454
it('should not have its store referenced by the underlying async resource', () => {
@@ -58,7 +58,7 @@ describe('storage', () => {
5858

5959
for (const sym of Object.getOwnPropertySymbols(resource)) {
6060
if (sym.toString() === 'Symbol(kResourceStore)' && resource[sym]) {
61-
expect(resource[sym]).to.not.have.property('internal')
61+
assert.ok(!Object.hasOwn(resource[sym], 'internal'))
6262
}
6363
}
6464
})

packages/datadog-core/test/utils/src/parse-tags.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { expect } = require('chai')
3+
const assert = require('node:assert/strict')
44
const { describe, it } = require('tap').mocha
55

66
require('../../../../dd-trace/test/setup/core')
@@ -15,12 +15,12 @@ describe('parseTags', () => {
1515
'a.1.a': 'baz'
1616
}
1717

18-
expect(parseTags(obj)).to.deep.equal({
18+
assert.deepStrictEqual(parseTags(obj), {
1919
a: [{ a: 'foo', b: 'bar' }, { a: 'baz' }]
2020
})
2121
})
2222

2323
it('should work with empty object', () => {
24-
expect(parseTags({})).to.deep.equal({})
24+
assert.deepStrictEqual(parseTags({}), {})
2525
})
2626
})

packages/datadog-core/test/utils/src/set.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { expect } = require('chai')
3+
const assert = require('node:assert/strict')
44
const { describe, it } = require('tap').mocha
55

66
require('../../../../dd-trace/test/setup/core')
@@ -14,6 +14,6 @@ describe('set', () => {
1414
set(obj, 'a', 1)
1515
set(obj, 'b.c', 2)
1616
set(obj, 'b.d.e', 3)
17-
expect(obj).to.deep.equal({ a: 1, b: { c: 2, d: { e: 3 } } })
17+
assert.deepStrictEqual(obj, { a: 1, b: { c: 2, d: { e: 3 } } })
1818
})
1919
})

packages/datadog-instrumentations/test/body-parser.spec.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
'use strict'
22

3+
const assert = require('node:assert/strict')
4+
35
const axios = require('axios')
46
const { expect } = require('chai')
57
const dc = require('dc-polyfill')
6-
const { describe, it, beforeEach, before, after } = require('mocha')
8+
const { after, before, beforeEach, describe, it } = require('mocha')
79
const sinon = require('sinon')
810

9-
const agent = require('../../dd-trace/test/plugins/agent')
1011
const { storage } = require('../../datadog-core')
12+
const agent = require('../../dd-trace/test/plugins/agent')
1113
const { withVersions } = require('../../dd-trace/test/setup/mocha')
12-
1314
withVersions('body-parser', 'body-parser', version => {
1415
describe('body parser instrumentation', () => {
1516
const bodyParserReadCh = dc.channel('datadog:body-parser:read:finish')
@@ -46,8 +47,8 @@ withVersions('body-parser', 'body-parser', version => {
4647
it('should not abort the request by default', async () => {
4748
const res = await axios.post(`http://localhost:${port}/`, { key: 'value' })
4849

49-
expect(middlewareProcessBodyStub).to.be.calledOnce
50-
expect(res.data).to.be.equal('DONE')
50+
sinon.assert.calledOnce(middlewareProcessBodyStub)
51+
assert.strictEqual(res.data, 'DONE')
5152
})
5253

5354
it('should not abort the request with non blocker subscription', async () => {
@@ -56,8 +57,8 @@ withVersions('body-parser', 'body-parser', version => {
5657

5758
const res = await axios.post(`http://localhost:${port}/`, { key: 'value' })
5859

59-
expect(middlewareProcessBodyStub).to.be.calledOnce
60-
expect(res.data).to.be.equal('DONE')
60+
sinon.assert.calledOnce(middlewareProcessBodyStub)
61+
assert.strictEqual(res.data, 'DONE')
6162

6263
bodyParserReadCh.unsubscribe(noop)
6364
})
@@ -72,7 +73,7 @@ withVersions('body-parser', 'body-parser', version => {
7273
const res = await axios.post(`http://localhost:${port}/`, { key: 'value' })
7374

7475
expect(middlewareProcessBodyStub).not.to.be.called
75-
expect(res.data).to.be.equal('BLOCKED')
76+
assert.strictEqual(res.data, 'BLOCKED')
7677

7778
bodyParserReadCh.unsubscribe(blockRequest)
7879
})
@@ -89,12 +90,12 @@ withVersions('body-parser', 'body-parser', version => {
8990

9091
const res = await axios.post(`http://localhost:${port}/`, { key: 'value' })
9192

92-
expect(store).to.have.property('req', payload.req)
93-
expect(store).to.have.property('res', payload.res)
94-
expect(store).to.have.property('span')
93+
assert.strictEqual(store.req, payload.req)
94+
assert.strictEqual(store.res, payload.res)
95+
assert.ok(Object.hasOwn(store, 'span'))
9596

96-
expect(middlewareProcessBodyStub).to.be.calledOnce
97-
expect(res.data).to.be.equal('DONE')
97+
sinon.assert.calledOnce(middlewareProcessBodyStub)
98+
assert.strictEqual(res.data, 'DONE')
9899

99100
bodyParserReadCh.unsubscribe(handler)
100101
})

0 commit comments

Comments
 (0)