Skip to content

Commit 79f4b42

Browse files
MoLowjuanarbol
authored andcommitted
test_runner: report file in test runner events
PR-URL: #46030 Backport-PR-URL: #46839 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 403df21 commit 79f4b42

File tree

5 files changed

+47
-28
lines changed

5 files changed

+47
-28
lines changed

doc/api/test.md

+10
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,8 @@ object, streaming a series of events representing the execution of the tests.
12041204
### Event: `'test:diagnostic'`
12051205

12061206
* `data` {Object}
1207+
* `file` {string|undefined} The path of the test file,
1208+
undefined if test is not ran through a file.
12071209
* `message` {string} The diagnostic message.
12081210
* `nesting` {number} The nesting level of the test.
12091211

@@ -1215,6 +1217,8 @@ Emitted when [`context.diagnostic`][] is called.
12151217
* `details` {Object} Additional execution metadata.
12161218
* `duration` {number} The duration of the test in milliseconds.
12171219
* `error` {Error} The error thrown by the test.
1220+
* `file` {string|undefined} The path of the test file,
1221+
undefined if test is not ran through a file.
12181222
* `name` {string} The test name.
12191223
* `nesting` {number} The nesting level of the test.
12201224
* `testNumber` {number} The ordinal number of the test.
@@ -1228,6 +1232,8 @@ Emitted when a test fails.
12281232
* `data` {Object}
12291233
* `details` {Object} Additional execution metadata.
12301234
* `duration` {number} The duration of the test in milliseconds.
1235+
* `file` {string|undefined} The path of the test file,
1236+
undefined if test is not ran through a file.
12311237
* `name` {string} The test name.
12321238
* `nesting` {number} The nesting level of the test.
12331239
* `testNumber` {number} The ordinal number of the test.
@@ -1239,6 +1245,8 @@ Emitted when a test passes.
12391245
### Event: `'test:plan'`
12401246

12411247
* `data` {Object}
1248+
* `file` {string|undefined} The path of the test file,
1249+
undefined if test is not ran through a file.
12421250
* `nesting` {number} The nesting level of the test.
12431251
* `count` {number} The number of subtests that have ran.
12441252

@@ -1247,6 +1255,8 @@ Emitted when all subtests have completed for a given test.
12471255
### Event: `'test:start'`
12481256

12491257
* `data` {Object}
1258+
* `file` {string|undefined} The path of the test file,
1259+
undefined if test is not ran through a file.
12501260
* `name` {string} The test name.
12511261
* `nesting` {number} The nesting level of the test.
12521262

lib/internal/test_runner/runner.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ class FileTest extends Test {
139139
break;
140140

141141
case TokenKind.TAP_PLAN:
142-
this.reporter.plan(nesting, node.end - node.start + 1);
142+
this.reporter.plan(nesting, this.name, node.end - node.start + 1);
143143
break;
144144

145145
case TokenKind.TAP_SUBTEST_POINT:
146-
this.reporter.start(nesting, node.name);
146+
this.reporter.start(nesting, this.name, node.name);
147147
break;
148148

149149
case TokenKind.TAP_TEST_POINT:
@@ -163,6 +163,7 @@ class FileTest extends Test {
163163
if (pass) {
164164
this.reporter.ok(
165165
nesting,
166+
this.name,
166167
node.id,
167168
node.description,
168169
YAMLToJs(node.diagnostics),
@@ -171,6 +172,7 @@ class FileTest extends Test {
171172
} else {
172173
this.reporter.fail(
173174
nesting,
175+
this.name,
174176
node.id,
175177
node.description,
176178
YAMLToJs(node.diagnostics),
@@ -184,11 +186,11 @@ class FileTest extends Test {
184186
// Ignore file top level diagnostics
185187
break;
186188
}
187-
this.reporter.diagnostic(nesting, node.comment);
189+
this.reporter.diagnostic(nesting, this.name, node.comment);
188190
break;
189191

190192
case TokenKind.UNKNOWN:
191-
this.reporter.diagnostic(nesting, node.value);
193+
this.reporter.diagnostic(nesting, this.name, node.value);
192194
break;
193195
}
194196
}

lib/internal/test_runner/test.js

+15-14
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

+10-10
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

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
const assert = require('assert');
2+
const path = require('path');
3+
14
module.exports = async function * customReporter(source) {
25
const counters = {};
36
for await (const event of source) {
7+
if (event.data.file) {
8+
assert.strictEqual(event.data.file, path.resolve(__dirname, '../reporters.js'));
9+
}
410
counters[event.type] = (counters[event.type] ?? 0) + 1;
511
}
612
yield "custom.js ";

0 commit comments

Comments
 (0)