test_runner: report `file` in test runner events · nodejs/node@a49e17e · GitHub
Skip to content

Commit a49e17e

Browse files
MoLowruyadorno
authored andcommitted
test_runner: report file in test runner events
Backport-PR-URL: #46361 PR-URL: #46030 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 3a3a6d8 commit a49e17e

5 files changed

Lines changed: 47 additions & 28 deletions

File tree

doc/api/test.md

Lines changed: 10 additions & 0 deletions

lib/internal/test_runner/runner.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ class FileTest extends Test {
140140
break;
141141

142142
case TokenKind.TAP_PLAN:
143-
this.reporter.plan(nesting, node.end - node.start + 1);
143+
this.reporter.plan(nesting, this.name, node.end - node.start + 1);
144144
break;
145145

146146
case TokenKind.TAP_SUBTEST_POINT:
147-
this.reporter.start(nesting, node.name);
147+
this.reporter.start(nesting, this.name, node.name);
148148
break;
149149

150150
case TokenKind.TAP_TEST_POINT:
@@ -164,6 +164,7 @@ class FileTest extends Test {
164164
if (pass) {
165165
this.reporter.ok(
166166
nesting,
167+
this.name,
167168
node.id,
168169
node.description,
169170
YAMLToJs(node.diagnostics),
@@ -172,6 +173,7 @@ class FileTest extends Test {
172173
} else {
173174
this.reporter.fail(
174175
nesting,
176+
this.name,
175177
node.id,
176178
node.description,
177179
YAMLToJs(node.diagnostics),
@@ -185,11 +187,11 @@ class FileTest extends Test {
185187
// Ignore file top level diagnostics
186188
break;
187189
}
188-
this.reporter.diagnostic(nesting, node.comment);
190+
this.reporter.diagnostic(nesting, this.name, node.comment);
189191
break;
190192

191193
case TokenKind.UNKNOWN:
192-
this.reporter.diagnostic(nesting, node.value);
194+
this.reporter.diagnostic(nesting, this.name, node.value);
193195
break;
194196
}
195197
}

lib/internal/test_runner/test.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const testNamePatterns = testNamePatternFlag?.length > 0 ?
7474
(re) => convertStringToRegExp(re, '--test-name-pattern')
7575
) : null;
7676
const kShouldAbort = Symbol('kShouldAbort');
77+
const kFilename = process.argv?.[1];
7778
const kHookNames = ObjectSeal(['before', 'after', 'beforeEach', 'afterEach']);
7879
const kUnwrapErrors = new SafeSet()
7980
.add(kTestCodeFailure).add(kHookFailure)
@@ -632,19 +633,19 @@ class Test extends AsyncResource {
632633
this.parent.processPendingSubtests();
633634
} else if (!this.reported) {
634635
this.reported = true;
635-
this.reporter.plan(this.nesting, this.subtests.length);
636+
this.reporter.plan(this.nesting, kFilename, this.subtests.length);
636637

637638
for (let i = 0; i < this.diagnostics.length; i++) {
638-
this.reporter.diagnostic(this.nesting, this.diagnostics[i]);
639+
this.reporter.diagnostic(this.nesting, kFilename, this.diagnostics[i]);
639640
}
640641

641-
this.reporter.diagnostic(this.nesting, `tests ${this.subtests.length}`);
642-
this.reporter.diagnostic(this.nesting, `pass ${counters.passed}`);
643-
this.reporter.diagnostic(this.nesting, `fail ${counters.failed}`);
644-
this.reporter.diagnostic(this.nesting, `cancelled ${counters.cancelled}`);
645-
this.reporter.diagnostic(this.nesting, `skipped ${counters.skipped}`);
646-
this.reporter.diagnostic(this.nesting, `todo ${counters.todo}`);
647-
this.reporter.diagnostic(this.nesting, `duration_ms ${this.#duration()}`);
642+
this.reporter.diagnostic(this.nesting, kFilename, `tests ${this.subtests.length}`);
643+
this.reporter.diagnostic(this.nesting, kFilename, `pass ${counters.passed}`);
644+
this.reporter.diagnostic(this.nesting, kFilename, `fail ${counters.failed}`);
645+
this.reporter.diagnostic(this.nesting, kFilename, `cancelled ${counters.cancelled}`);
646+
this.reporter.diagnostic(this.nesting, kFilename, `skipped ${counters.skipped}`);
647+
this.reporter.diagnostic(this.nesting, kFilename, `todo ${counters.todo}`);
648+
this.reporter.diagnostic(this.nesting, kFilename, `duration_ms ${this.#duration()}`);
648649
this.reporter.push(null);
649650
}
650651
}
@@ -680,7 +681,7 @@ class Test extends AsyncResource {
680681

681682
report() {
682683
if (this.subtests.length > 0) {
683-
this.reporter.plan(this.subtests[0].nesting, this.subtests.length);
684+
this.reporter.plan(this.subtests[0].nesting, kFilename, this.subtests.length);
684685
} else {
685686
this.reportStarted();
686687
}
@@ -694,14 +695,14 @@ class Test extends AsyncResource {
694695
}
695696

696697
if (this.passed) {
697-
this.reporter.ok(this.nesting, this.testNumber, this.name, details, directive);
698+
this.reporter.ok(this.nesting, kFilename, this.testNumber, this.name, details, directive);
698699
} else {
699700
details.error = this.error;
700-
this.reporter.fail(this.nesting, this.testNumber, this.name, details, directive);
701+
this.reporter.fail(this.nesting, kFilename, this.testNumber, this.name, details, directive);
701702
}
702703

703704
for (let i = 0; i < this.diagnostics.length; i++) {
704-
this.reporter.diagnostic(this.nesting, this.diagnostics[i]);
705+
this.reporter.diagnostic(this.nesting, kFilename, this.diagnostics[i]);
705706
}
706707
}
707708

@@ -711,7 +712,7 @@ class Test extends AsyncResource {
711712
}
712713
this.#reportedSubtest = true;
713714
this.parent.reportStarted();
714-
this.reporter.start(this.nesting, this.name);
715+
this.reporter.start(this.nesting, kFilename, this.name);
715716
}
716717
}
717718

lib/internal/test_runner/tests_stream.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ class TestsStream extends Readable {
2727
}
2828
}
2929

