Skip to content

Commit 4b403e5

Browse files
fix(test-optimization): support older Jest and Mocha versions (#9695)
1 parent 4ccdc93 commit 4b403e5

3 files changed

Lines changed: 59 additions & 6 deletions

File tree

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

3-
const assert = require('node:assert/strict')
4-
53
describe('test-flaky-test-retries', () => {
64
test.each([
75
['passing row', true],
86
['failing row', false],
97
])('preserves parameters between retries', (row, shouldPass) => {
10-
assert.strictEqual(shouldPass, true, row)
8+
expect(shouldPass).toBe(true)
119
})
1210
})

integration-tests/ci-visibility/mocha-plugin-tests/top-level-it-mixed-only.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ it('top-level passing test', () => {})
44

55
// eslint-disable-next-line mocha/no-exclusive-tests
66
describe.only('an exclusive describe block', () => {
7-
it('nested passing test', () => {})
7+
// eslint-disable-next-line mocha/no-exclusive-tests
8+
it.only('nested passing test', () => {})
9+
it('nested non-exclusive test', () => {})
810
})

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

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,57 @@ function isFailedTestReplayEnabled () {
398398
return config.isTestDynamicInstrumentationEnabled && config.isDiEnabled
399399
}
400400

401+
/**
402+
* @typedef {object} MochaSuite
403+
* @property {MochaSuite[]} suites
404+
* @property {import('mocha').Test[]} tests
405+
* @property {MochaSuite[]} _onlySuites
406+
* @property {import('mocha').Test[]} _onlyTests
407+
*/
408+
409+
/**
410+
* Mirrors Mocha 5's private exclusivity check.
411+
*
412+
* @param {MochaSuite} suite
413+
* @returns {boolean}
414+
*/
415+
function hasOnly (suite) {
416+
if (suite._onlyTests.length || suite._onlySuites.length) return true
417+
418+
for (const childSuite of suite.suites) {
419+
if (hasOnly(childSuite)) return true
420+
}
421+
return false
422+
}
423+
424+
/**
425+
* Mirrors Mocha 5's private exclusivity filter.
426+
*
427+
* @param {MochaSuite} suite
428+
* @returns {boolean}
429+
*/
430+
function filterOnly (suite) {
431+
if (suite._onlyTests.length) {
432+
suite.tests = suite._onlyTests
433+
suite.suites = []
434+
} else {
435+
suite.tests = []
436+
437+
for (const onlySuite of suite._onlySuites) {
438+
if (hasOnly(onlySuite)) filterOnly(onlySuite)
439+
}
440+
441+
const filteredSuites = []
442+
for (const childSuite of suite.suites) {
443+
if (suite._onlySuites.includes(childSuite) || filterOnly(childSuite)) {
444+
filteredSuites.push(childSuite)
445+
}
446+
}
447+
suite.suites = filteredSuites
448+
}
449+
return suite.tests.length > 0 || suite.suites.length > 0
450+
}
451+
401452
function getExecutionConfiguration (runner, isParallel, frameworkVersion, onFinishRequest, localSuites) {
402453
const ctx = {
403454
isParallel,
@@ -431,8 +482,10 @@ function getExecutionConfiguration (runner, isParallel, frameworkVersion, onFini
431482

432483
// We remove the suites that we skip through ITR
433484
// Mocha normally applies exclusivity after this asynchronous configuration step.
434-
if (runner.suite.hasOnly()) {
435-
runner.suite.filterOnly()
485+
if (typeof runner.suite.hasOnly === 'function') {
486+
if (runner.suite.hasOnly()) runner.suite.filterOnly()
487+
} else if (hasOnly(runner.suite)) {
488+
filterOnly(runner.suite)
436489
}
437490
const numTestsToRun = runner.grepTotal(runner.suite)
438491
const filteredSuites = getFilteredSuites(runner.suite.suites)

0 commit comments

Comments
 (0)