Skip to content

Commit 9eb363a

Browse files
authoredJan 2, 2023
test_runner: report file in test runner events
PR-URL: #46030 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 3a44f9a commit 9eb363a

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
@@ -1222,6 +1222,8 @@ object, streaming a series of events representing the execution of the tests.
12221222
### Event: `'test:diagnostic'`
12231223

12241224
* `data` {Object}
1225+
* `file` {string|undefined} The path of the test file,
1226+
undefined if test is not ran through a file.
12251227
* `message` {string} The diagnostic message.
12261228
* `nesting` {number} The nesting level of the test.
12271229

@@ -1233,6 +1235,8 @@ Emitted when [`context.diagnostic`][] is called.
12331235
* `details` {Object} Additional execution metadata.
12341236
* `duration` {number} The duration of the test in milliseconds.
12351237
* `error` {Error} The error thrown by the test.
1238+
* `file` {string|undefined} The path of the test file,
1239+
undefined if test is not ran through a file.
12361240
* `name` {string} The test name.
12371241
* `nesting` {number} The nesting level of the test.
12381242
* `testNumber` {number} The ordinal number of the test.
@@ -1246,6 +1250,8 @@ Emitted when a test fails.
12461250
* `data` {Object}
12471251
* `details` {Object} Additional execution metadata.
12481252
* `duration` {number} The duration of the test in milliseconds.
1253+
* `file` {string|undefined} The path of the test file,
1254+
undefined if test is not ran through a file.
12491255
* `name` {string} The test name.
12501256
* `nesting` {number} The nesting level of the test.
12511257
* `testNumber` {number} The ordinal number of the test.
@@ -1257,6 +1263,8 @@ Emitted when a test passes.
12571263
### Event: `'test:plan'`
12581264

12591265
* `data` {Object}
1266+
* `file` {string|undefined} The path of the test file,
1267+
undefined if test is not ran through a file.
12601268
* `nesting` {number} The nesting level of the test.
12611269
* `count` {number} The number of subtests that have ran.
12621270

@@ -1265,6 +1273,8 @@ Emitted when all subtests have completed for a given test.
12651273
### Event: `'test:start'`
12661274

12671275
* `data` {Object}
1276+
* `file` {string|undefined} The path of the test file,
1277+
undefined if test is not ran through a file.
12681278
* `name` {string} The test name.
12691279
* `nesting` {number} The nesting level of the test.
12701280

‎lib/internal/test_runner/runner.js

+6-4
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

+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)
Please sign in to comment.