Skip to content

Commit c8f45ab

Browse files
[ci-visibility] Fix calculation of test suites to skip based on ITR response (#3130)
1 parent 4e7da80 commit c8f45ab

File tree

3 files changed

+75
-14
lines changed

3 files changed

+75
-14
lines changed

packages/datadog-instrumentations/src/jest.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@ const {
99
getCoveredFilenamesFromCoverage,
1010
JEST_WORKER_TRACE_PAYLOAD_CODE,
1111
JEST_WORKER_COVERAGE_PAYLOAD_CODE,
12-
getTestLineStart
12+
getTestLineStart,
13+
getTestSuitePath,
14+
getTestParametersString
1315
} = require('../../dd-trace/src/plugins/util/test')
16+
const {
17+
getFormattedJestTestParameters,
18+
getJestTestName,
19+
getJestSuitesToRun
20+
} = require('../../datadog-plugin-jest/src/util')
1421

1522
const testSessionStartCh = channel('ci:jest:session:start')
1623
const testSessionFinishCh = channel('ci:jest:session:finish')
@@ -37,13 +44,6 @@ let skippableSuites = []
3744
let isCodeCoverageEnabled = false
3845
let isSuitesSkippingEnabled = false
3946

40-
const {
41-
getTestSuitePath,
42-
getTestParametersString
43-
} = require('../../dd-trace/src/plugins/util/test')
44-
45-
const { getFormattedJestTestParameters, getJestTestName } = require('../../datadog-plugin-jest/src/util')
46-
4747
const sessionAsyncResource = new AsyncResource('bound-anonymous-fn')
4848

4949
const specStatusToTestStatus = {
@@ -429,10 +429,7 @@ addHook({
429429
const testPaths = await getTestPaths.apply(this, arguments)
430430
const { tests } = testPaths
431431

432-
const filteredTests = tests.filter(({ path: testPath }) => {
433-
const relativePath = testPath.replace(`${rootDir}/`, '')
434-
return !skippableSuites.includes(relativePath)
435-
})
432+
const filteredTests = getJestSuitesToRun(skippableSuites, tests, rootDir)
436433

437434
skippableSuites = []
438435

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { getTestSuitePath } = require('../../dd-trace/src/plugins/util/test')
2+
13
/**
24
* There are two ways to call `test.each` in `jest`:
35
* 1. With an array of arrays: https://jestjs.io/docs/api#1-testeachtablename-fn-timeout
@@ -45,4 +47,11 @@ function getJestTestName (test) {
4547
return titles.join(' ')
4648
}
4749

48-
module.exports = { getFormattedJestTestParameters, getJestTestName }
50+
function getJestSuitesToRun (skippableSuites, originalTests, rootDir) {
51+
return originalTests.filter(({ path: testPath }) => {
52+
const relativePath = getTestSuitePath(testPath, rootDir)
53+
return !skippableSuites.includes(relativePath)
54+
})
55+
}
56+
57+
module.exports = { getFormattedJestTestParameters, getJestTestName, getJestSuitesToRun }

packages/datadog-plugin-jest/test/util.spec.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const { getFormattedJestTestParameters } = require('../src/util')
1+
const path = require('path')
2+
const { getFormattedJestTestParameters, getJestSuitesToRun } = require('../src/util')
23

34
describe('getFormattedJestTestParameters', () => {
45
it('returns formatted parameters for arrays', () => {
@@ -18,3 +19,57 @@ describe('getFormattedJestTestParameters', () => {
1819
expect(resultObject).to.eql(undefined)
1920
})
2021
})
22+
23+
describe('getJestSuitesToRun', () => {
24+
it('returns filtered suites', () => {
25+
const skippableSuites = [
26+
'src/unit.spec.js',
27+
'src/integration.spec.js'
28+
]
29+
const tests = [
30+
{ path: '/workspace/dd-trace-js/src/unit.spec.js' },
31+
{ path: '/workspace/dd-trace-js/src/integration.spec.js' },
32+
{ path: '/workspace/dd-trace-js/src/e2e.spec.js' }
33+
]
34+
const rootDir = '/workspace/dd-trace-js'
35+
36+
const filteredSuites = getJestSuitesToRun(skippableSuites, tests, rootDir)
37+
expect(filteredSuites).to.eql([{ path: '/workspace/dd-trace-js/src/e2e.spec.js' }])
38+
})
39+
40+
it('returns filtered suites when paths are windows like', () => {
41+
const skippableSuites = [
42+
'src/unit.spec.js',
43+
'src/integration.spec.js'
44+
]
45+
const tests = [
46+
{ path: `C:${path.sep}temp${path.sep}dd-trace-js${path.sep}src${path.sep}unit.spec.js` },
47+
{ path: `C:${path.sep}temp${path.sep}dd-trace-js${path.sep}src${path.sep}integration.spec.js` },
48+
{ path: `C:${path.sep}temp${path.sep}dd-trace-js${path.sep}src${path.sep}e2e.spec.js` }
49+
]
50+
const rootDir = `C:${path.sep}temp${path.sep}dd-trace-js`
51+
52+
const filteredSuites = getJestSuitesToRun(skippableSuites, tests, rootDir)
53+
expect(filteredSuites).to.eql([
54+
{ path: `C:${path.sep}temp${path.sep}dd-trace-js${path.sep}src${path.sep}e2e.spec.js` }
55+
])
56+
})
57+
58+
it('returns filtered suites when paths are relative', () => {
59+
const skippableSuites = [
60+
'../../src/unit.spec.js',
61+
'../../src/integration.spec.js'
62+
]
63+
const tests = [
64+
{ path: '/workspace/dd-trace-js/src/unit.spec.js' },
65+
{ path: '/workspace/dd-trace-js/src/integration.spec.js' },
66+
{ path: '/workspace/dd-trace-js/src/e2e.spec.js' }
67+
]
68+
const rootDir = '/workspace/dd-trace-js/config/root-config'
69+
70+
const filteredSuites = getJestSuitesToRun(skippableSuites, tests, rootDir)
71+
expect(filteredSuites).to.eql([
72+
{ path: '/workspace/dd-trace-js/src/e2e.spec.js' }
73+
])
74+
})
75+
})

0 commit comments

Comments
 (0)