Skip to content

Commit e743e26

Browse files
committed
test_runner: print Bailout for spec reporter
1 parent 427661d commit e743e26

File tree

7 files changed

+96
-11
lines changed

7 files changed

+96
-11
lines changed

lib/internal/test_runner/reporter/spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class SpecReporter extends Transform {
7878
}
7979
#handleEvent({ type, data }) {
8080
switch (type) {
81+
case 'test:bail':
82+
return `${reporterColorMap['test:bail']}Bailing out! no new test files will be started!${colors.white}\n`;
8183
case 'test:fail':
8284
if (data.details?.error?.failureType !== kSubtestsFailed) {
8385
ArrayPrototypePush(this.#failedTests, data);

lib/internal/test_runner/reporter/utils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ const reporterColorMap = {
3737
get 'test:diagnostic'() {
3838
return colors.blue;
3939
},
40+
get 'test:bail'() {
41+
return colors.yellow;
42+
},
4043
get 'info'() {
4144
return colors.blue;
4245
},

lib/internal/test_runner/runner.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,6 @@ class FileTest extends Test {
257257
item.data.details.error = deserializeError(item.data.details.error);
258258
}
259259

260-
if (item.type === 'test:fail' && this.root.harness.bail && !this.root.harness.bailedOut) {
261-
// Trigger bail if enabled and this is the first failure
262-
this.root.harness.bailedOut = true;
263-
this.reporter[kEmitMessage]('test:bail', {
264-
__proto__: null,
265-
file: this.name,
266-
test: item.data,
267-
});
268-
}
269-
270260
if (item.type === 'test:pass' || item.type === 'test:fail') {
271261
item.data.testNumber = isTopLevel ? (this.root.harness.counters.topLevel + 1) : item.data.testNumber;
272262
countCompletedTest({
@@ -855,6 +845,18 @@ function run(options = kEmptyObject) {
855845
}
856846
};
857847

848+
root.reporter.on('test:fail', (item) => {
849+
if (root.harness.bail && !root.harness.bailedOut) {
850+
process.nextTick(() => {
851+
root.harness.bailedOut = true;
852+
root.reporter[kEmitMessage]('test:bail', {
853+
__proto__: null,
854+
file: item.name,
855+
test: item.data,
856+
});
857+
});
858+
}
859+
});
858860
} else {
859861
runFiles = () => {
860862
root.harness.bootstrapPromise = null;

test/fixtures/test-runner/bail/bail-test-2-fail.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ test('failing test 1', () => {
77
});
88

99
test('failing test 2', () => {
10-
assert.strictEqual(3, 4, 'This test should also fail but might not run');
10+
assert.strictEqual(3, 4, 'This test fails as well');
1111
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
const fixtures = require('../../../common/fixtures');
3+
const { spec } = require('node:test/reporters');
4+
const { run } = require('node:test');
5+
6+
const files = [
7+
fixtures.path('test-runner', 'bail', 'bail-test-1-pass.js'),
8+
fixtures.path('test-runner', 'bail', 'bail-test-2-fail.js'),
9+
fixtures.path('test-runner', 'bail', 'bail-test-3-pass.js'),
10+
fixtures.path('test-runner', 'bail', 'bail-test-4-pass.js'),
11+
];
12+
13+
run({ bail: true, concurrency: 1, files }).compose(spec).compose(process.stdout);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
✔ test 1 passes (*ms)
2+
✔ test 2 passes (*ms)
3+
✖ failing test 1 (*ms)
4+
Bailing out! no new test files will be started!
5+
✖ failing test 2 (*ms)
6+
ℹ tests 4
7+
ℹ suites 0
8+
ℹ pass 2
9+
ℹ fail 2
10+
ℹ cancelled 0
11+
ℹ skipped 0
12+
ℹ todo 0
13+
ℹ duration_ms *
14+
15+
✖ failing tests:
16+
17+
*
18+
✖ failing test 1 (*ms)
19+
AssertionError [ERR_ASSERTION]: This test should fail
20+
21+
1 !== 2
22+
23+
*
24+
*
25+
*
26+
*
27+
* {
28+
generatedMessage: false,
29+
code: 'ERR_ASSERTION',
30+
actual: 1,
31+
expected: 2,
32+
operator: 'strictEqual',
33+
diff: 'simple'
34+
}
35+
36+
*
37+
✖ failing test 2 (*ms)
38+
AssertionError [ERR_ASSERTION]: This test fails as well
39+
40+
3 !== 4
41+
42+
*
43+
*
44+
*
45+
*
46+
*
47+
*
48+
* {
49+
generatedMessage: false,
50+
code: 'ERR_ASSERTION',
51+
actual: 3,
52+
expected: 4,
53+
operator: 'strictEqual',
54+
diff: 'simple'
55+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Test that the output of test-runner/output/bail_concurrency_1.js matches test-runner/output/bail_concurrency_1.snapshot
2+
import '../common/index.mjs';
3+
import * as fixtures from '../common/fixtures.mjs';
4+
import { spawnAndAssert, specTransform, ensureCwdIsProjectRoot } from '../common/assertSnapshot.js';
5+
6+
ensureCwdIsProjectRoot();
7+
await spawnAndAssert(
8+
fixtures.path('test-runner/output/bail_concurrency_1.js'),
9+
specTransform,
10+
);

0 commit comments

Comments
 (0)