30-
fail(nesting, testNumber, name, details, directive) {
31-
this.#emit('test:fail', { __proto__: null, name, nesting, testNumber, details, ...directive });
30+
fail(nesting, file, testNumber, name, details, directive) {
31+
this.#emit('test:fail', { __proto__: null, name, nesting, file, testNumber, details, ...directive });
3232
}
3333

34-
ok(nesting, testNumber, name, details, directive) {
35-
this.#emit('test:pass', { __proto__: null, name, nesting, testNumber, details, ...directive });
34+
ok(nesting, file, testNumber, name, details, directive) {
35+
this.#emit('test:pass', { __proto__: null, name, nesting, file, testNumber, details, ...directive });
3636
}
3737

38-
plan(nesting, count) {
39-
this.#emit('test:plan', { __proto__: null, nesting, count });
38+
plan(nesting, file, count) {
39+
this.#emit('test:plan', { __proto__: null, nesting, file, count });
4040
}
4141

4242
getSkip(reason = undefined) {
@@ -47,12 +47,12 @@ class TestsStream extends Readable {
4747
return { __proto__: null, todo: reason ?? true };
4848
}
4949

50-
start(nesting, name) {
51-
this.#emit('test:start', { __proto__: null, nesting, name });
50+
start(nesting, file, name) {
51+
this.#emit('test:start', { __proto__: null, nesting, file, name });
5252
}
5353

54-
diagnostic(nesting, message) {
55-
this.#emit('test:diagnostic', { __proto__: null, nesting, message });
54+
diagnostic(nesting, file, message) {
55+
this.#emit('test:diagnostic', { __proto__: null, nesting, file, message });
5656
}
5757

5858
#emit(type, data) {

test/fixtures/test-runner/custom_reporters/custom.js

Lines changed: 6 additions & 0 deletions

0 commit comments

Comments
 (0)