|
3 | 3 | const { addHook, channel } = require('./helpers/instrument')
|
4 | 4 | const shimmer = require('../../datadog-shimmer')
|
5 | 5 | const log = require('../../dd-trace/src/log')
|
| 6 | +const path = require('path') |
6 | 7 | const {
|
7 | 8 | getCoveredFilenamesFromCoverage,
|
8 | 9 | JEST_WORKER_TRACE_PAYLOAD_CODE,
|
@@ -90,6 +91,7 @@ const retriedTestsToNumAttempts = new Map()
|
90 | 91 | const newTestsTestStatuses = new Map()
|
91 | 92 | const attemptToFixRetriedTestsStatuses = new Map()
|
92 | 93 | const wrappedWorkers = new WeakSet()
|
| 94 | +const testSuiteMockedFiles = new Map() |
93 | 95 |
|
94 | 96 | const BREAKPOINT_HIT_GRACE_PERIOD_MS = 200
|
95 | 97 |
|
@@ -137,6 +139,7 @@ function getWrappedEnvironment (BaseEnvironment, jestVersion) {
|
137 | 139 | this.nameToParams = {}
|
138 | 140 | this.global._ddtrace = global._ddtrace
|
139 | 141 | this.hasSnapshotTests = undefined
|
| 142 | + this.testSuiteAbsolutePath = context.testPath |
140 | 143 |
|
141 | 144 | this.displayName = config.projectConfig?.displayName?.name
|
142 | 145 | this.testEnvironmentOptions = getTestEnvironmentOptions(config)
|
@@ -1095,10 +1098,12 @@ function jestAdapterWrapper (jestAdapter, jestVersion) {
|
1095 | 1098 | if (environment.testEnvironmentOptions?._ddTestCodeCoverageEnabled) {
|
1096 | 1099 | const root = environment.repositoryRoot || environment.rootDir
|
1097 | 1100 |
|
1098 |
| - const coverageFiles = getCoveredFilenamesFromCoverage(environment.global.__coverage__) |
1099 |
| - .map(filename => getTestSuitePath(filename, root)) |
| 1101 | + const getFilesWithPath = (files) => files.map(file => getTestSuitePath(file, root)) |
1100 | 1102 |
|
1101 |
| - testSuiteCodeCoverageCh.publish({ coverageFiles, testSuite: environment.testSourceFile }) |
| 1103 | + const coverageFiles = getFilesWithPath(getCoveredFilenamesFromCoverage(environment.global.__coverage__)) |
| 1104 | + const mockedFiles = getFilesWithPath(testSuiteMockedFiles.get(environment.testSuiteAbsolutePath) || []) |
| 1105 | + |
| 1106 | + testSuiteCodeCoverageCh.publish({ coverageFiles, testSuite: environment.testSourceFile, mockedFiles }) |
1102 | 1107 | }
|
1103 | 1108 | testSuiteFinishCh.publish({ status, errorMessage })
|
1104 | 1109 | return suiteResults
|
@@ -1267,6 +1272,23 @@ addHook({
|
1267 | 1272 | }, (runtimePackage) => {
|
1268 | 1273 | const Runtime = runtimePackage.default ?? runtimePackage
|
1269 | 1274 |
|
| 1275 | + shimmer.wrap(Runtime.prototype, '_createJestObjectFor', _createJestObjectFor => function (from) { |
| 1276 | + const result = _createJestObjectFor.apply(this, arguments) |
| 1277 | + const suiteFilePath = this._testPath |
| 1278 | + |
| 1279 | + shimmer.wrap(result, 'mock', mock => function (moduleName) { |
| 1280 | + if (suiteFilePath) { |
| 1281 | + const existingMockedFiles = testSuiteMockedFiles.get(suiteFilePath) || [] |
| 1282 | + const suiteDir = path.dirname(suiteFilePath) |
| 1283 | + const mockPath = path.resolve(suiteDir, moduleName) |
| 1284 | + existingMockedFiles.push(mockPath) |
| 1285 | + testSuiteMockedFiles.set(suiteFilePath, existingMockedFiles) |
| 1286 | + } |
| 1287 | + return mock.apply(this, arguments) |
| 1288 | + }) |
| 1289 | + return result |
| 1290 | + }) |
| 1291 | + |
1270 | 1292 | shimmer.wrap(Runtime.prototype, 'requireModuleOrMock', requireModuleOrMock => function (from, moduleName) {
|
1271 | 1293 | // TODO: do this for every library that we instrument
|
1272 | 1294 | if (shouldBypassJestRequireEngine(moduleName)) {
|
|
0 commit comments