示例
@vitest/runner 2.1.9: execute test suites and hooks with custom runner using startTests
已验证示例 — npm @vitest/runner 2.1.9: execute test suites and hooks with custom runner using startTests. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过.
sha256:a8b88a1327c6634161c5d687fe29232ebc0cf4002ef150fc365c23958d42dfeb
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。
MIT-0
执行证据
声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。
- 证据依据
- 签名契约通过
- 验证回执
- 1
- 构建过它的签名密钥
- 1
声明的环境
linux 24 · ubuntu · glibc 2.39 x64 npm
验证运行环境
| 环境 | 契约 | 阶段 | 运行日期 |
|---|---|---|---|
| node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5… |
2026-08-26 |
案例
HOW- 目标
- execute test suites and hooks with custom runner using startTests
- 符号
-
- VitestRunner
- afterAll
- afterEach
- beforeAll
- beforeEach
- describe
- onTestFinished
- startTests
- test
- 创建时间
- 2026-08-26T14:46:53Z
契约
- startTests executes test files using a custom VitestRunner and reports pass and fail states
- describe and test define test suites and individual test cases
- startTests records test failures and error messages when assertions fail
- beforeEach and afterEach hooks execute before and after each test case in a suite
- beforeAll and afterAll hooks execute once before and after all tests in a suite
- onTestFinished hook executes when a test completes
- runTestSuite helper convenience wrapper executes tests successfully
文件
- PROMPT.md
- csx.json
- package-lock.json
- package.json
- spec.json
- src/index.mjs
- test/contract.mjs
源代码
Clean-room public code sample — generation instructions
Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.
A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.
Goal: execute test suites and hooks with custom runner using startTests
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:npm/%40vitest/runner@2.1.9
Demonstrate these symbols/APIs:
- VitestRunner
- afterAll
- afterEach
- beforeAll
- beforeEach
- describe
- onTestFinished
- startTests
- test
Rules:
- One focused purpose; the smallest project that proves the goal.
- Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
- Pin every dependency with a lockfile so resolution is reproducible.
- No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
- No personal names, emails, company names, or project identifiers of any kind.
- No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
- Keep it under 200 files and 256KB packed.
{"case":{"caseId":"case:sha256:4dbeba3cbbda0a7eac82549100c70f736f5c87b63776d629712d2da2b98ca856","contract":["startTests executes test files using a custom VitestRunner and reports pass and fail states","describe and test define test suites and individual test cases","startTests records test failures and error messages when assertions fail","beforeEach and afterEach hooks execute before and after each test case in a suite","beforeAll and afterAll hooks execute once before and after all tests in a suite","onTestFinished hook executes when a test completes","runTestSuite helper convenience wrapper executes tests successfully"],"goal":"execute test suites and hooks with custom runner using startTests","kind":"HOW","packages":["pkg:npm/%40vitest/runner@2.1.9"],"schemaVersion":1,"symbols":["VitestRunner","afterAll","afterEach","beforeAll","beforeEach","describe","onTestFinished","startTests","test"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/%40vitest/runner@2.1.9"],"schemaVersion":1,"subject":"pkg:npm/%40vitest/runner@2.1.9","symbols":["VitestRunner","afterAll","afterEach","beforeAll","beforeEach","describe","onTestFinished","startTests","test"],"verifierAdapter":"node-typescript@1"}
{
"name": "sample-vitest-runner",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "sample-vitest-runner",
"version": "1.0.0",
"license": "MIT-0",
"dependencies": {
"@vitest/runner": "2.1.9"
}
},
"node_modules/@vitest/pretty-format": {
"version": "2.1.9",
"resolved": "https://registry.npmjs.org/@vitest/pretty-format/-/pretty-format-2.1.9.tgz",
"integrity": "sha512-KhRIdGV2U9HOUzxfiHmY8IFHTdqtOhIzCpd8WRdJiE7D/HUcZVD0EgQCVjm+Q9gkUXWgBvMmTtZgIG48wq7sOQ==",
"license": "MIT",
"dependencies": {
"tinyrainbow": "^1.2.0"
},
"funding": {
"url": "https://opencollective.com/vitest"
}
},
"node_modules/@vitest/runner": {
"version": "2.1.9",
"resolved": "https://registry.npmjs.org/@vitest/runner/-/runner-2.1.9.tgz",
"integrity": "sha512-ZXSSqTFIrzduD63btIfEyOmNcBmQvgOVsPNPe0jYtESiXkhd8u2erDLnMxmGrDCwHCCHE7hxwRDCT3pt0esT4g==",
"license": "MIT",
"dependencies": {
"@vitest/utils": "2.1.9",
"pathe": "^1.1.2"
},
"funding": {
"url": "https://opencollective.com/vitest"
}
},
"node_modules/@vitest/utils": {
"version": "2.1.9",
"resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-2.1.9.tgz",
"integrity": "sha512-v0psaMSkNJ3A2NMrUEHFRzJtDPFn+/VWZ5WxImB21T9fjucJRmS7xCS3ppEnARb9y11OAzaD+P2Ps+b+BGX5iQ==",
"license": "MIT",
"dependencies": {
"@vitest/pretty-format": "2.1.9",
"loupe": "^3.1.2",
"tinyrainbow": "^1.2.0"
},
"funding": {
"url": "https://opencollective.com/vitest"
}
},
"node_modules/loupe": {
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/loupe/-/loupe-3.2.1.tgz",
"integrity": "sha512-CdzqowRJCeLU72bHvWqwRBBlLcMEtIvGrlvef74kMnV2AolS9Y8xUv1I0U/MNAWMhBlKIoyuEgoJ0t/bbwHbLQ==",
"license": "MIT"
},
"node_modules/pathe": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/pathe/-/pathe-1.1.2.tgz",
"integrity": "sha512-whLdWMYL2TwI08hn8/ZqAbrVemu0LNaNNJZX73O6qaIdCTfXutsLhMkjdENX0qhsQ9uIimo4/aQOmXkoon2nDQ==",
"license": "MIT"
},
"node_modules/tinyrainbow": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/tinyrainbow/-/tinyrainbow-1.2.0.tgz",
"integrity": "sha512-weEDEq7Z5eTHPDh4xjX789+fHfF+P8boiFB+0vbWzpbnbsEr/GRaohi/uMKxg8RZMXnl1ItAi/IUHWMsjDV7kQ==",
"license": "MIT",
"engines": {
"node": ">=14.0.0"
}
}
}
}
{
"name": "sample-vitest-runner",
"version": "1.0.0",
"private": true,
"description": "Clean-room verification of @vitest/runner@2.1.9",
"main": "src/index.mjs",
"type": "module",
"license": "MIT-0",
"dependencies": {
"@vitest/runner": "2.1.9"
}
}
{
"schemaVersion": 1,
"goal": "execute test suites and hooks with custom runner using startTests",
"kind": "HOW",
"packages": [
"pkg:npm/%40vitest/runner@2.1.9"
],
"symbols": [
"VitestRunner",
"afterAll",
"afterEach",
"beforeAll",
"beforeEach",
"describe",
"onTestFinished",
"startTests",
"test"
]
}
import {
startTests,
test,
describe,
it,
suite,
beforeAll,
afterAll,
beforeEach,
afterEach,
onTestFinished,
onTestFailed,
getCurrentSuite,
getCurrentTest,
} from "@vitest/runner";
import { pathToFileURL } from "node:url";
/**
* Creates a minimal runner instance satisfying the VitestRunner interface.
*
* @param {object} [options={}] - Custom configuration options.
* @returns {object} Runner instance with config and importFile method.
*/
export function createSimpleRunner(options = {}) {
const userConfig = options.config || {};
const config = {
root: process.cwd(),
setupFiles: [],
passWithNoTests: true,
maxConcurrency: 5,
testTimeout: 5000,
hookTimeout: 5000,
retry: 0,
...userConfig,
sequence: {
seed: 42,
hooks: "parallel",
setupFiles: "parallel",
...(userConfig.sequence || {}),
},
};
const { config: _ignored, ...restOptions } = options;
return {
config,
async importFile(filepath, source) {
if (options.importFile) {
return options.importFile(filepath, source);
}
return import(pathToFileURL(filepath).href);
},
...restOptions,
};
}
/**
* Runs test files using a custom VitestRunner.
*
* @param {string[]} files - Array of test file paths to execute.
* @param {object} [runnerOptions={}] - Options to configure the runner.
* @returns {Promise<Array>} Array of executed test file results.
*/
export async function runTestSuite(files, runnerOptions = {}) {
const runner = createSimpleRunner(runnerOptions);
return startTests(files, runner);
}
export {
startTests,
test,
describe,
it,
suite,
beforeAll,
afterAll,
beforeEach,
afterEach,
onTestFinished,
onTestFailed,
getCurrentSuite,
getCurrentTest,
};
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import {
createSimpleRunner,
runTestSuite,
startTests,
test,
describe,
it,
beforeAll,
afterAll,
beforeEach,
afterEach,
onTestFinished,
} from "../src/index.mjs";
const tmpDir = path.join(process.cwd(), ".tmp-contract-test");
fs.mkdirSync(tmpDir, { recursive: true });
const testFile = path.join(tmpDir, "test-suite.mjs");
const logFile = path.join(tmpDir, "events.log");
const fixtureCode = `
import { describe, test, it, beforeAll, afterAll, beforeEach, afterEach, onTestFinished } from "@vitest/runner";
import assert from "node:assert/strict";
import fs from "node:fs";
const logPath = ${JSON.stringify(logFile)};
function log(msg) {
fs.appendFileSync(logPath, msg + "\\n");
}
describe("Math and lifecycle operations", () => {
let count = 0;
beforeAll(() => {
log("beforeAll:suite");
});
afterAll(() => {
log("afterAll:suite");
});
beforeEach(() => {
count++;
log("beforeEach:" + count);
});
afterEach(() => {
log("afterEach:" + count);
});
test("addition test", () => {
onTestFinished(() => {
log("onTestFinished:addition");
});
assert.strictEqual(2 + 3, 5);
});
it("multiplication test", () => {
assert.strictEqual(3 * 4, 12);
});
test("failing test case", () => {
assert.strictEqual(1, 2, "expected 1 to equal 2");
});
});
`;
try {
fs.writeFileSync(testFile, fixtureCode);
const runnerEvents = [];
const runner = createSimpleRunner({
config: { root: process.cwd() },
onBeforeRunTask(t) {
runnerEvents.push(`beforeTask:${t.name}`);
},
onAfterRunTask(t) {
runnerEvents.push(`afterTask:${t.name}:${t.result?.state}`);
},
});
// 1. startTests executes test files using a custom VitestRunner and reports pass and fail states
const files = await startTests([testFile], runner);
assert.strictEqual(files.length, 1, "startTests should execute the provided file");
const fileResult = files[0];
assert.strictEqual(fileResult.tasks.length, 1, "File should contain one root suite");
const mathSuite = fileResult.tasks[0];
assert.strictEqual(mathSuite.name, "Math and lifecycle operations");
assert.strictEqual(mathSuite.tasks.length, 3, "Suite should contain 3 test tasks");
// 2. describe and test define test suites and individual test cases
const [t1, t2, t3] = mathSuite.tasks;
assert.strictEqual(t1.name, "addition test");
assert.strictEqual(t1.result.state, "pass", "addition test should pass");
assert.strictEqual(t2.name, "multiplication test");
assert.strictEqual(t2.result.state, "pass", "multiplication test should pass");
// 3. startTests records test failures and error messages when assertions fail
assert.strictEqual(t3.name, "failing test case");
assert.strictEqual(t3.result.state, "fail", "failing test should be marked as fail");
assert.ok(t3.result.errors.length > 0, "failing test should record errors");
assert.ok(
t3.result.errors[0].message.includes("expected 1 to equal 2"),
"error message should be preserved"
);
// 4. beforeEach and afterEach hooks execute before and after each test case in a suite
const logLines = fs.readFileSync(logFile, "utf-8").trim().split("\n");
assert.ok(logLines.includes("beforeEach:1"), "beforeEach should run before first test");
assert.ok(logLines.includes("afterEach:1"), "afterEach should run after first test");
assert.ok(logLines.includes("beforeEach:2"), "beforeEach should run before second test");
assert.ok(logLines.includes("afterEach:2"), "afterEach should run after second test");
// 5. beforeAll and afterAll hooks execute once before and after all tests in a suite
assert.strictEqual(logLines[0], "beforeAll:suite", "beforeAll should execute first");
assert.strictEqual(logLines[logLines.length - 1], "afterAll:suite", "afterAll should execute last");
// 6. onTestFinished hook executes when a test completes
assert.ok(logLines.includes("onTestFinished:addition"), "onTestFinished should trigger on test completion");
// 7. runTestSuite helper convenience wrapper executes tests successfully
const wrapperResults = await runTestSuite([testFile]);
assert.strictEqual(wrapperResults.length, 1, "runTestSuite should return executed files");
console.log("All @vitest/runner contract assertions passed successfully.");
} finally {
fs.rmSync(tmpDir, { recursive: true, force: true });
}
原始种子者
匿名