Файли
openidea/src/features/run/testing/nodeTest.ts
T
LU XIANGRUIтаwildcatDownstairs 8e541d5b45 feat(run): run node:test tests with a test tree and Rerun Failed Tests (#5172)
`node --test` runs showed no test tree, and single tests ran with Jest's `-t`.

- Detection: a `package.json` script whose Node options include `--test` (also `tsx --test`) is a test configuration with `testRunner: "node-test"`; a script that picks its own `--test-reporter` runs as written.
- Test tree: test runs add the IDE's reporter (`nodeTestReporter.js`, passed as a `data:` URL) next to `spec` through `NODE_OPTIONS`, which works through `npm run`, Node's per-file child processes and remote run targets. It prints a record per test (file, describe chain, status, duration, declaration line, error); the records become the tree and are hidden from the console, which keeps `spec`'s output.
- Gutter, Rerun Failed Tests, run selected tests: select by full name with `--test-name-pattern` in `NODE_OPTIONS`, plus the files.

New fixture `fixtures/node-test-app`, run by `smoke/run.test.ts` in the TypeScript job.

Closes #5152

Co-authored-by: wildcatDownstairs <[email protected]>
2026-11-22 15:19:57 +08:00

32 рядки
1.7 KiB
TypeScript

/**
* `node --test`: the IDE's reporter (`nodeTestReporter.js`) runs next to `spec` and prints a
* record per finished test (`records.ts`). It reaches Node through `NODE_OPTIONS`, as does the
* name filter: a `package.json` script's own arguments can't take them (`node --test` reads
* options only before its file patterns).
*/
import REPORTER_SOURCE from "./nodeTestReporter.js?raw";
const REPORTER = `--test-reporter=data:text/javascript;base64,${btoa(REPORTER_SOURCE)}`;
const PATTERN_RE = /(^|\s)--test-name-pattern=(?:"(?:[^"\\]|\\.)*"|\S+)/g;
/** `NODE_OPTIONS` with the IDE's reporter added (once), `spec` keeping the console readable. */
export function withNodeTestReporter(env: Record<string, string>): Record<string, string> {
const options = env.NODE_OPTIONS ?? "";
if (options.includes(REPORTER) || /(^|\s)--test-reporter[=\s]/.test(options)) return env;
const reporters = [
"--test-reporter=spec",
"--test-reporter-destination=stdout",
REPORTER,
"--test-reporter-destination=stdout",
];
return { ...env, NODE_OPTIONS: [options, ...reporters].filter(Boolean).join(" ") };
}
/** `NODE_OPTIONS` selecting the tests whose full name (describe chain and name, space separated) matches `pattern`. */
export function withNodeTestNamePattern(env: Record<string, string>, pattern: string): Record<string, string> {
const options = (env.NODE_OPTIONS ?? "").replace(PATTERN_RE, "").trim();
// NODE_OPTIONS splits at spaces outside double quotes, where a backslash escapes the next character.
const quoted = `"${pattern.replace(/[\\"]/g, "\\$&")}"`;
return { ...env, NODE_OPTIONS: [options, `--test-name-pattern=${quoted}`].filter(Boolean).join(" ") };
}