Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a49e17e

Browse filesBrowse 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
Copy full SHA for a49e17e

File tree

Expand file treeCollapse file tree

5 files changed

+47
-28
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+47
-28
lines changed
Open diff view settings
Collapse file

‎doc/api/test.md‎

Copy file name to clipboardExpand all lines: doc/api/test.md
+10Lines changed: 10 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,8 @@ object, streaming a series of events representing the execution of the tests.
12181218
### Event: `'test:diagnostic'`
12191219

12201220
* `data` {Object}
1221+
* `file` {string|undefined} The path of the test file,
1222+
undefined if test is not ran through a file.
12211223
* `message` {string} The diagnostic message.
12221224
* `nesting` {number} The nesting level of the test.
12231225

@@ -1229,6 +1231,8 @@ Emitted when [`context.diagnostic`][] is called.
12291231
* `details` {Object} Additional execution metadata.
12301232
* `duration` {number} The duration of the test in milliseconds.
12311233
* `error` {Error} The error thrown by the test.
1234+
* `file` {string|undefined} The path of the test file,
1235+
undefined if test is not ran through a file.
12321236
* `name` {string} The test name.
12331237
* `nesting` {number} The nesting level of the test.
12341238
* `testNumber` {number} The ordinal number of the test.
@@ -1242,6 +1246,8 @@ Emitted when a test fails.
12421246
* `data` {Object}
12431247
* `details` {Object} Additional execution metadata.
12441248
* `duration` {number} The duration of the test in milliseconds.
1249+
* `file` {string|undefined} The path of the test file,
1250+
undefined if test is not ran through a file.
12451251
* `name` {string} The test name.
12461252
* `nesting` {number} The nesting level of the test.
12471253
* `testNumber` {number} The ordinal number of the test.
@@ -1253,6 +1259,8 @@ Emitted when a test passes.
12531259
### Event: `'test:plan'`
12541260

12551261
* `data` {Object}
1262+
* `file` {string|undefined} The path of the test file,
1263+
undefined if test is not ran through a file.
12561264
* `nesting` {number} The nesting level of the test.
12571265
* `count` {number} The number of subtests that have ran.
12581266

@@ -1261,6 +1269,8 @@ Emitted when all subtests have completed for a given test.
12611269
### Event: `'test:start'`
12621270

12631271
* `data` {Object}
1272+
* `file` {string|undefined} The path of the test file,
1273+
undefined if test is not ran through a file.
12641274
* `name` {string} The test name.
12651275
* `nesting` {number} The nesting level of the test.
12661276

Collapse file

‎lib/internal/test_runner/runner.js‎

Copy file name to clipboardExpand all lines: lib/internal/test_runner/runner.js
+6-4Lines 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
}
Collapse file

‎lib/internal/test_runner/test.js‎

Copy file name to clipboardExpand all lines: lib/internal/test_runner/test.js
+15-14Lines 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

Collapse file

‎lib/internal/test_runner/tests_stream.js‎

Copy file name to clipboardExpand all lines: lib/internal/test_runner/tests_stream.js
+10-10Lines 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) {
Collapse file

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

Copy file name to clipboardExpand all lines: test/fixtures/test-runner/custom_reporters/custom.js
+6Lines changed: 6 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.