Skip to content

Commit 10d6603

Browse files
committed
feat: report file in test runner events
PR-URL: nodejs/node#46030 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> (cherry picked from commit 9eb363a3e00dbba572756c7ed314273f17ea8e2e)
1 parent a64358a commit 10d6603

File tree

5 files changed

+50
-32
lines changed

5 files changed

+50
-32
lines changed

Diff for: README.md

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

11301130
* `data` {Object}
1131+
* `file` {string|undefined} The path of the test file,
1132+
undefined if test is not ran through a file.
11311133
* `message` {string} The diagnostic message.
11321134
* `nesting` {number} The nesting level of the test.
11331135

@@ -1139,6 +1141,8 @@ Emitted when [`context.diagnostic`][] is called.
11391141
* `details` {Object} Additional execution metadata.
11401142
* `duration` {number} The duration of the test in milliseconds.
11411143
* `error` {Error} The error thrown by the test.
1144+
* `file` {string|undefined} The path of the test file,
1145+
undefined if test is not ran through a file.
11421146
* `name` {string} The test name.
11431147
* `nesting` {number} The nesting level of the test.
11441148
* `testNumber` {number} The ordinal number of the test.
@@ -1152,6 +1156,8 @@ Emitted when a test fails.
11521156
* `data` {Object}
11531157
* `details` {Object} Additional execution metadata.
11541158
* `duration` {number} The duration of the test in milliseconds.
1159+
* `file` {string|undefined} The path of the test file,
1160+
undefined if test is not ran through a file.
11551161
* `name` {string} The test name.
11561162
* `nesting` {number} The nesting level of the test.
11571163
* `testNumber` {number} The ordinal number of the test.
@@ -1163,6 +1169,8 @@ Emitted when a test passes.
11631169
### Event: `'test:plan'`
11641170

11651171
* `data` {Object}
1172+
* `file` {string|undefined} The path of the test file,
1173+
undefined if test is not ran through a file.
11661174
* `nesting` {number} The nesting level of the test.
11671175
* `count` {number} The number of subtests that have ran.
11681176

@@ -1171,6 +1179,8 @@ Emitted when all subtests have completed for a given test.
11711179
### Event: `'test:start'`
11721180

11731181
* `data` {Object}
1182+
* `file` {string|undefined} The path of the test file,
1183+
undefined if test is not ran through a file.
11741184
* `name` {string} The test name.
11751185
* `nesting` {number} The nesting level of the test.
11761186

