Skip to content

Commit 39ba6b3

Browse files
[ci-visibility] Remove usage of .asyncResource in mocha plugin (#4348)
1 parent bf909c0 commit 39ba6b3

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

packages/datadog-instrumentations/src/mocha/main.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const {
2323
getOnTestEndHandler,
2424
getOnHookEndHandler,
2525
getOnFailHandler,
26-
getOnPendingHandler
26+
getOnPendingHandler,
27+
testFileToSuiteAr
2728
} = require('./utils')
2829
require('./common')
2930

@@ -39,7 +40,6 @@ let isSuitesSkippingEnabled = false
3940
let earlyFlakeDetectionNumRetries = 0
4041
let knownTests = []
4142
let itrCorrelationId = ''
42-
const testFileToSuiteAr = new Map()
4343
let isForcedToRun = false
4444

4545
// We'll preserve the original coverage here
@@ -469,7 +469,7 @@ addHook({
469469
// Used to start and finish test session and test module
470470
addHook({
471471
name: 'mocha',
472-
versions: ['>=8.0.0'],
472+
versions: ['>=5.2.0'],
473473
file: 'lib/nodejs/parallel-buffered-runner.js'
474474
}, (ParallelBufferedRunner, frameworkVersion) => {
475475
shimmer.wrap(ParallelBufferedRunner.prototype, 'run', run => function () {

packages/datadog-instrumentations/src/mocha/utils.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const testToAr = new WeakMap()
2121
const originalFns = new WeakMap()
2222
const testToStartLine = new WeakMap()
2323
const testFileToSuiteAr = new Map()
24+
const wrappedFunctions = new WeakSet()
2425

2526
function isNewTest (test, knownTests) {
2627
const testSuite = getTestSuitePath(test.file, process.cwd())
@@ -87,7 +88,7 @@ function getTestAsyncResource (test) {
8788
if (!test.fn) {
8889
return testToAr.get(test)
8990
}
90-
if (!test.fn.asyncResource) {
91+
if (!wrappedFunctions.has(test.fn)) {
9192
return testToAr.get(test.fn)
9293
}
9394
const originalFn = originalFns.get(test.fn)
@@ -105,9 +106,10 @@ function runnableWrapper (RunnablePackage) {
105106
const isTestHook = isBeforeEach || isAfterEach
106107

107108
// we restore the original user defined function
108-
if (this.fn.asyncResource) {
109+
if (wrappedFunctions.has(this.fn)) {
109110
const originalFn = originalFns.get(this.fn)
110111
this.fn = originalFn
112+
wrappedFunctions.delete(this.fn)
111113
}
112114

113115
if (isTestHook || this.type === 'test') {
@@ -122,11 +124,7 @@ function runnableWrapper (RunnablePackage) {
122124
originalFns.set(newFn, this.fn)
123125
this.fn = newFn
124126

125-
// Temporarily keep functionality when .asyncResource is removed from node
126-
// in https://github.com/nodejs/node/pull/46432
127-
if (!this.fn.asyncResource) {
128-
this.fn.asyncResource = asyncResource
129-
}
127+
wrappedFunctions.add(this.fn)
130128
}
131129
}
132130

@@ -303,5 +301,6 @@ module.exports = {
303301
getOnTestEndHandler,
304302
getOnHookEndHandler,
305303
getOnFailHandler,
306-
getOnPendingHandler
304+
getOnPendingHandler,
305+
testFileToSuiteAr
307306
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ describe('Plugin', () => {
143143
mocha.addFile(testFilePath)
144144
mocha.run()
145145
})
146+
146147
it('works with failing tests', (done) => {
147148
const testFilePath = path.join(__dirname, 'mocha-test-fail.js')
148149
const testSuite = testFilePath.replace(`${process.cwd()}/`, '')
@@ -179,6 +180,7 @@ describe('Plugin', () => {
179180
mocha.addFile(testFilePath)
180181
mocha.run()
181182
})
183+
182184
it('works with skipping tests', (done) => {
183185
const testFilePath = path.join(__dirname, 'mocha-test-skip.js')
184186
const testNames = [

packages/dd-trace/test/setup/mocha.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ function loadInst (plugin) {
2626
loadInstFile(`${plugin}/server.js`, instrumentations)
2727
loadInstFile(`${plugin}/client.js`, instrumentations)
2828
} catch (e) {
29-
loadInstFile(`${plugin}.js`, instrumentations)
29+
try {
30+
loadInstFile(`${plugin}/main.js`, instrumentations)
31+
} catch (e) {
32+
loadInstFile(`${plugin}.js`, instrumentations)
33+
}
3034
}
3135

3236
return instrumentations
@@ -143,10 +147,12 @@ function withNamingSchema (
143147
function withPeerService (tracer, pluginName, spanGenerationFn, service, serviceSource, opts = {}) {
144148
describe('peer service computation' + (opts.desc ? ` ${opts.desc}` : ''), () => {
145149
let computePeerServiceSpy
150+
146151
beforeEach(() => {
147152
const plugin = tracer()._pluginManager._pluginsByName[pluginName]
148153
computePeerServiceSpy = sinon.stub(plugin._tracerConfig, 'spanComputePeerService').value(true)
149154
})
155+
150156
afterEach(() => {
151157
computePeerServiceSpy.restore()
152158
})

0 commit comments

Comments
 (0)