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 alpine/x64 · docker에서 contract를 실행해…

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 });
}

오리진 시더

익명