mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-23 08:31:44 +01:00
Test files that use Node's built-in test runner (`node:test`) now run with `node --test` in repos whose package.json declares vitest or jest, so /ship's pre-merge test check stops failing on them. Before, a root `.js`, `.mjs` or `.cjs` file that loads `node:test` was handed to vitest, which found no tests and failed every PR that touched one. Reported by @eoin-obrien in #4586. Nothing to do. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> Review-Finding: p1/specialist-batched#1 reader=testing Review-Finding: p2/outside-voice#1 reader=outside-voice
This commit is contained in:
1 parent
a1e2c7eeaa
commit
f71e785e4e
4 files changed
+47
-4
No files matched your search
@@ -0,0 +1 @@
|
||||
**Node's built-in test files now run with `node --test` in repos that use vitest or jest.** In a repo whose root package.json declares vitest (a common pnpm monorepo setup), the pre-merge test check ran root `.js`, `.mjs` and `.cjs` files that load `node:test` under vitest, which found no suite in them and failed, so `/ship` stopped at its finish step on every PR that touched one. Those files now get their own `node --test` run, vitest and jest tests keep their runner, and bun repos are unchanged because bun runs `node:test` files itself. Reported by @eoin-obrien in #4586.
|
||||
+1
-1
@@ -91,7 +91,7 @@ the byte-pinned `/ship` renders in `test/fixtures/golden/`).
|
||||
|
||||
| Key | Meaning |
|
||||
|---|---|
|
||||
| `runner` | the local test command with `{files}` (gstack: `bun test --timeout=30000 {files}`, the 30 s per-test timeout CI uses); set, the local lane gives it only the selected files its tool runs, judged by extension (JS/TS files for `bun test`, `node --test`, `deno test`, vitest, jest, mocha, ava or playwright; `.py` files for pytest; each matched as a whole word, so `npx vitest run` counts and `tests/node/run.sh` does not; every file for any other command, such as a `node scripts/run-tests.js` dispatcher, or one naming both), and the lanes detail names each file it cannot run as not run and not counted as covered, so a `*_test.py` never reaches a local `bun test` (no Python lane runs beside a declared runner; the remote gate and `test_backend` commands still receive the whole selection, a declared test tree's tests included, since that exclusion is local-only); unset, it is detected (bun, vitest or jest for JS/TS files, pytest when python3 can import it (else `pytest` or `python3` is named missing), `node --test`, and `flutter test`/`dart test` per Dart package root, through fvm when that package or an ancestor pins it with `.fvmrc` or `.fvm/fvm_config.json` and fvm is on PATH; `cargo test -p <name>` per Rust crate, run from the root of the workspace the crate is a member of (Cargo's `members` globs, `exclude` and `package.workspace`), else the crate directory; a JS/TS test whose nearest package.json is a nested package declaring vitest or jest (a dependency or its `test` script) runs from that package, with package-relative paths, through the package manager its `packageManager` field or the nearest lockfile up to the repo root names: `pnpm exec`, `yarn`, `bun x`, else `npx`; a JS/TS test file that imports `@playwright/test` at the start of a line runs as `playwright test --reporter=list` from its nearest package through that same package manager, whatever the unit runner is, a type-only import (`import type …`, or one whose every specifier is `type X`) does not count, file arguments are passed as escaped regexes anchored to the path (Playwright reads them as regexes), and a run where every test fails and Playwright says its browser is not installed is `PREGATE_ENVIRONMENT` (a partial install, where some tests pass, stays the diff's failure); every other test keeps the root detection, split by language when no one runner fits them all, so `.mjs` and `.py` root tests each get their own runner); nothing detected, a Dart SDK, cargo or package manager missing from PATH, a `.dart` file with no `pubspec.yaml` above it, a `.rs` file outside any crate, or one in a crate whose name Cargo would reject (anything but letters, digits, `_` and `-`), is `incomplete` (the runnable groups still run, so a real failure still fails) |
|
||||
| `runner` | the local test command with `{files}` (gstack: `bun test --timeout=30000 {files}`, the 30 s per-test timeout CI uses); set, the local lane gives it only the selected files its tool runs, judged by extension (JS/TS files for `bun test`, `node --test`, `deno test`, vitest, jest, mocha, ava or playwright; `.py` files for pytest; each matched as a whole word, so `npx vitest run` counts and `tests/node/run.sh` does not; every file for any other command, such as a `node scripts/run-tests.js` dispatcher, or one naming both), and the lanes detail names each file it cannot run as not run and not counted as covered, so a `*_test.py` never reaches a local `bun test` (no Python lane runs beside a declared runner; the remote gate and `test_backend` commands still receive the whole selection, a declared test tree's tests included, since that exclusion is local-only); unset, it is detected (bun, vitest or jest for JS/TS files, pytest when python3 can import it (else `pytest` or `python3` is named missing), `node --test`, and `flutter test`/`dart test` per Dart package root, through fvm when that package or an ancestor pins it with `.fvmrc` or `.fvm/fvm_config.json` and fvm is on PATH; `cargo test -p <name>` per Rust crate, run from the root of the workspace the crate is a member of (Cargo's `members` globs, `exclude` and `package.workspace`), else the crate directory; a JS/TS test whose nearest package.json is a nested package declaring vitest or jest (a dependency or its `test` script) runs from that package, with package-relative paths, through the package manager its `packageManager` field or the nearest lockfile up to the repo root names: `pnpm exec`, `yarn`, `bun x`, else `npx`; a JS/TS test file that imports `@playwright/test` at the start of a line runs as `playwright test --reporter=list` from its nearest package through that same package manager, whatever the unit runner is, a type-only import (`import type …`, or one whose every specifier is `type X`) does not count, file arguments are passed as escaped regexes anchored to the path (Playwright reads them as regexes), and a run where every test fails and Playwright says its browser is not installed is `PREGATE_ENVIRONMENT` (a partial install, where some tests pass, stays the diff's failure); a `.js`, `.mjs` or `.cjs` test that loads `node:test` in a statement starting a line, and loads neither vitest nor `@jest/globals`, runs through `node --test` from the repo root even when the repo or its package declares vitest or jest (which find no suite in such a file), while a bun repo keeps `bun test` for it; every other test keeps the root detection, split by language when no one runner fits them all, so `.mjs` and `.py` root tests each get their own runner); nothing detected, a Dart SDK, cargo or package manager missing from PATH, a `.dart` file with no `pubspec.yaml` above it, a `.rs` file outside any crate, or one in a crate whose name Cargo would reject (anything but letters, digits, `_` and `-`), is `incomplete` (the runnable groups still run, so a real failure still fails) |
|
||||
| `dependencies` | glob → test files for subprocess targets, runtime reads and generated inputs the import graph cannot see; `[]` declares "no lane needed" (docs, licences). A `[]` on a folder inside a test directory (`test/`, `tests/`, `spec/` or `__tests__/`, as in `"tests/live/**": []`; never the whole directory, so `"test/**": []` changes nothing) is the repo's statement that the tests under it run in their own tier, not in /ship's local lane: pregate keeps every selected test under that glob out of the local lane, whether touched or picked through an import (a test `.gstack/always-run.json` names still runs; the remote side is unchanged: the remote gate, `test_backend` and any remote platform lane that selects the test still owe it; and only a base-branch or user-owned policy's `[]` counts, never a bootstrap head file read under `--policy-from` head when neither exists), and prints `declared test tree <glob>: N test file(s) not run in the local lane` in the `lanes` lines it records; tests outside the glob still run, and a `[]` on any other glob never skips a test. A `[]` that matches a deleted test does not skip it: the deleted test still runs its owning suite, and a file naming it that `[]` covers (a durations list) counts as covered. With this key present, even empty, the policy [declares lanes](#lane-requirements) |
|
||||
| `empty_lanes` | extra globs for the docs/no-tests empty lane. Any Markdown file already qualifies with no pin file when no test depends on it (the same import graph as other files: a quoted path, a `join()` of path segments, an import, or a `dependencies` entry). reStructuredText and AsciiDoc count as Markdown here. README, CHANGELOG, CONTRIBUTING, LICENSE, AGENTS and CLAUDE qualify even without `.md`, and so does licence text under any vendored name (`roboto-LICENSE.txt`, `LICENSE-MIT`, `licenses/*.txt`). Source code and files under a test directory never qualify. A test that reads the file still selects that test |
|
||||
| `selection` | `workflow.yml/job` → a command printing the lane's file list, for matrices a planner computes (gstack: `scripts/test-free-shards.ts --list`) |
|
||||
|
||||
@@ -49,7 +49,7 @@ import type { CheckDef, CheckResult, PregateContext, RemoteObligation } from '..
|
||||
/** The runner a `.py`-only selection gets; it needs the pytest module importable by python3 (`pytestMissing`). */
|
||||
const PYTEST = 'python3 -m pytest {files}';
|
||||
|
||||
/** The test command with `{files}`; detected from the repo when the config does not name one. The repo's bun, vitest or jest claims JS/TS files only. */
|
||||
/** The test command with `{files}`; detected from the repo when the config does not name one. The repo's bun, vitest or jest claims JS/TS files only; JS files that all load `node:test` get `node --test` ahead of vitest and jest, which find no suite in them, while bun runs them itself. */
|
||||
export function detectRunner(ctx: Pick<PregateContext, 'config' | 'repoRoot'>, files: readonly string[]): string | null {
|
||||
if (ctx.config.runner) return ctx.config.runner;
|
||||
const has = (f: string) => fs.existsSync(path.join(ctx.repoRoot, f));
|
||||
@@ -58,6 +58,7 @@ export function detectRunner(ctx: Pick<PregateContext, 'config' | 'repoRoot'>, f
|
||||
const deps = { ...pkg.dependencies, ...pkg.devDependencies };
|
||||
const jsts = files.every(f => /\.[cm]?[jt]sx?$/.test(f));
|
||||
if (jsts && (has('bunfig.toml') || has('bun.lock') || has('bun.lockb'))) return 'bun test {files}';
|
||||
if (files.length && files.every(f => /\.[cm]?js$/.test(f) && importsNodeTest(readRepoFile(ctx.repoRoot, f) ?? ''))) return 'node --test {files}';
|
||||
if (jsts && deps.vitest) return 'npx vitest run {files}';
|
||||
if (jsts && deps.jest) return 'npx jest {files}';
|
||||
if (files.every(f => /\.py$/.test(f)) && files.length) return PYTEST;
|
||||
@@ -196,6 +197,12 @@ function importsPlaywright(text: string): boolean {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/** Whether a JS test is a node:test file: it loads `node:test` in a statement that starts a line (`import … from 'node:test'`, a bare import, or `require`) and loads neither vitest nor `@jest/globals`, whose runner owns it. Text that mentions a module mid-line, in a comment or in a template literal does not count. */
|
||||
function importsNodeTest(text: string): boolean {
|
||||
const code = text.replace(/\/\*[\s\S]*?\*\/|\/\/[^\n]*|`(?:\\[\s\S]|[^\\`])*`/g, '');
|
||||
const loads = (mod: string) => new RegExp(`^[\\t ]*(?:import(?=[\\s{*'"])[^'"\`;]*?|(?:const|let|var)\\b[^=\\n]*=[\\t ]*require\\([\\t ]*)['"](?:${mod})['"]`, 'm').test(code);
|
||||
return loads('node:test') && !loads('vitest|@jest/globals');
|
||||
}
|
||||
/** Playwright's own text when its browsers are not installed on this machine. */
|
||||
const PLAYWRIGHT_NO_BROWSERS = /^.*(?:Executable doesn't exist at|download new browsers).*$/m;
|
||||
|
||||
@@ -216,6 +223,7 @@ function declaredRunnerFiles(runner: string): RegExp | null {
|
||||
* from the crate's workspace root; JS/TS tests owned by a nested package that
|
||||
* declares vitest or jest run from that package; a JS/TS test that imports
|
||||
* `@playwright/test` runs through `playwright test` from its nearest package;
|
||||
* a `.js`, `.mjs` or `.cjs` test that loads `node:test` runs from the repo root with the runner `detectRunner` gives node:test files;
|
||||
* the rest go to the detected runner from the repo root, split by language
|
||||
* when no one runner fits them all (`.mjs` + `.py` runs both), and pytest only
|
||||
* when python3 can import it. Files no runner claims (a tool missing from PATH,
|
||||
@@ -239,6 +247,7 @@ export function planRuns(ctx: PregateContext, files: readonly string[]): RunPlan
|
||||
let model: CargoModel | null = null;
|
||||
const pkgRunners = new Map<string, ReturnType<typeof packageRunner>>();
|
||||
const rest: string[] = [];
|
||||
const nodeTests: string[] = [];
|
||||
const noPubspec: string[] = [];
|
||||
const unclaimed: string[] = [];
|
||||
const missing = new Set<string>();
|
||||
@@ -257,6 +266,7 @@ export function planRuns(ctx: PregateContext, files: readonly string[]): RunPlan
|
||||
}
|
||||
const pkg = /\.[cm]?[jt]sx?$/.test(f) ? nearestRoot(ctx.repoRoot, f, 'package.json', seenPackage) : null;
|
||||
if (pkg !== null && importsPlaywright(readRepoFile(ctx.repoRoot, f) ?? '')) { add(playwright, pkg, under(pkg, f)); continue; }
|
||||
if (/\.[cm]?js$/.test(f) && importsNodeTest(readRepoFile(ctx.repoRoot, f) ?? '')) { nodeTests.push(f); continue; }
|
||||
if (pkg) {
|
||||
if (!pkgRunners.has(pkg)) pkgRunners.set(pkg, packageRunner(ctx.repoRoot, pkg, ctx.env));
|
||||
const r = pkgRunners.get(pkg);
|
||||
@@ -279,7 +289,7 @@ export function planRuns(ctx: PregateContext, files: readonly string[]): RunPlan
|
||||
unclaimed.push(...c.files.map(f => (c.cwd ? `${c.cwd}/${f}` : f)));
|
||||
}
|
||||
const language = (f: string) => (/\.py$/.test(f) ? 'py' : /\.[cm]?js$/.test(f) ? 'js' : /\.[cm]?[jt]sx?$/.test(f) ? 'ts' : f);
|
||||
const parts = !rest.length ? [] : detectRunner(ctx, rest) ? [rest] : [...Map.groupBy(rest, language).values()];
|
||||
const parts = [...(!rest.length ? [] : detectRunner(ctx, rest) ? [rest] : Map.groupBy(rest, language).values()), ...(nodeTests.length ? [nodeTests] : [])];
|
||||
const byRunner = new Map<string, string[]>();
|
||||
for (const part of parts) {
|
||||
const runner = detectRunner(ctx, part);
|
||||
|
||||
@@ -1036,6 +1036,35 @@ describe('nested JS packages run their own runner from their own directory', ()
|
||||
expect(planRuns(ctxFor(repo, { PATH: path.join(repo.root, 'empty-path') }), ['app/e2e/app.spec.ts'])).toEqual({ groups: [], unclaimed: ['app/e2e/app.spec.ts'], missing: ['pnpm'], noPubspec: [] });
|
||||
});
|
||||
|
||||
// Value: protects=a JS test that loads node:test runs through `node --test`, never the repo's vitest (which finds no suite in it and fails
|
||||
// the lane, stopping /ship's finish on every PR that selects one); fails_when=the repo-wide runner takes node:test files again (#4586);
|
||||
// why_new=no detection looked at node:test imports; seam=none; no_claim=a real node or vitest run (plan only)
|
||||
test('a JS test importing node:test runs through node --test even when the repo declares vitest; vitest tests keep vitest', () => {
|
||||
const repo = fixture({ pregate: null, baseFiles: APP });
|
||||
const ctx = ctxFor(repo, pmShim(repo));
|
||||
write(repo.work, 'package.json', JSON.stringify({ name: 'monorepo', private: true, devDependencies: { vitest: '^4.1.0' } }) + '\n');
|
||||
write(repo.work, 'scripts/release-notes.test.mjs', "import { describe, it } from \"node:test\";\nimport assert from 'node:assert/strict';\nit('formats', () => assert.ok(true));\n");
|
||||
write(repo.work, 'scripts/bare.test.js', "import 'node:test';\n");
|
||||
write(repo.work, 'scripts/legacy.test.cjs', "const assert = require('node:assert');\nconst { test } = require( 'node:test' );\ntest('legacy', () => assert.ok(true));\n");
|
||||
write(repo.work, 'app/scripts/check.test.mjs', "import test from 'node:test';\ntest('nested', () => {});\n");
|
||||
write(repo.work, 'test/a.test.ts', "import { expect, test } from 'vitest';\nimport { test as nodeTest } from 'node:test';\ntest('ts stays', () => expect(1).toBe(1));\n");
|
||||
write(repo.work, 'test/fixture-text.test.js', "import { test } from 'vitest';\n// import { test } from 'node:test';\nconst sample = \"const { test } = require('node:test');\";\ntest('mentions node:test only in text', () => {});\n");
|
||||
const files = ['test/a.test.ts', 'scripts/release-notes.test.mjs', 'test/fixture-text.test.js', 'scripts/bare.test.js', 'scripts/legacy.test.cjs', 'app/src/sum.test.ts', 'app/scripts/check.test.mjs'];
|
||||
expect(planRuns(ctx, files)).toEqual({ groups: [
|
||||
{ runner: 'npx vitest run {files}', cwd: '', files: ['test/a.test.ts', 'test/fixture-text.test.js'] },
|
||||
{ runner: 'node --test {files}', cwd: '', files: ['scripts/release-notes.test.mjs', 'scripts/bare.test.js', 'scripts/legacy.test.cjs', 'app/scripts/check.test.mjs'] },
|
||||
{ runner: 'pnpm exec vitest run {files}', cwd: 'app', files: ['src/sum.test.ts'] },
|
||||
], unclaimed: [], missing: [], noPubspec: [] });
|
||||
expect(planRuns(ctx, ['test/fixture-text.test.js']).groups).toEqual([{ runner: 'npx vitest run {files}', cwd: '', files: ['test/fixture-text.test.js'] }]);
|
||||
write(repo.work, 'test/mixed.test.js', "import { test } from 'vitest';\nimport { mock } from 'node:test';\ntest('vitest owns it', () => {});\n");
|
||||
write(repo.work, 'test/jest-mixed.test.cjs', "const { test } = require('@jest/globals');\nconst { mock } = require('node:test');\ntest('jest owns it', () => {});\n");
|
||||
expect(planRuns(ctx, ['test/mixed.test.js', 'test/jest-mixed.test.cjs']).groups).toEqual([{ runner: 'npx vitest run {files}', cwd: '', files: ['test/mixed.test.js', 'test/jest-mixed.test.cjs'] }]);
|
||||
write(repo.work, 'scripts/gen.test.mjs', "import { test } from 'node:test';\nconst generated = `\nimport { test } from 'vitest';\ntest('generated', () => {});\n`;\ntest('writes a vitest file', () => generated.includes('vitest'));\n");
|
||||
expect(planRuns(ctx, ['scripts/gen.test.mjs']).groups).toEqual([{ runner: 'node --test {files}', cwd: '', files: ['scripts/gen.test.mjs'] }]);
|
||||
const declared = { ...ctx, config: { ...DEFAULT_CONFIG, runner: 'npx vitest run {files}' } } as PregateContext;
|
||||
expect(planRuns(declared, ['scripts/release-notes.test.mjs']).groups).toEqual([{ runner: 'npx vitest run {files}', cwd: '', files: ['scripts/release-notes.test.mjs'] }]);
|
||||
});
|
||||
|
||||
// Value: protects=repos where root detection works today are unchanged: a nested package that declares no test runner stays with the
|
||||
// root runner from the root, a root vitest stays `npx vitest run` from the root, and a declared runner takes every file;
|
||||
// fails_when=every nested package.json splits a group or the root command changes; why_new=negative controls for the new
|
||||
@@ -1048,7 +1077,10 @@ describe('nested JS packages run their own runner from their own directory', ()
|
||||
fs.rmSync(path.join(repo.work, 'bun.lock'));
|
||||
write(repo.work, 'package.json', JSON.stringify({ name: 'fixture', devDependencies: { vitest: '^3.2.0' } }) + '\n');
|
||||
write(repo.work, 'pnpm-lock.yaml', "lockfileVersion: '9.0'\n");
|
||||
expect(planRuns(ctx, ['app/src/sum.test.ts', 'test/util.test.js']).groups).toEqual([{ runner: 'npx vitest run {files}', cwd: '', files: ['app/src/sum.test.ts', 'test/util.test.js'] }]);
|
||||
expect(planRuns(ctx, ['app/src/sum.test.ts', 'test/util.test.js']).groups).toEqual([
|
||||
{ runner: 'npx vitest run {files}', cwd: '', files: ['app/src/sum.test.ts'] },
|
||||
{ runner: 'node --test {files}', cwd: '', files: ['test/util.test.js'] },
|
||||
]);
|
||||
const declared = { ...ctx, config: { ...DEFAULT_CONFIG, runner: 'bun test {files}' } } as PregateContext;
|
||||
write(repo.work, 'app/package.json', APP['app/package.json']);
|
||||
expect(planRuns(declared, ['app/src/sum.test.ts']).groups).toEqual([{ runner: 'bun test {files}', cwd: '', files: ['app/src/sum.test.ts'] }]);
|
||||
|
||||
Reference in new issue
Block a user