Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v20.x backport] backport multiple test runner related prs #49225

Closed
wants to merge 8 commits into from
36 changes: 36 additions & 0 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -2049,8 +2049,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 @@ -2059,8 +2063,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 @@ -2069,8 +2077,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 @@ -2079,6 +2091,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 @@ -2087,6 +2101,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 @@ -2098,12 +2114,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 @@ -2115,8 +2135,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 @@ -2125,8 +2149,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 @@ -2137,7 +2165,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 @@ -2146,7 +2178,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
36 changes: 29 additions & 7 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
PromiseResolve,
SafeMap,
} = primordials;
const { getCallerLocation } = internalBinding('util');
const {
createHook,
executionAsyncId,
Expand Down Expand Up @@ -141,8 +142,8 @@ function setup(root) {
const rejectionHandler =
createProcessEventHandler('unhandledRejection', root);
const coverage = configureCoverage(root, globalOptions);
const exitHandler = async () => {
await root.run(new ERR_TEST_FAILURE(
const exitHandler = () => {
root.postRun(new ERR_TEST_FAILURE(
'Promise resolution is still pending but the event loop has already resolved',
kCancelledByParent));

Expand All @@ -151,8 +152,8 @@ function setup(root) {
process.removeListener('uncaughtException', exceptionHandler);
};

const terminationHandler = async () => {
await exitHandler();
const terminationHandler = () => {
exitHandler();
process.exit();
};

Expand Down Expand Up @@ -217,17 +218,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading