Skip to content

Commit 19a7bfc

Browse files
authored
[test optimization] feat: support multiple docblocks (#6925)
This allows other comments to be above the actual datadog docblock. This is also a requested feature in jest-docblock. Refs: jestjs/jest#12573 It is limited to 10 comments to prevent taking too long in case the file contains many comments.
1 parent df0ee1e commit 19a7bfc

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

integration-tests/ci-visibility/unskippable-test/test-unskippable.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
/** Some comment */
12
/**
23
* @datadog {"unskippable": true}
34
*/
5+
/* Some other comment */
46
'use strict'
57

68
const { expect } = require('chai')

packages/datadog-plugin-jest/src/util.js

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

33
const { readFileSync } = require('fs')
4-
const { parse, extract } = require('jest-docblock')
4+
const { parse } = require('jest-docblock')
55

66
const { getTestSuitePath } = require('../../dd-trace/src/plugins/util/test')
77
const log = require('../../dd-trace/src/log')
@@ -61,29 +61,59 @@ function getJestTestName (test, shouldStripSeed = false) {
6161
return testName
6262
}
6363

64+
const globalDocblockRegExp = /^\s*(\/\*\*?(.|\r?\n)*?\*\/)/
65+
6466
function isMarkedAsUnskippable (test) {
65-
let docblocks
67+
let testSource
6668

6769
try {
68-
const testSource = readFileSync(test.path, 'utf8')
69-
docblocks = parse(extract(testSource))
70+
testSource = readFileSync(test.path, 'utf8')
7071
} catch {
71-
// If we have issues parsing the file, we'll assume no unskippable was passed
7272
return false
7373
}
7474

75-
// docblocks were correctly parsed but it does not include a @datadog block
76-
if (!docblocks?.datadog) {
77-
return false
78-
}
75+
const re = globalDocblockRegExp
76+
re.lastIndex = 0
77+
let commentsChecked = 0
78+
79+
while (testSource.length) {
80+
const match = re.exec(testSource)
81+
if (!match) break
82+
const comment = match[1]
83+
84+
let docblocks
85+
try {
86+
docblocks = parse(comment)
87+
} catch {
88+
// Skip unparsable comment and continue scanning
89+
if (commentsChecked++ >= 10) {
90+
return false
91+
}
92+
continue
93+
}
7994

80-
try {
81-
return JSON.parse(docblocks.datadog).unskippable
82-
} catch {
83-
// If the @datadog block comment is present but malformed, we'll run the suite
84-
log.warn('@datadog block comment is malformed.')
85-
return true
95+
if (docblocks?.datadog) {
96+
try {
97+
// @ts-expect-error The datadog type is defined by us and may only be a string.
98+
return JSON.parse(docblocks.datadog).unskippable
99+
} catch {
100+
// If the @datadog block comment is present but malformed, we'll run the suite
101+
log.warn('@datadog block comment is malformed.')
102+
return true
103+
}
104+
}
105+
106+
if (commentsChecked++ >= 10) {
107+
return false
108+
}
109+
110+
// To stop as soon as no doc blocks are found, slice the source. That way the
111+
// regexp works by using the `^` anchor. Without it, it would continue
112+
// scanning the rest of the file.
113+
testSource = testSource.slice(match[0].length)
86114
}
115+
116+
return false
87117
}
88118

89119
function getJestSuitesToRun (skippableSuites, originalTests, rootDir) {

0 commit comments

Comments
 (0)