CodeSampleX

Пример

@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. Контракт выполнен на node 22 · linux…

sha256:a8b88a1327c6634161c5d687fe29232ebc0cf4002ef150fc365c23958d42dfeb

Эта сеть предлагает одно: образец, который собирается. Она запустила его в песочнице и сохранила подписанную квитанцию. Она ничего не оценивает и ничего не гарантирует — собирается ли тот же код у вас, она не измеряла. Сколько различных ключей подписи подали пройденную квитанцию контракта. Один — только автор; больше одного — значит, кто-то ещё тоже собрал. Ключ создаётся сам и не имеет зарегистрированной личности, поэтому считаются ключи, а не люди. 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

Контракт

  1. startTests executes test files using a custom VitestRunner and reports pass and fail states
  2. describe and test define test suites and individual test cases
  3. startTests records test failures and error messages when assertions fail
  4. beforeEach and afterEach hooks execute before and after each test case in a suite
  5. beforeAll and afterAll hooks execute once before and after all tests in a suite
  6. onTestFinished hook executes when a test completes
  7. runTestSuite helper convenience wrapper executes tests successfully

Файлы

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.mjs
  • test/contract.mjs

Скачать артефакт с исходным кодом (tar.gz)

Исходный код

PROMPT.md
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.
csx.json
{"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"}
package-lock.json
{
  "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"
      }
    }
  }
}
package.json
{
  "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"
  }
}
spec.json
{
  "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"
  ]
}
src/index.mjs
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,
};
test/contract.mjs
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 });
}

Исходный сидер

аноним