Skip to content

Commit db532e0

Browse files
committed
test_runner: expose location of tests
This commit adds each test's line and column number to the reporter output. This will aid in debugging test suite failures when error stacks are not helpful, test suites are large, or tests have the same name. This data is also exposed on the spec reporter. This commit also replaces the filename that was previously being reported, with the filename where the test actually exists. These are normally correct, but could be wrong if tests were run from a file other than the user's entrypoint. PR-URL: nodejs/node#48975 Fixes: nodejs/node#48457 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent 00d32a2 commit db532e0

27 files changed

+637
-273
lines changed

graal-nodejs/doc/api/test.md

+36
Original file line numberDiff line numberDiff line change
@@ -1984,8 +1984,12 @@ Emitted when code coverage is enabled and all tests have completed.
19841984
### Event: `'test:dequeue'`
19851985

19861986
* `data` {Object}
1987+
* `column` {number|undefined} The column number where the test is defined, or
1988+
`undefined` if the test was run through the REPL.
19871989
* `file` {string|undefined} The path of the test file,
19881990
`undefined` if test was run through the REPL.
1991+
* `line` {number|undefined} The line number where the test is defined, or
1992+
`undefined` if the test was run through the REPL.
19891993
* `name` {string} The test name.
19901994
* `nesting` {number} The nesting level of the test.
19911995

@@ -1994,8 +1998,12 @@ Emitted when a test is dequeued, right before it is executed.
19941998
### Event: `'test:diagnostic'`
19951999

19962000
* `data` {Object}
2001+
* `column` {number|undefined} The column number where the test is defined, or
2002+
`undefined` if the test was run through the REPL.
19972003
* `file` {string|undefined} The path of the test file,
19982004
`undefined` if test was run through the REPL.
2005+
* `line` {number|undefined} The line number where the test is defined, or
2006+
`undefined` if the test was run through the REPL.
19992007
* `message` {string} The diagnostic message.
20002008
* `nesting` {number} The nesting level of the test.
20012009

@@ -2004,8 +2012,12 @@ Emitted when [`context.diagnostic`][] is called.
20042012
### Event: `'test:enqueue'`
20052013

20062014
* `data` {Object}
2015+
* `column` {number|undefined} The column number where the test is defined, or
2016+
`undefined` if the test was run through the REPL.
20072017
* `file` {string|undefined} The path of the test file,
20082018
`undefined` if test was run through the REPL.
2019+
* `line` {number|undefined} The line number where the test is defined, or
2020+
`undefined` if the test was run through the REPL.
20092021
* `name` {string} The test name.
20102022
* `nesting` {number} The nesting level of the test.
20112023

@@ -2014,6 +2026,8 @@ Emitted when a test is enqueued for execution.
20142026
### Event: `'test:fail'`
20152027

20162028
* `data` {Object}
2029+
* `column` {number|undefined} The column number where the test is defined, or
2030+
`undefined` if the test was run through the REPL.
20172031
* `details` {Object} Additional execution metadata.
20182032
* `duration_ms` {number} The duration of the test in milliseconds.
20192033
* `error` {Error} An error wrapping the error thrown by the test.
@@ -2022,6 +2036,8 @@ Emitted when a test is enqueued for execution.
20222036
this is a suite.
20232037
* `file` {string|undefined} The path of the test file,
20242038
`undefined` if test was run through the REPL.
2039+
* `line` {number|undefined} The line number where the test is defined, or
2040+
`undefined` if the test was run through the REPL.
20252041
* `name` {string} The test name.
20262042
* `nesting` {number} The nesting level of the test.
20272043
* `testNumber` {number} The ordinal number of the test.
@@ -2033,12 +2049,16 @@ Emitted when a test fails.
20332049
### Event: `'test:pass'`
20342050

