Skip to content

Commit

Permalink
test_runner: expose location of tests
Browse files Browse the repository at this point in the history
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: #48975
Fixes: #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>
  • Loading branch information
cjihrig authored and targos committed Nov 27, 2023
1 parent 00c70e1 commit d61a505
Show file tree
Hide file tree
Showing 27 changed files with 637 additions and 273 deletions.
36 changes: 36 additions & 0 deletions doc/api/test.md
Expand Up @@ -1984,8 +1984,12 @@ Emitted when code coverage is enabled and all tests have completed.
### Event: `'test:dequeue'`

* `data` {Object}
* `column` {number|undefined} The column number where the test is defined, or
`undefined` if the test was run through the REPL.
* `file` {string|undefined} The path of the test file,
`undefined` if test was run through the REPL.
* `line` {number|undefined} The line number where the test is defined, or
`undefined` if the test was run through the REPL.
* `name` {string} The test name.
* `nesting` {number} The nesting level of the test.

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

* `data` {Object}
* `column` {number|undefined} The column number where the test is defined, or
`undefined` if the test was run through the REPL.
* `file` {string|undefined} The path of the test file,
`undefined` if test was run through the REPL.
* `line` {number|undefined} The line number where the test is defined, or
`undefined` if the test was run through the REPL.
* `message` {string} The diagnostic message.
* `nesting` {number} The nesting level of the test.

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

* `data` {Object}
* `column` {number|undefined} The column number where the test is defined, or
`undefined` if the test was run through the REPL.
* `file` {string|undefined} The path of the test file,
`undefined` if test was run through the REPL.
* `line` {number|undefined} The line number where the test is defined, or
`undefined` if the test was run through the REPL.
* `name` {string} The test name.
* `nesting` {number} The nesting level of the test.

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

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

* `data` {Object}
* `column` {number|undefined} The column number where the test is defined, or
`undefined` if the test was run through the REPL.
* `details` {Object} Additional execution metadata.
* `duration_ms` {number} The duration of the test in milliseconds.
* `type` {string|undefined} The type of the test, used to denote whether
this is a suite.
* `file` {string|undefined} The path of the test file,
`undefined` if test was run through the REPL.
* `line` {number|undefined} The line number where the test is defined, or
`undefined` if the test was run through the REPL.
* `name` {string} The test name.
* `nesting` {number} The nesting level of the test.
* `testNumber` {number} The ordinal number of the test.
Expand All @@ -2050,8 +2070,12 @@ Emitted when a test passes.
### Event: `'test:plan'`

* `data` {Object}
* `column` {number|undefined} The column number where the test is defined, or
`undefined` if the test was run through the REPL.
* `file` {string|undefined} The path of the test file,
`undefined` if test was run through the REPL.
* `line` {number|undefined} The line number where the test is defined, or
`undefined` if the test was run through the REPL.
* `nesting` {number} The nesting level of the test.
* `count` {number} The number of subtests that have ran.

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

* `data` {Object}
* `column` {number|undefined} The column number where the test is defined, or
`undefined` if the test was run through the REPL.
* `file` {string|undefined} The path of the test file,
`undefined` if test was run through the REPL.
* `line` {number|undefined} The line number where the test is defined, or
`undefined` if the test was run through the REPL.
* `name` {string} The test name.
* `nesting` {number} The nesting level of the test.

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

* `data` {Object}
* `column` {number|undefined} The column number where the test is defined, or
`undefined` if the test was run through the REPL.
* `file` {string} The path of the test file.
* `line` {number|undefined} The line number where the test is defined, or
`undefined` if the test was run through the REPL.
* `message` {string} The message written to `stderr`.

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

* `data` {Object}
* `column` {number|undefined} The column number where the test is defined, or
`undefined` if the test was run through the REPL.
* `file` {string} The path of the test file.
* `line` {number|undefined} The line number where the test is defined, or
`undefined` if the test was run through the REPL.
* `message` {string} The message written to `stdout`.

