|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | 3 | const { readFileSync } = require('fs') |
4 | | -const { parse, extract } = require('jest-docblock') |
| 4 | +const { parse } = require('jest-docblock') |
5 | 5 |
|
6 | 6 | const { getTestSuitePath } = require('../../dd-trace/src/plugins/util/test') |
7 | 7 | const log = require('../../dd-trace/src/log') |
@@ -61,29 +61,59 @@ function getJestTestName (test, shouldStripSeed = false) { |
61 | 61 | return testName |
62 | 62 | } |
63 | 63 |
|
| 64 | +const globalDocblockRegExp = /^\s*(\/\*\*?(.|\r?\n)*?\*\/)/ |
| 65 | + |
64 | 66 | function isMarkedAsUnskippable (test) { |
65 | | - let docblocks |
| 67 | + let testSource |
66 | 68 |
|
67 | 69 | try { |
68 | | - const testSource = readFileSync(test.path, 'utf8') |
69 | | - docblocks = parse(extract(testSource)) |
| 70 | + testSource = readFileSync(test.path, 'utf8') |
70 | 71 | } catch { |
71 | | - // If we have issues parsing the file, we'll assume no unskippable was passed |
72 | 72 | return false |
73 | 73 | } |
74 | 74 |
|
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 | + } |
79 | 94 |
|
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) |
86 | 114 | } |
| 115 | + |
| 116 | + return false |
87 | 117 | } |
88 | 118 |
|
89 | 119 | function getJestSuitesToRun (skippableSuites, originalTests, rootDir) { |
|
0 commit comments