test_runner: report `entryFile` in `TestStream` events · nodejs/node@add1edb · GitHub
Skip to content

Commit add1edb

Browse files
MoLowaduh95
authored andcommitted
test_runner: report entryFile in TestStream events
Signed-off-by: Moshe Atlov <moshe@atlow.co.il> PR-URL: #64309 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent 949bc3c commit add1edb

7 files changed

Lines changed: 131 additions & 11 deletions

File tree

doc/api/test.md

Lines changed: 42 additions & 0 deletions

lib/internal/test_runner/runner.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ class FileTest extends Test {
295295
ArrayPrototypeIncludes(kDiagnosticsFilterArgs, StringPrototypeSlice(comment, 0, firstSpaceIndex));
296296
}
297297
#handleReportItem(item) {
298+
// The name is empty when a single child process runs all test files.
299+
if (this.name !== '') {
300+
item.data.entryFile = this.loc.file;
301+
}
298302
const isTopLevel = item.data.nesting === 0;
299303
if (isTopLevel) {
300304
if (item.type === 'test:plan' && this.#skipReporting()) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from 'node:test';
2+
import { runShared } from './helper.mjs';
3+
test('backup A', async (t) => { await runShared(t, 'A'); });
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from 'node:test';
2+
import { runShared } from './helper.mjs';
3+
test('backup B', async (t) => { await runShared(t, 'B'); });
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export async function runShared(t, target) {
2+
await t.test(`restore ${target}`, async () => {});
3+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import '../common/index.mjs';
2+
import * as fixtures from '../common/fixtures.mjs';
3+
import { describe, it, run } from 'node:test';
4+
import assert from 'node:assert';
5+
6+
const aPath = fixtures.path('test-runner', 'entry-file', 'a.test.mjs');
7+
const bPath = fixtures.path('test-runner', 'entry-file', 'b.test.mjs');
8+
const helperPath = fixtures.path('test-runner', 'entry-file', 'helper.mjs');
9+
10+
async function collectEvents(options) {
11+
const events = [];
12+
const stream = run({ files: [aPath, bPath], ...options });
13+
stream.on('test:fail', () => {});
14+
for await (const event of stream) {
15+
events.push(event);
16+
}
17+
return events;
18+
}
19+
20+
describe('entryFile attribution in reporter events', { concurrency: false }, () => {
21+
it('stamps entryFile on events forwarded from child processes', async () => {
22+
const events = await collectEvents({ isolation: 'process' });
23+
const checked = { __proto__: null, A: 0, B: 0 };
24+
25+
for (const { type, data } of events) {
26+
if (data?.name === 'restore A' || data?.name === 'restore B') {
27+
const target = data.name === 'restore A' ? 'A' : 'B';
28+
const expectedEntry = target === 'A' ? aPath : bPath;
29+
assert.strictEqual(data.file, helperPath,
30+
`${type} file should be the definition site`);
31+
assert.strictEqual(data.entryFile, expectedEntry,
32+
`${type} entryFile should be the entry file`);
33+
checked[target]++;
34+
}
35+
}
36+
37+
// Each subtest emits at least enqueue/dequeue/start/pass/complete.
38+
assert.ok(checked.A >= 4, `expected events for restore A, got ${checked.A}`);
39+
assert.ok(checked.B >= 4, `expected events for restore B, got ${checked.B}`);
40+
});
41+
42+
it('stamps entryFile on top-level tests forwarded from child processes', async () => {
43+
const events = await collectEvents({ isolation: 'process' });
44+
const pass = events.filter(({ type }) => type === 'test:pass');
45+
const backupA = pass.find(({ data }) => data.name === 'backup A');
46+
const backupB = pass.find(({ data }) => data.name === 'backup B');
47+
assert.strictEqual(backupA.data.entryFile, aPath);
48+
assert.strictEqual(backupB.data.entryFile, bPath);
49+
});
50+
51+
it('does not stamp entryFile with isolation none', async () => {
52+
const events = await collectEvents({ isolation: 'none' });
53+
for (const { data } of events) {
54+
if (data?.name === 'restore A' || data?.name === 'restore B') {
55+
assert.strictEqual(data.entryFile, undefined);
56+
}
57+
}
58+
});
59+
});

test/parallel/test-runner-v8-deserializer.mjs

Lines changed: 17 additions & 11 deletions

0 commit comments

Comments
 (0)