Emitted when a running test writes to `stdout`.
Expand Down
28 changes: 25 additions & 3 deletions lib/internal/test_runner/harness.js
Expand Up @@ -5,6 +5,7 @@ const {
PromiseResolve,
SafeMap,
} = primordials;
const { getCallerLocation } = internalBinding('util');
const {
createHook,
executionAsyncId,
Expand Down Expand Up @@ -215,17 +216,38 @@ function runInParentContext(Factory) {
return PromiseResolve();
}

const test = (name, options, fn) => run(name, options, fn);
const test = (name, options, fn) => {
const overrides = {
__proto__: null,
loc: getCallerLocation(),
};

return run(name, options, fn, overrides);
};
ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => {
test[keyword] = (name, options, fn) => run(name, options, fn, { __proto__: null, [keyword]: true });
test[keyword] = (name, options, fn) => {
const overrides = {
__proto__: null,
[keyword]: true,
loc: getCallerLocation(),
};

return run(name, options, fn, overrides);
};
});
return test;
}

function hook(hook) {
return (fn, options) => {
const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
parent.createHook(hook, fn, options);
parent.createHook(hook, fn, {
__proto__: null,
...options,
parent,
hookType: hook,
loc: getCallerLocation(),
});
};
}

Expand Down
12 changes: 8 additions & 4 deletions lib/internal/test_runner/reporter/spec.js
Expand Up @@ -17,6 +17,7 @@ const { inspectWithNoCustomRetry } = require('internal/errors');
const { green, blue, red, white, gray, shouldColorize } = require('internal/util/colors');
const { kSubtestsFailed } = require('internal/test_runner/test');
const { getCoverageReport } = require('internal/test_runner/utils');
const { relative } = require('path');

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

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

constructor() {
super({ __proto__: null, writableObjectMode: true });
Expand Down Expand Up @@ -142,10 +144,12 @@ class SpecReporter extends Transform {
}
const results = [`\n${colors['test:fail']}${symbols['test:fail']}failing tests:${white}\n`];
for (let i = 0; i < this.#failedTests.length; i++) {
ArrayPrototypePush(results, this.#formatTestReport(
'test:fail',
this.#failedTests[i],
));
const test = this.#failedTests[i];
const relPath = relative(this.#cwd, test.file);
const formattedErr = this.#formatTestReport('test:fail', test);
const location = `test at ${relPath}:${test.line}:${test.column}`;

ArrayPrototypePush(results, location, formattedErr);
}
callback(null, ArrayPrototypeJoin(results, '\n'));
}
Expand Down
16 changes: 11 additions & 5 deletions lib/internal/test_runner/reporter/tap.js
Expand Up @@ -31,13 +31,14 @@ async function * tapReporter(source) {
yield `TAP version ${kDefaultTAPVersion}\n`;
for await (const { type, data } of source) {
switch (type) {
case 'test:fail':
case 'test:fail': {
yield reportTest(data.nesting, data.testNumber, 'not ok', data.name, data.skip, data.todo);
yield reportDetails(data.nesting, data.details);
const location = `${data.file}:${data.line}:${data.column}`;
yield reportDetails(data.nesting, data.details, location);
break;
case 'test:pass':
} case 'test:pass':
yield reportTest(data.nesting, data.testNumber, 'ok', data.name, data.skip, data.todo);
yield reportDetails(data.nesting, data.details);
yield reportDetails(data.nesting, data.details, null);
break;
case 'test:plan':
yield `${indent(data.nesting)}1..${data.count}\n`;
Expand Down Expand Up @@ -81,13 +82,18 @@ function reportTest(nesting, testNumber, status, name, skip, todo) {
return line;
}

function reportDetails(nesting, data = kEmptyObject) {
function reportDetails(nesting, data = kEmptyObject, location) {
const { error, duration_ms } = data;
const _indent = indent(nesting);
let details = `${_indent} ---\n`;

details += jsToYaml(_indent, 'duration_ms', duration_ms);
details += jsToYaml(_indent, 'type', data.type);

if (location) {
details += jsToYaml(_indent, 'location', location);
}

details += jsToYaml(_indent, null, error, new SafeSet());
details += `${_indent} ...\n`;
return details;
Expand Down

0 comments on commit d61a505

Please sign in to comment.