Diff for: lib/internal/test_runner/runner.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// https://github.com/nodejs/node/blob/a1b27b25bb01aadd3fd2714e4b136db11b7eb85a/lib/internal/test_runner/runner.js
1+
// https://github.com/nodejs/node/blob/9eb363a3e00dbba572756c7ed314273f17ea8e2e/lib/internal/test_runner/runner.js
22
'use strict'
33
const {
44
ArrayFrom,
@@ -133,11 +133,11 @@ class FileTest extends Test {
133133
break
134134

135135
case TokenKind.TAP_PLAN:
136-
this.reporter.plan(nesting, node.end - node.start + 1)
136+
this.reporter.plan(nesting, this.name, node.end - node.start + 1)
137137
break
138138

139139
case TokenKind.TAP_SUBTEST_POINT:
140-
this.reporter.start(nesting, node.name)
140+
this.reporter.start(nesting, this.name, node.name)
141141
break
142142

143143
case TokenKind.TAP_TEST_POINT:
@@ -157,6 +157,7 @@ class FileTest extends Test {
157157
if (pass) {
158158
this.reporter.ok(
159159
nesting,
160+
this.name,
160161
node.id,
161162
node.description,
162163
YAMLToJs(node.diagnostics),
@@ -165,6 +166,7 @@ class FileTest extends Test {
165166
} else {
166167
this.reporter.fail(
167168
nesting,
169+
this.name,
168170
node.id,
169171
node.description,
170172
YAMLToJs(node.diagnostics),
@@ -178,11 +180,11 @@ class FileTest extends Test {
178180
// Ignore file top level diagnostics
179181
break
180182
}
181-
this.reporter.diagnostic(nesting, node.comment)
183+
this.reporter.diagnostic(nesting, this.name, node.comment)
182184
break
183185

184186
case TokenKind.UNKNOWN:
185-
this.reporter.diagnostic(nesting, node.value)
187+
this.reporter.diagnostic(nesting, this.name, node.value)
186188
break
187189
}
188190
}

Diff for: lib/internal/test_runner/test.js

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// https://github.com/nodejs/node/blob/a1b27b25bb01aadd3fd2714e4b136db11b7eb85a/lib/internal/test_runner/test.js
1+
// https://github.com/nodejs/node/blob/9eb363a3e00dbba572756c7ed314273f17ea8e2e/lib/internal/test_runner/test.js
22

33
'use strict'
44

@@ -79,6 +79,7 @@ const testNamePatterns = testNamePatternFlag?.length > 0
7979
)
8080
: null
8181
const kShouldAbort = Symbol('kShouldAbort')
82+
const kFilename = process.argv?.[1]
8283
const kHookNames = ObjectSeal(['before', 'after', 'beforeEach', 'afterEach'])
8384
const kUnwrapErrors = new SafeSet()
8485
.add(kTestCodeFailure).add(kHookFailure)
@@ -622,19 +623,19 @@ class Test extends AsyncResource {
622623
this.parent.processPendingSubtests()
623624
} else if (!this.reported) {
624625
this.reported = true
625-
this.reporter.plan(this.nesting, this.subtests.length)
626+
this.reporter.plan(this.nesting, kFilename, this.subtests.length)
626627

627628
for (let i = 0; i < this.diagnostics.length; i++) {
628-
this.reporter.diagnostic(this.nesting, this.diagnostics[i])
629+
this.reporter.diagnostic(this.nesting, kFilename, this.diagnostics[i])
629630
}
630631

631-
this.reporter.diagnostic(this.nesting, `tests ${this.subtests.length}`)
632-
this.reporter.diagnostic(this.nesting, `pass ${counters.passed}`)
633-
this.reporter.diagnostic(this.nesting, `fail ${counters.failed}`)
634-
this.reporter.diagnostic(this.nesting, `cancelled ${counters.cancelled}`)
635-
this.reporter.diagnostic(this.nesting, `skipped ${counters.skipped}`)
636-
this.reporter.diagnostic(this.nesting, `todo ${counters.todo}`)
637-
this.reporter.diagnostic(this.nesting, `duration_ms ${this.#duration()}`)
632+
this.reporter.diagnostic(this.nesting, kFilename, `tests ${this.subtests.length}`)
633+
this.reporter.diagnostic(this.nesting, kFilename, `pass ${counters.passed}`)
634+
this.reporter.diagnostic(this.nesting, kFilename, `fail ${counters.failed}`)
635+
this.reporter.diagnostic(this.nesting, kFilename, `cancelled ${counters.cancelled}`)
636+
this.reporter.diagnostic(this.nesting, kFilename, `skipped ${counters.skipped}`)
637+
this.reporter.diagnostic(this.nesting, kFilename, `todo ${counters.todo}`)
638+
this.reporter.diagnostic(this.nesting, kFilename, `duration_ms ${this.#duration()}`)
638639
this.reporter.push(null)
639640
}
640641
}
@@ -670,7 +671,7 @@ class Test extends AsyncResource {
670671

671672
report () {
672673
if (this.subtests.length > 0) {
673-
this.reporter.plan(this.subtests[0].nesting, this.subtests.length)
674+
this.reporter.plan(this.subtests[0].nesting, kFilename, this.subtests.length)
674675
} else {
675676
this.reportStarted()
676677
}
@@ -684,14 +685,14 @@ class Test extends AsyncResource {
684685
}
685686

686687
if (this.passed) {
687-
this.reporter.ok(this.nesting, this.testNumber, this.name, details, directive)
688+
this.reporter.ok(this.nesting, kFilename, this.testNumber, this.name, details, directive)
688689
} else {
689690
details.error = this.error
690-
this.reporter.fail(this.nesting, this.testNumber, this.name, details, directive)
691+
this.reporter.fail(this.nesting, kFilename, this.testNumber, this.name, details, directive)
691692
}
692693

693694
for (let i = 0; i < this.diagnostics.length; i++) {
694-
this.reporter.diagnostic(this.nesting, this.diagnostics[i])
695+
this.reporter.diagnostic(this.nesting, kFilename, this.diagnostics[i])
695696
}
696697
}
697698

@@ -701,7 +702,7 @@ class Test extends AsyncResource {
701702
}
702703
this.#reportedSubtest = true
703704
this.parent.reportStarted()
704-
this.reporter.start(this.nesting, this.name)
705+
this.reporter.start(this.nesting, kFilename, this.name)
705706
}
706707
}
707708

Diff for: lib/internal/test_runner/tests_stream.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// https://github.com/nodejs/node/blob/a1b27b25bb01aadd3fd2714e4b136db11b7eb85a/lib/internal/test_runner/tests_stream.js
1+
// https://github.com/nodejs/node/blob/9eb363a3e00dbba572756c7ed314273f17ea8e2e/lib/internal/test_runner/tests_stream.js
22
'use strict'
33
const {
44
ArrayPrototypePush,
@@ -28,16 +28,16 @@ class TestsStream extends Readable {
2828
}
2929
}
3030

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

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

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

4343
getSkip (reason = undefined) {
@@ -48,12 +48,12 @@ class TestsStream extends Readable {
4848
return { __proto__: null, todo: reason ?? true }
4949
}
5050

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

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

5959
#emit (type, data) {

Diff for: test/fixtures/test-runner/custom_reporters/custom.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
// https://github.com/nodejs/node/blob/a1b27b25bb01aadd3fd2714e4b136db11b7eb85a/test/fixtures/test-runner/custom_reporters/custom.js
1+
// https://github.com/nodejs/node/blob/9eb363a3e00dbba572756c7ed314273f17ea8e2e/test/fixtures/test-runner/custom_reporters/custom.js
2+
const assert = require('assert')
3+
const path = require('path')
24
module.exports = async function * customReporter (source) {
35
const counters = {}
46
for await (const event of source) {
7+
if (event.data.file) {
8+
assert.strictEqual(event.data.file, path.resolve(__dirname, '../reporters.js'))
9+
}
510
counters[event.type] = (counters[event.type] ?? 0) + 1
611
}
712
yield 'custom.js '

0 commit comments

Comments
 (0)