Skip to content

Commit 4bc0a8f

Browse files
rluvatonUlisesGascon
authored andcommitted
test_runner: fix global after not failing the tests
PR-URL: #48913 Backport-PR-URL: #49225Fixes: #48867 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent 08738b2 commit 4bc0a8f

6 files changed

Lines changed: 103 additions & 34 deletions

File tree

‎lib/internal/test_runner/test.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,14 @@ class Test extends AsyncResource {
688688
this.parent.processReadySubtestRange(false);
689689
this.parent.processPendingSubtests();
690690
}elseif(!this.reported){
691+
if(!this.passed&&failed===0&&this.error){
692+
this.reporter.fail(0,kFilename,this.subtests.length+1,kFilename,{
693+
__proto__: null,
694+
duration_ms: this.#duration(),
695+
error: this.error,
696+
},undefined);
697+
}
698+
691699
this.reported=true;
692700
this.reporter.plan(this.nesting,kFilename,this.root.harness.counters.topLevel);
693701

‎test/common/assertSnapshot.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ function replaceWindowsPaths(str) {
2020
returncommon.isWindows ? str.replaceAll(path.win32.sep,path.posix.sep) : str;
2121
}
2222

23+
functionreplaceFullPaths(str){
24+
returnstr.replaceAll(process.cwd(),'');
25+
}
26+
2327
functiontransform(...args){
2428
return(str)=>args.reduce((acc,fn)=>fn(acc),str);
2529
}
@@ -79,6 +83,7 @@ async function spawnAndAssert(filename, transform = (x) => x, { tty = false, ...
7983
module.exports={
8084
assertSnapshot,
8185
getSnapshotPath,
86+
replaceFullPaths,
8287
replaceStackTrace,
8388
replaceWindowsLineEndings,
8489
replaceWindowsPaths,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
const{ it, after }=require('node:test');
3+
4+
after(()=>{
5+
thrownewError('this should fail the test')
6+
});
7+
8+
it('this is a test',()=>{
9+
console.log('this is a test')
10+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
this is a test
2+
TAP version 13
3+
# Subtest: this is a test
4+
ok 1 - this is a test
5+
---
6+
duration_ms: *
7+
...
8+
not ok 2 - /test/fixtures/test-runner/output/global_after_should_fail_the_test.js
9+
---
10+
duration_ms: *
11+
failureType: 'hookFailed'
12+
error: 'this should fail the test'
13+
code: 'ERR_TEST_FAILURE'
14+
stack: |-
15+
*
16+
*
17+
*
18+
*
19+
*
20+
*
21+
*
22+
*
23+
*
24+
*
25+
...
26+
1..1
27+
# tests 1
28+
# suites 0
29+
# pass 1
30+
# fail 0
31+
# cancelled 0
32+
# skipped 0
33+
# todo 0
34+
# duration_ms *

‎test/fixtures/test-runner/output/hooks-with-no-global-test.js‎

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,36 @@ before(() => testArr.push('global before'));
99
after(()=>{
1010
testArr.push('global after');
1111

12-
try{
13-
assert.deepStrictEqual(testArr,[
14-
'global before',
15-
'describe before',
12+
assert.deepStrictEqual(testArr,[
13+
'global before',
14+
'describe before',
1615

17-
'describe beforeEach',
18-
'describe it 1',
19-
'describe afterEach',
16+
'describe beforeEach',
17+
'describe it 1',
18+
'describe afterEach',
2019

21-
'describe beforeEach',
22-
'describe test 2',
23-
'describe afterEach',
20+
'describe beforeEach',
21+
'describe test 2',
22+
'describe afterEach',
2423

25-
'describe nested before',
24+
'describe nested before',
2625

27-
'describe beforeEach',
28-
'describe nested beforeEach',
29-
'describe nested it 1',
30-
'describe afterEach',
31-
'describe nested afterEach',
26+
'describe beforeEach',
27+
'describe nested beforeEach',
28+
'describe nested it 1',
29+
'describe afterEach',
30+
'describe nested afterEach',
3231

33-
'describe beforeEach',
34-
'describe nested beforeEach',
35-
'describe nested test 2',
36-
'describe afterEach',
37-
'describe nested afterEach',
32+
'describe beforeEach',
33+
'describe nested beforeEach',
34+
'describe nested test 2',
35+
'describe afterEach',
36+
'describe nested afterEach',
3837

39-
'describe nested after',
40-
'describe after',
41-
'global after',
42-
]);
43-
}catch(e){
44-
// TODO(rluvaton): remove the try catch after #48867 is fixed
45-
console.error(e);
46-
process.exit(1);
47-
}
38+
'describe nested after',
39+
'describe after',
40+
'global after',
41+
]);
4842
});
4943

5044
describe('describe hooks with no global tests',()=>{

‎test/parallel/test-runner-output.mjs‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,27 @@ function replaceSpecDuration(str) {
2424
.replaceAll(/duration_ms[0-9.]+/g,'duration_ms *')
2525
.replace(stackTraceBasePath,'$3');
2626
}
27-
constdefaultTransform=snapshot
28-
.transform(snapshot.replaceWindowsLineEndings,snapshot.replaceStackTrace,replaceTestDuration);
29-
constspecTransform=snapshot
30-
.transform(replaceSpecDuration,snapshot.replaceWindowsLineEndings,snapshot.replaceStackTrace);
27+
28+
functionremoveWindowsPathEscaping(str){
29+
returncommon.isWindows ? str.replaceAll(/\\\\/g,'\\') : str;
30+
}
31+
32+
constdefaultTransform=snapshot.transform(
33+
snapshot.replaceWindowsLineEndings,
34+
snapshot.replaceStackTrace,
35+
replaceTestDuration,
36+
);
37+
constspecTransform=snapshot.transform(
38+
replaceSpecDuration,
39+
snapshot.replaceWindowsLineEndings,
40+
snapshot.replaceStackTrace,
41+
);
42+
constwithFileNameTransform=snapshot.transform(
43+
defaultTransform,
44+
removeWindowsPathEscaping,
45+
snapshot.replaceFullPaths,
46+
snapshot.replaceWindowsPaths,
47+
);
3148

3249

3350
consttests=[
@@ -41,6 +58,7 @@ const tests = [
4158
{name: 'test-runner/output/hooks-with-no-global-test.js'},
4259
{name: 'test-runner/output/before-and-after-each-too-many-listeners.js'},
4360
{name: 'test-runner/output/before-and-after-each-with-timeout-too-many-listeners.js'},
61+
{name: 'test-runner/output/global_after_should_fail_the_test.js',transform: withFileNameTransform},
4462
{name: 'test-runner/output/no_refs.js'},
4563
{name: 'test-runner/output/no_tests.js'},
4664
{name: 'test-runner/output/only_tests.js'},

0 commit comments

Comments
 (0)