20352051
* `data` {Object}
2052+
* `column` {number|undefined} The column number where the test is defined, or
2053+
`undefined` if the test was run through the REPL.
20362054
* `details` {Object} Additional execution metadata.
20372055
* `duration_ms` {number} The duration of the test in milliseconds.
20382056
* `type` {string|undefined} The type of the test, used to denote whether
20392057
this is a suite.
20402058
* `file` {string|undefined} The path of the test file,
20412059
`undefined` if test was run through the REPL.
2060+
* `line` {number|undefined} The line number where the test is defined, or
2061+
`undefined` if the test was run through the REPL.
20422062
* `name` {string} The test name.
20432063
* `nesting` {number} The nesting level of the test.
20442064
* `testNumber` {number} The ordinal number of the test.
@@ -2050,8 +2070,12 @@ Emitted when a test passes.
20502070
### Event: `'test:plan'`
20512071

20522072
* `data` {Object}
2073+
* `column` {number|undefined} The column number where the test is defined, or
2074+
`undefined` if the test was run through the REPL.
20532075
* `file` {string|undefined} The path of the test file,
20542076
`undefined` if test was run through the REPL.
2077+
* `line` {number|undefined} The line number where the test is defined, or
2078+
`undefined` if the test was run through the REPL.
20552079
* `nesting` {number} The nesting level of the test.
20562080
* `count` {number} The number of subtests that have ran.
20572081

@@ -2060,8 +2084,12 @@ Emitted when all subtests have completed for a given test.
20602084
### Event: `'test:start'`
20612085

20622086
* `data` {Object}
2087+
* `column` {number|undefined} The column number where the test is defined, or
2088+
`undefined` if the test was run through the REPL.
20632089
* `file` {string|undefined} The path of the test file,
20642090
`undefined` if test was run through the REPL.
2091+
* `line` {number|undefined} The line number where the test is defined, or
2092+
`undefined` if the test was run through the REPL.
20652093
* `name` {string} The test name.
20662094
* `nesting` {number} The nesting level of the test.
20672095

@@ -2072,7 +2100,11 @@ defined.
20722100
### Event: `'test:stderr'`
20732101

20742102
* `data` {Object}
2103+
* `column` {number|undefined} The column number where the test is defined, or
2104+
`undefined` if the test was run through the REPL.
20752105
* `file` {string} The path of the test file.
2106+
* `line` {number|undefined} The line number where the test is defined, or
2107+
`undefined` if the test was run through the REPL.
20762108
* `message` {string} The message written to `stderr`.
20772109

20782110
Emitted when a running test writes to `stderr`.
@@ -2081,7 +2113,11 @@ This event is only emitted if `--test` flag is passed.
20812113
### Event: `'test:stdout'`
20822114

20832115
* `data` {Object}
2116+
* `column` {number|undefined} The column number where the test is defined, or
2117+
`undefined` if the test was run through the REPL.
20842118
* `file` {string} The path of the test file.
2119+
* `line` {number|undefined} The line number where the test is defined, or
2120+
`undefined` if the test was run through the REPL.
20852121
* `message` {string} The message written to `stdout`.
20862122

20872123
Emitted when a running test writes to `stdout`.

graal-nodejs/lib/internal/test_runner/harness.js

+25-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
PromiseResolve,
66
SafeMap,
77
} = primordials;
8+
const { getCallerLocation } = internalBinding('util');
89
const {
910
createHook,
1011
executionAsyncId,
@@ -215,17 +216,38 @@ function runInParentContext(Factory) {
215216
return PromiseResolve();
216217
}
217218

218-
const test = (name, options, fn) => run(name, options, fn);
219+
const test = (name, options, fn) => {
220+
const overrides = {
221+
__proto__: null,
222+
loc: getCallerLocation(),
223+
};
224+
225+
return run(name, options, fn, overrides);
226+
};
219227
ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => {
220-
test[keyword] = (name, options, fn) => run(name, options, fn, { __proto__: null, [keyword]: true });
228+
test[keyword] = (name, options, fn) => {
229+
const overrides = {
230+
__proto__: null,
231+
[keyword]: true,
232+
loc: getCallerLocation(),
233+
};
234+
235+
return run(name, options, fn, overrides);
236+
};
221237
});
222238
return test;
223239
}
224240

