Skip to content

Commit 9f903dd

Browse files
authored
[test-optimization] [SDTEST-1060] Add mocked files of Jest to the list of dependencies in TIA (#5885)
1 parent 07e9939 commit 9f903dd

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// eslint-disable-next-line no-undef
2+
jest.mock('../test/sum.js')
3+
4+
test('adds 1 + 2 to equal 3', () => {
5+
expect(1 + 2).toBe(3)
6+
})

integration-tests/jest/jest.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,6 +1560,37 @@ describe('jest CommonJS', () => {
15601560
}).catch(done)
15611561
})
15621562
})
1563+
1564+
it('report code coverage with all mocked files', (done) => {
1565+
const codeCovRequestPromise = receiver.payloadReceived(({ url }) => url === '/api/v2/citestcov')
1566+
1567+
codeCovRequestPromise.then((codeCovRequest) => {
1568+
const allCoverageFiles = codeCovRequest.payload
1569+
.flatMap(coverage => coverage.content.coverages)
1570+
.flatMap(file => file.files)
1571+
.map(file => file.filename)
1572+
1573+
assert.includeMembers(allCoverageFiles, [
1574+
'ci-visibility/test/sum.js',
1575+
'ci-visibility/jest/mocked-test.js'
1576+
])
1577+
}).catch(done)
1578+
1579+
childProcess = exec(
1580+
runTestsWithCoverageCommand,
1581+
{
1582+
cwd,
1583+
env: {
1584+
...getCiVisAgentlessConfig(receiver.port),
1585+
TESTS_TO_RUN: 'jest/mocked-test.js',
1586+
},
1587+
stdio: 'pipe'
1588+
}
1589+
)
1590+
childProcess.on('exit', () => {
1591+
done()
1592+
})
1593+
})
15631594
})
15641595

15651596
context('early flake detection', () => {

packages/datadog-instrumentations/src/jest.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const { addHook, channel } = require('./helpers/instrument')
44
const shimmer = require('../../datadog-shimmer')
55
const log = require('../../dd-trace/src/log')
6+
const path = require('path')
67
const {
78
getCoveredFilenamesFromCoverage,
89
JEST_WORKER_TRACE_PAYLOAD_CODE,
@@ -90,6 +91,7 @@ const retriedTestsToNumAttempts = new Map()
9091
const newTestsTestStatuses = new Map()
9192
const attemptToFixRetriedTestsStatuses = new Map()
9293
const wrappedWorkers = new WeakSet()
94+
const testSuiteMockedFiles = new Map()
9395

9496
const BREAKPOINT_HIT_GRACE_PERIOD_MS = 200
9597

@@ -137,6 +139,7 @@ function getWrappedEnvironment (BaseEnvironment, jestVersion) {
137139
this.nameToParams = {}
138140
this.global._ddtrace = global._ddtrace
139141
this.hasSnapshotTests = undefined
142+
this.testSuiteAbsolutePath = context.testPath
140143

141144
this.displayName = config.projectConfig?.displayName?.name
142145
this.testEnvironmentOptions = getTestEnvironmentOptions(config)
@@ -1095,10 +1098,12 @@ function jestAdapterWrapper (jestAdapter, jestVersion) {
10951098
if (environment.testEnvironmentOptions?._ddTestCodeCoverageEnabled) {
10961099
const root = environment.repositoryRoot || environment.rootDir
10971100

1098-
const coverageFiles = getCoveredFilenamesFromCoverage(environment.global.__coverage__)
1099-
.map(filename => getTestSuitePath(filename, root))
1101+
const getFilesWithPath = (files) => files.map(file => getTestSuitePath(file, root))
11001102

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 })
11021107
}
11031108
testSuiteFinishCh.publish({ status, errorMessage })
11041109
return suiteResults
@@ -1267,6 +1272,23 @@ addHook({
12671272
}, (runtimePackage) => {
12681273
const Runtime = runtimePackage.default ?? runtimePackage
12691274

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+
12701292
shimmer.wrap(Runtime.prototype, 'requireModuleOrMock', requireModuleOrMock => function (from, moduleName) {
12711293
// TODO: do this for every library that we instrument
12721294
if (shouldBypassJestRequireEngine(moduleName)) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,11 @@ class JestPlugin extends CiPlugin {
318318
* because this subscription happens in a different process from the one
319319
* fetching the ITR config.
320320
*/
321-
this.addSub('ci:jest:test-suite:code-coverage', ({ coverageFiles, testSuite }) => {
321+
this.addSub('ci:jest:test-suite:code-coverage', ({ coverageFiles, testSuite, mockedFiles }) => {
322322
if (!coverageFiles.length) {
323323
this.telemetry.count(TELEMETRY_CODE_COVERAGE_EMPTY)
324324
}
325-
const files = [...coverageFiles, testSuite]
325+
const files = [...coverageFiles, ...mockedFiles, testSuite]
326326

327327
const { _traceId, _spanId } = this.testSuiteSpan.context()
328328
const formattedCoverage = {

0 commit comments

Comments
 (0)