Skip to content

Commit 36773a2

Browse files
cjihrigCeres6
authored andcommitted
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#48975 Fixes: nodejs#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 722d36b commit 36773a2

27 files changed

+637
-273
lines changed

doc/api/test.md

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

20362036
* `data` {Object}
2037+
* `column` {number|undefined} The column number where the test is defined, or
2038+
`undefined` if the test was run through the REPL.
20372039
* `file` {string|undefined} The path of the test file,
20382040
`undefined` if test was run through the REPL.
2041+
* `line` {number|undefined} The line number where the test is defined, or
2042+
`undefined` if the test was run through the REPL.
20392043
* `name` {string} The test name.
20402044
* `nesting` {number} The nesting level of the test.
20412045

@@ -2044,8 +2048,12 @@ Emitted when a test is dequeued, right before it is executed.
20442048
### Event: `'test:diagnostic'`
20452049

20462050
* `data` {Object}
2051+
* `column` {number|undefined} The column number where the test is defined, or
2052+
`undefined` if the test was run through the REPL.
20472053
* `file` {string|undefined} The path of the test file,
20482054
`undefined` if test was run through the REPL.
2055+
* `line` {number|undefined} The line number where the test is defined, or
2056+
`undefined` if the test was run through the REPL.
20492057
* `message` {string} The diagnostic message.
20502058
* `nesting` {number} The nesting level of the test.
20512059

@@ -2054,8 +2062,12 @@ Emitted when [`context.diagnostic`][] is called.
20542062
### Event: `'test:enqueue'`
20552063

20562064
* `data` {Object}
2065+
* `column` {number|undefined} The column number where the test is defined, or
2066+
`undefined` if the test was run through the REPL.
20572067
* `file` {string|undefined} The path of the test file,
20582068
`undefined` if test was run through the REPL.
2069+
* `line` {number|undefined} The line number where the test is defined, or
2070+
`undefined` if the test was run through the REPL.
20592071
* `name` {string} The test name.
20602072
* `nesting` {number} The nesting level of the test.
20612073

@@ -2064,6 +2076,8 @@ Emitted when a test is enqueued for execution.
20642076
### Event: `'test:fail'`
20652077

20662078
* `data` {Object}
2079+
* `column` {number|undefined} The column number where the test is defined, or
2080+
`undefined` if the test was run through the REPL.
20672081
* `details` {Object} Additional execution metadata.
20682082
* `duration_ms` {number} The duration of the test in milliseconds.
20692083
* `error` {Error} An error wrapping the error thrown by the test.
@@ -2072,6 +2086,8 @@ Emitted when a test is enqueued for execution.
20722086
this is a suite.
20732087
* `file` {string|undefined} The path of the test file,
20742088
`undefined` if test was run through the REPL.
2089+
* `line` {number|undefined} The line number where the test is defined, or
2090+
`undefined` if the test was run through the REPL.
20752091
* `name` {string} The test name.
20762092
* `nesting` {number} The nesting level of the test.
20772093
* `testNumber` {number} The ordinal number of the test.
@@ -2083,12 +2099,16 @@ Emitted when a test fails.
20832099
### Event: `'test:pass'`
20842100

20852101
* `data` {Object}
2102+
* `column` {number|undefined} The column number where the test is defined, or
2103+
`undefined` if the test was run through the REPL.
20862104
* `details` {Object} Additional execution metadata.
20872105
* `duration_ms` {number} The duration of the test in milliseconds.
20882106
* `type` {string|undefined} The type of the test, used to denote whether
20892107
this is a suite.
20902108
* `file` {string|undefined} The path of the test file,
20912109
`undefined` if test was run through the REPL.
2110+
* `line` {number|undefined} The line number where the test is defined, or
2111+
`undefined` if the test was run through the REPL.
20922112
* `name` {string} The test name.
20932113
* `nesting` {number} The nesting level of the test.
20942114
* `testNumber` {number} The ordinal number of the test.
@@ -2100,8 +2120,12 @@ Emitted when a test passes.
21002120
### Event: `'test:plan'`
21012121

21022122
* `data` {Object}
2123+
* `column` {number|undefined} The column number where the test is defined, or
2124+
`undefined` if the test was run through the REPL.
21032125
* `file` {string|undefined} The path of the test file,
21042126
`undefined` if test was run through the REPL.
2127+
* `line` {number|undefined} The line number where the test is defined, or
2128+
`undefined` if the test was run through the REPL.
21052129
* `nesting` {number} The nesting level of the test.
21062130
* `count` {number} The number of subtests that have ran.
21072131

@@ -2110,8 +2134,12 @@ Emitted when all subtests have completed for a given test.
21102134
### Event: `'test:start'`
21112135

21122136
* `data` {Object}
2137+
* `column` {number|undefined} The column number where the test is defined, or
2138+
`undefined` if the test was run through the REPL.
21132139
* `file` {string|undefined} The path of the test file,
21142140
`undefined` if test was run through the REPL.
2141+
* `line` {number|undefined} The line number where the test is defined, or
2142+
`undefined` if the test was run through the REPL.
21152143
* `name` {string} The test name.
21162144
* `nesting` {number} The nesting level of the test.
21172145

@@ -2122,7 +2150,11 @@ defined.
21222150
### Event: `'test:stderr'`
21232151

21242152
* `data` {Object}
2153+
* `column` {number|undefined} The column number where the test is defined, or
2154+
`undefined` if the test was run through the REPL.
21252155
* `file` {string} The path of the test file.
2156+
* `line` {number|undefined} The line number where the test is defined, or
2157+
`undefined` if the test was run through the REPL.
21262158
* `message` {string} The message written to `stderr`.
21272159

21282160
Emitted when a running test writes to `stderr`.
@@ -2131,7 +2163,11 @@ This event is only emitted if `--test` flag is passed.
21312163
### Event: `'test:stdout'`
21322164

21332165
* `data` {Object}
2166+
* `column` {number|undefined} The column number where the test is defined, or
2167+
`undefined` if the test was run through the REPL.
21342168
* `file` {string} The path of the test file.
2169+
* `line` {number|undefined} The line number where the test is defined, or
2170+
`undefined` if the test was run through the REPL.
21352171
* `message` {string} The message written to `stdout`.
21362172

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

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,
@@ -217,17 +218,38 @@ function runInParentContext(Factory) {
217218
return PromiseResolve();
218219
}
219220

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

227243
function hook(hook) {
228244
return (fn, options) => {
229245
const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
230-
parent.createHook(hook, fn, options);
246+
parent.createHook(hook, fn, {
247+
__proto__: null,
248+
...options,
249+
parent,
250+
hookType: hook,
251+
loc: getCallerLocation(),
252+
});
231253
};
232254
}
233255

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
}

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)