225241
function hook(hook) {
226242
return (fn, options) => {
227243
const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
228-
parent.createHook(hook, fn, options);
244+
parent.createHook(hook, fn, {
245+
__proto__: null,
246+
...options,
247+
parent,
248+
hookType: hook,
249+
loc: getCallerLocation(),
250+
});
229251
};
230252
}
231253

graal-nodejs/lib/internal/test_runner/reporter/spec.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const { inspectWithNoCustomRetry } = require('internal/errors');
1717
const { green, blue, red, white, gray, shouldColorize } = require('internal/util/colors');
1818
const { kSubtestsFailed } = require('internal/test_runner/test');
1919
const { getCoverageReport } = require('internal/test_runner/utils');
20+
const { relative } = require('path');
2021

2122
const inspectOptions = { __proto__: null, colors: shouldColorize(process.stdout), breakLength: Infinity };
2223

@@ -40,6 +41,7 @@ class SpecReporter extends Transform {
4041
#reported = [];
4142
#indentMemo = new SafeMap();
4243
#failedTests = [];
44+
#cwd = process.cwd();
4345

4446
constructor() {
4547
super({ __proto__: null, writableObjectMode: true });
@@ -142,10 +144,12 @@ class SpecReporter extends Transform {
142144
}
143145
const results = [`\n${colors['test:fail']}${symbols['test:fail']}failing tests:${white}\n`];
144146
for (let i = 0; i < this.#failedTests.length; i++) {
145-
ArrayPrototypePush(results, this.#formatTestReport(
146-
'test:fail',
147-
this.#failedTests[i],
148-
));
147+
const test = this.#failedTests[i];
148+
const relPath = relative(this.#cwd, test.file);
149+
const formattedErr = this.#formatTestReport('test:fail', test);
150+
const location = `test at ${relPath}:${test.line}:${test.column}`;
151+
152+
ArrayPrototypePush(results, location, formattedErr);
149153
}
150154
callback(null, ArrayPrototypeJoin(results, '\n'));
151155
}

graal-nodejs/lib/internal/test_runner/reporter/tap.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ async function * tapReporter(source) {
3131
yield `TAP version ${kDefaultTAPVersion}\n`;
3232
for await (const { type, data } of source) {
3333
switch (type) {
34-
case 'test:fail':
34+
case 'test:fail': {
3535
yield reportTest(data.nesting, data.testNumber, 'not ok', data.name, data.skip, data.todo);
36-
yield reportDetails(data.nesting, data.details);
36+
const location = `${data.file}:${data.line}:${data.column}`;
37+
yield reportDetails(data.nesting, data.details, location);
3738
break;
38-
case 'test:pass':
39+
} case 'test:pass':
3940
yield reportTest(data.nesting, data.testNumber, 'ok', data.name, data.skip, data.todo);
40-
yield reportDetails(data.nesting, data.details);
41+
yield reportDetails(data.nesting, data.details, null);
4142
break;
4243
case 'test:plan':
4344
yield `${indent(data.nesting)}1..${data.count}\n`;
@@ -81,13 +82,18 @@ function reportTest(nesting, testNumber, status, name, skip, todo) {
8182
return line;
8283
}
8384

84-
function reportDetails(nesting, data = kEmptyObject) {
85+
function reportDetails(nesting, data = kEmptyObject, location) {
8586
const { error, duration_ms } = data;
8687
const _indent = indent(nesting);
8788
let details = `${_indent} ---\n`;
8889

8990
details += jsToYaml(_indent, 'duration_ms', duration_ms);
9091
details += jsToYaml(_indent, 'type', data.type);
92+
93+
if (location) {
94+
details += jsToYaml(_indent, 'location', location);
95+
}
96+
9197
details += jsToYaml(_indent, null, error, new SafeSet());
9298
details += `${_indent} ...\n`;
9399
return details;

0 commit comments

Comments
 (0)