Skip to content

Commit f6c929e

Browse files
[test optimization] Fix test suite error handling in jest (#6913)
1 parent b4d004d commit f6c929e

File tree

5 files changed

+23
-18
lines changed

5 files changed

+23
-18
lines changed
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
'use strict'
22

33
it('will fail', () => {
4-
import('./off-timing-import.js')
4+
setTimeout(() => {
5+
const sum = require('./off-timing-import.js')
6+
7+
expect(sum(1, 2)).toBe(3)
8+
}, 0)
59
})
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict'
22

3-
module.exports = {
4-
sum: (a, b) => a + b
3+
module.exports = function sum (a, b) {
4+
return a + b
55
}

integration-tests/ci-visibility/run-jest.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ if (process.env.DO_NOT_INJECT_GLOBALS) {
4141
options.injectGlobals = false
4242
}
4343

44+
if (process.env.WAIT_FOR_UNHANDLED_REJECTIONS) {
45+
options.waitForUnhandledRejections = true
46+
}
47+
4448
jest.runCLI(
4549
options,
4650
options.projects

integration-tests/jest/jest.spec.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,36 +1008,36 @@ describe(`jest@${JEST_VERSION} commonJS`, () => {
10081008
})
10091009

10101010
context('when using off timing imports', () => {
1011-
onlyLatestIt('reports test suite errors when using off timing import', async () => {
1011+
onlyLatestIt('reports test suite errors when waitForUnhandledRejections=true', async () => {
10121012
const eventsPromise = receiver
10131013
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
10141014
const events = payloads.flatMap(({ payload }) => payload.events)
10151015
const suites = events.filter(event => event.type === 'test_suite_end')
1016-
assert.equal(suites.length, 6)
1016+
assert.equal(suites.length, 3)
1017+
10171018
const failedTestSuites = suites.filter(
10181019
suite => suite.content.meta[TEST_SUITE] === 'ci-visibility/jest-bad-import/jest-bad-import-test.js'
10191020
)
1020-
assert.equal(failedTestSuites.length, 4)
1021-
failedTestSuites.forEach(suite => {
1022-
assert.equal(suite.content.meta[TEST_STATUS], 'fail')
1023-
assert.include(suite.content.meta[ERROR_MESSAGE], 'a file outside of the scope of the test code')
1024-
assert.equal(suite.content.meta[ERROR_TYPE], 'ReferenceError')
1025-
})
1021+
assert.equal(failedTestSuites.length, 1)
1022+
const [failedTestSuite] = failedTestSuites
1023+
1024+
assert.equal(failedTestSuite.content.meta[TEST_STATUS], 'fail')
1025+
assert.include(failedTestSuite.content.meta[ERROR_MESSAGE], 'a file outside of the scope of the test code')
1026+
assert.equal(failedTestSuite.content.meta[ERROR_TYPE], 'Error')
1027+
10261028
const passedTestSuites = suites.filter(
10271029
suite => suite.content.meta[TEST_STATUS] === 'pass'
10281030
)
10291031
assert.equal(passedTestSuites.length, 2)
10301032
})
10311033

1032-
const { NODE_OPTIONS, ...restEnvVars } = getCiVisAgentlessConfig(receiver.port)
10331034
childProcess = exec(runTestsCommand, {
10341035
cwd,
10351036
env: {
1036-
...restEnvVars,
1037-
// need --experimental-vm-modules to trigger the error
1038-
NODE_OPTIONS: `${NODE_OPTIONS} --experimental-vm-modules`,
1037+
...getCiVisAgentlessConfig(receiver.port),
10391038
TESTS_TO_RUN: 'jest-bad-import/jest-bad-import-test',
10401039
RUN_IN_PARALLEL: true,
1040+
WAIT_FOR_UNHANDLED_REJECTIONS: true
10411041
},
10421042
stdio: 'inherit'
10431043
})

packages/datadog-instrumentations/src/jest.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,9 +1345,6 @@ addHook({
13451345
}
13461346
}
13471347
return returnedValue
1348-
} catch (error) {
1349-
testSuiteErrorCh.publish({ error, testSuiteAbsolutePath: this._testPath })
1350-
throw error
13511348
} finally {
13521349
// Restore original prepareStackTrace
13531350
Error.prepareStackTrace = originalPrepareStackTrace

0 commit comments

Comments
 (0)