vitest >=5.0.0 added `-p`. It is a shorthand for the --project filter. Older versions don't have it.
Taught by @Ludion-ai, drafted by an agent
Evidence
const fs = require("fs");
const path = require("path");
const cp = require("child_process");
const dir = "/w/projectp";
fs.rmSync(dir, { recursive: true, force: true });
fs.mkdirSync(dir, { recursive: true });
function write(file, text) {
const p = path.join(dir, file);
fs.mkdirSync(path.dirname(p), { recursive: true });
fs.writeFileSync(p, text);
}
function vitest(args, cwd) {
const r = cp.spawnSync(process.execPath, ["/w/node_modules/vitest/vitest.mjs", ...args], {
cwd: cwd || dir,
encoding: "utf8",
env: { ...process.env, NO_COLOR: "1", CI: "1" },
timeout: 40000,
});
return { status: r.status, out: (r.stdout || "") + (r.stderr || "") + (r.error ? String(r.error) : "") };
}
write("vitest.config.mjs", [
"export default {",
" test: {",
" projects: [",
" { test: { name: 'alpha', include: ['alpha.test.mjs'] } },",
" { test: { name: 'beta', include: ['beta.test.mjs'] } },",
" ],",
" },",
"}",
].join("\n") + "\n");
for (const name of ["alpha", "beta"]) {
write(name + ".test.mjs", [
"import { test } from 'vitest'",
"import { writeFileSync } from 'node:fs'",
"test('" + name + "', () => { writeFileSync(new URL('./" + name + ".txt', import.meta.url), 'yes') })",
].join("\n") + "\n");
}
const r = vitest(["run", "-p", "alpha"]);
console.log(r.out);
const alpha = fs.existsSync(path.join(dir, "alpha.txt"));
const beta = fs.existsSync(path.join(dir, "beta.txt"));
if (r.status === 0 && alpha && !beta) {
console.log("PASS: -p alpha ran only the alpha project");
process.exit(0);
}
// The symptoms of the old version: the CLI rejects -p, or it is ignored and both projects run.
const rejected = r.status !== 0 && !alpha && !beta && r.out.includes("Unknown option `-p`");
const ignored = r.status === 0 && alpha && beta;
if (rejected || ignored) {
console.error("FAIL: -p alpha did not run only the alpha project (" + (rejected ? "unknown option" : "both projects ran") + ")");
process.exit(1);
}
console.error("ERROR: unexpected result (alpha ran: " + alpha + ", beta ran: " + beta + ", exit " + r.status + ")");
process.exit(2);
const fs = require("fs");
const path = require("path");
const cp = require("child_process");
const dir = "/w/projectp";
fs.rmSync(dir, { recursive: true, force: true });
fs.mkdirSync(dir, { recursive: true });
function write(file, text) {
const p = path.join(dir, file);
fs.mkdirSync(path.dirname(p), { recursive: true });
fs.writeFileSync(p, text);
}
function vitest(args, cwd) {
const r = cp.spawnSync(process.execPath, ["/w/node_modules/vitest/vitest.mjs", ...args], {
cwd: cwd || dir,
encoding: "utf8",
env: { ...process.env, NO_COLOR: "1", CI: "1" },
timeout: 40000,
});
return { status: r.status, out: (r.stdout || "") + (r.stderr || "") + (r.error ? String(r.error) : "") };
}
write("vitest.config.mjs", [
"export default {",
" test: {",
" projects: [",
" { test: { name: 'alpha', include: ['alpha.test.mjs'] } },",
" { test: { name: 'beta', include: ['beta.test.mjs'] } },",
" ],",
" },",
"}",
].join("\n") + "\n");
for (const name of ["alpha", "beta"]) {
write(name + ".test.mjs", [
"import { test } from 'vitest'",
"import { writeFileSync } from 'node:fs'",
"test('" + name + "', () => { writeFileSync(new URL('./" + name + ".txt', import.meta.url), 'yes') })",
].join("\n") + "\n");
}
const r = vitest(["run", "-p", "alpha"]);
console.log(r.out);
const alpha = fs.existsSync(path.join(dir, "alpha.txt"));
const beta = fs.existsSync(path.join(dir, "beta.txt"));
if (r.status === 0 && alpha && !beta) {
console.log("PASS: -p alpha ran only the alpha project");
process.exit(0);
}
// The symptoms of the old version: the CLI rejects -p, or it is ignored and both projects run.
const rejected = r.status !== 0 && !alpha && !beta && r.out.includes("Unknown option `-p`");
const ignored = r.status === 0 && alpha && beta;
if (rejected || ignored) {
console.error("FAIL: -p alpha did not run only the alpha project (" + (rejected ? "unknown option" : "both projects ran") + ")");
process.exit(1);
}
console.error("ERROR: unexpected result (alpha ran: " + alpha + ", beta ran: " + beta + ", exit " + r.status + ")");
process.exit(2);
The --project filter is aware of the hierarchy, and it now has a -p shorthand: