|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | + |
| 6 | +// Plain-JS CI helper; its types are INFERRED by `tsconfig.scripts.json` |
| 7 | +// (`allowJs`), so no `@ts-expect-error` here — see objectui#3494. |
| 8 | +import { cliHasTestFilters, evaluateVitestInvocation, parseVitestArgv } from '../vitest-invocation-guard.mjs'; |
| 9 | + |
| 10 | +/** |
| 11 | + * objectui#8240 — `pnpm --filter @object-ui/console test` collected ZERO test |
| 12 | + * files and exited 1, while the same 89 files were green from the repo root. |
| 13 | + * |
| 14 | + * ## The mechanism, measured at 0fa7a9c83 |
| 15 | + * |
| 16 | + * The entry was `vitest run --root ../.. apps/console/`, i.e. the spelling |
| 17 | + * objectui#3240 gave every `packages/*` entry, transplanted to an app. It works |
| 18 | + * for packages and cannot work for an app, and the reason is NOT the `apps/**` |
| 19 | + * line in the root config's `sharedExclude` (the card that reported this said it |
| 20 | + * was; that is wrong, and the control below pins the refutation). |
| 21 | + * |
| 22 | + * Vitest matches a positional filter per project, against each test file's path |
| 23 | + * RELATIVE TO THAT PROJECT'S OWN `dir`, and resolves a relative filter against |
| 24 | + * `process.cwd()` — not against `--root`: |
| 25 | + * |
| 26 | + * const testFile = relative(dir, t); |
| 27 | + * const relativePath = f.endsWith('/') ? join(relative(dir, f), '/') : relative(dir, f); |
| 28 | + * return testFile.includes(f) || testFile.includes(relativePath); |
| 29 | + * |
| 30 | + * For `packages/*` the files are collected by the ROOT-level projects, whose |
| 31 | + * `dir` is the repo root, so `testFile` reads `packages/<pkg>/src/x.test.ts` and |
| 32 | + * the literal `packages/<pkg>/` matches — cwd never enters into it. |
| 33 | + * |
| 34 | + * An app is different: it comes back through the root config's `projects` array |
| 35 | + * as its OWN project rooted at the app directory, so `testFile` reads |
| 36 | + * `src/x.test.tsx` and can never contain `apps/<app>/`. The only branch left is |
| 37 | + * `relative(dir, f)`, and pnpm launches the script with the app directory as |
| 38 | + * cwd, so `apps/console/` resolved to `apps/console/apps/console` — no match, |
| 39 | + * zero files, exit 1. |
| 40 | + * |
| 41 | + * Measured, same head, same vitest root (`RUN v4.1.10 <repo root>` in both), the |
| 42 | + * only variable being cwd: |
| 43 | + * |
| 44 | + * cwd = apps/console `vitest run --root ../.. apps/console/` 0 files, exit 1 |
| 45 | + * cwd = repo root `vitest run apps/console/` 89 files, exit 0 |
| 46 | + * cwd = repo root `vitest run --project @object-ui/console` |
| 47 | + * 89 files, 1069 tests, exit 0 |
| 48 | + * |
| 49 | + * ## Why the entry names a PROJECT and why it must also disable passWithNoTests |
| 50 | + * |
| 51 | + * There is no positional that is both cwd-stable and precise for an app, so the |
| 52 | + * entry names the project instead. That trades one hazard for another, and the |
| 53 | + * trade is only safe because both ends are closed: |
| 54 | + * |
| 55 | + * - a project name that resolves to nothing is loud: vitest throws |
| 56 | + * `No projects matched the filter "..."` (measured: exit 1); |
| 57 | + * - but a project that resolves and then globs zero files is SILENT, because |
| 58 | + * the root config sets `passWithNoTests: !cliHasTestFilters(process.argv)` |
| 59 | + * and `--project` is a flag, not a positional — so an app entry gets |
| 60 | + * `passWithNoTests: true`. Measured in a sandbox on the same vitest 4.1.10: |
| 61 | + * with `passWithNoTests: true` in the config, `--project <empty project>` |
| 62 | + * exits **0**; adding `--no-passWithNoTests` makes the same run exit **1**. |
| 63 | + * |
| 64 | + * A script that exits 0 having run nothing is strictly worse than the loud |
| 65 | + * exit 1 this card started from, so the negation is load-bearing, not decoration. |
| 66 | + * |
| 67 | + * Reverse verification: restore `"test": "vitest run --root ../.. apps/console/"` |
| 68 | + * and the shape assertions here go red naming `apps/console`; drop just the |
| 69 | + * `--no-passWithNoTests` and the false-green assertion goes red on its own. |
| 70 | + */ |
| 71 | + |
| 72 | +const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..'); |
| 73 | +const APPS_DIR = path.join(repoRoot, 'apps'); |
| 74 | +const TEST_FILE = /\.test\.[cm]?[jt]sx?$/; |
| 75 | + |
| 76 | +type AppEntry = { |
| 77 | + /** Repo-relative directory, e.g. `apps/console`. */ |
| 78 | + dir: string; |
| 79 | + /** The app's package name, which is also its vitest project name. */ |
| 80 | + pkgName: string; |
| 81 | + testScript: string; |
| 82 | +}; |
| 83 | + |
| 84 | +function readAppEntriesWithTestScript(): AppEntry[] { |
| 85 | + const entries: AppEntry[] = []; |
| 86 | + for (const name of fs.readdirSync(APPS_DIR).sort()) { |
| 87 | + const manifest = path.join(APPS_DIR, name, 'package.json'); |
| 88 | + if (!fs.existsSync(manifest)) continue; |
| 89 | + const pkg = JSON.parse(fs.readFileSync(manifest, 'utf8')) as { |
| 90 | + name?: string; |
| 91 | + scripts?: Record<string, string>; |
| 92 | + }; |
| 93 | + const testScript = pkg.scripts?.test; |
| 94 | + if (!testScript || !pkg.name) continue; |
| 95 | + entries.push({ dir: `apps/${name}`, pkgName: pkg.name, testScript }); |
| 96 | + } |
| 97 | + return entries; |
| 98 | +} |
| 99 | + |
| 100 | +/** `process.argv` as pnpm hands it to a package script. */ |
| 101 | +function argvForScript(script: string): string[] { |
| 102 | + const tokens = script.trim().split(/\s+/); |
| 103 | + return ['/usr/bin/node', '/bin/vitest', ...tokens.slice(1)]; |
| 104 | +} |
| 105 | + |
| 106 | +/** The directory this script makes vitest's ROOT (cwd unless `--root` moves it). */ |
| 107 | +function vitestRootOf(entry: AppEntry): string { |
| 108 | + const { flags } = parseVitestArgv(argvForScript(entry.testScript)); |
| 109 | + const raw = flags['--root'] ?? flags['-r']; |
| 110 | + const cwd = path.join(repoRoot, entry.dir); |
| 111 | + return typeof raw === 'string' ? path.resolve(cwd, raw) : cwd; |
| 112 | +} |
| 113 | + |
| 114 | +/** Either accepted spelling of "do not go green on an empty collection". */ |
| 115 | +function disablesPassWithNoTests(script: string): boolean { |
| 116 | + const { flags } = parseVitestArgv(argvForScript(script)); |
| 117 | + return flags['--no-passWithNoTests'] === true || flags['--passWithNoTests'] === 'false'; |
| 118 | +} |
| 119 | + |
| 120 | +function collectTestFiles(dir: string): string[] { |
| 121 | + const out: string[] = []; |
| 122 | + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { |
| 123 | + if (entry.name === 'node_modules' || entry.name === 'dist') continue; |
| 124 | + const abs = path.join(dir, entry.name); |
| 125 | + if (entry.isDirectory()) out.push(...collectTestFiles(abs)); |
| 126 | + else if (TEST_FILE.test(entry.name)) out.push(abs); |
| 127 | + } |
| 128 | + return out; |
| 129 | +} |
| 130 | + |
| 131 | +const appEntries = readAppEntriesWithTestScript(); |
| 132 | +const rootConfig = fs.readFileSync(path.join(repoRoot, 'vitest.config.mts'), 'utf8'); |
| 133 | + |
| 134 | +describe('objectui#8240 — apps/* test entries reach their project by NAME, never by path filter', () => { |
| 135 | + it('finds at least one app test entry to check', () => { |
| 136 | + // A zero-hit sweep and a clean sweep are the same output otherwise. The |
| 137 | + // population was measured as exactly one (`apps/console`; `apps/site` |
| 138 | + // declares no `test` script at all) — this fires if it ever empties out. |
| 139 | + expect(appEntries.map((e) => e.dir)).toContain('apps/console'); |
| 140 | + }); |
| 141 | + |
| 142 | + for (const entry of appEntries) { |
| 143 | + describe(entry.dir, () => { |
| 144 | + it('is routed back into the repo-root config and passes the invocation guard', () => { |
| 145 | + expect(vitestRootOf(entry)).toBe(repoRoot); |
| 146 | + expect( |
| 147 | + evaluateVitestInvocation({ |
| 148 | + argv: argvForScript(entry.testScript), |
| 149 | + cwd: path.join(repoRoot, entry.dir), |
| 150 | + repoRoot, |
| 151 | + }) |
| 152 | + ).toBeNull(); |
| 153 | + }); |
| 154 | + |
| 155 | + it('names its own project instead of passing a positional path filter', () => { |
| 156 | + const { positionals, flags } = parseVitestArgv(argvForScript(entry.testScript)); |
| 157 | + |
| 158 | + // The regression itself: a positional here is the objectui#8240 shape. |
| 159 | + expect(positionals).toEqual([]); |
| 160 | + // The project name vitest derives for an app is its package name. |
| 161 | + expect(flags['--project']).toBe(entry.pkgName); |
| 162 | + }); |
| 163 | + |
| 164 | + it('is declared as a project by the root config, so the name resolves', () => { |
| 165 | + // `--project` naming nothing is loud (vitest throws), but a red CI run |
| 166 | + // is a worse place to learn it than this file. |
| 167 | + expect(rootConfig).toContain(`./${entry.dir}/vitest.config.ts`); |
| 168 | + expect(fs.existsSync(path.join(repoRoot, entry.dir, 'vitest.config.ts'))).toBe(true); |
| 169 | + }); |
| 170 | + |
| 171 | + it('cannot go green on an empty collection', () => { |
| 172 | + // `--project` is a flag, so the root config's |
| 173 | + // `passWithNoTests: !cliHasTestFilters(process.argv)` resolves to TRUE |
| 174 | + // for this entry. That is precisely the case the explicit negation is |
| 175 | + // here to cover; assert the premise as well as the remedy, so this stops |
| 176 | + // asking for the flag if the premise ever changes. |
| 177 | + expect(cliHasTestFilters(argvForScript(entry.testScript))).toBe(false); |
| 178 | + expect(disablesPassWithNoTests(entry.testScript)).toBe(true); |
| 179 | + }); |
| 180 | + |
| 181 | + it('has test files for that project to collect', () => { |
| 182 | + expect(collectTestFiles(path.join(repoRoot, entry.dir)).length).toBeGreaterThan(0); |
| 183 | + }); |
| 184 | + |
| 185 | + it('could not have been reached by a path filter, whatever the exclude list says', () => { |
| 186 | + // The refutation of the reported mechanism. An app's project is rooted |
| 187 | + // at the app directory, so its files' project-relative paths never |
| 188 | + // contain `apps/<app>/` — the substring branch of vitest's filter match |
| 189 | + // cannot fire, independently of `sharedExclude`. Removing `apps/**` |
| 190 | + // from that list would therefore not have fixed anything, and is exactly |
| 191 | + // what the card asked nobody to do. |
| 192 | + const appDir = path.join(repoRoot, entry.dir); |
| 193 | + const files = collectTestFiles(appDir); |
| 194 | + expect(files.length).toBeGreaterThan(0); |
| 195 | + for (const file of files) { |
| 196 | + expect(path.relative(appDir, file)).not.toContain(`${entry.dir}/`); |
| 197 | + } |
| 198 | + |
| 199 | + // Control that fires: the same paths taken relative to the REPO ROOT do |
| 200 | + // contain it, which is why the identical spelling works for packages/*. |
| 201 | + for (const file of files) { |
| 202 | + expect(path.relative(repoRoot, file)).toContain(`${entry.dir}/`); |
| 203 | + } |
| 204 | + }); |
| 205 | + }); |
| 206 | + } |
| 207 | +}); |
0 commit comments