CodeSampleX

Пример

@vitest/runner 4.1.11: describe, it, startTests

Проверенный пример — npm @vitest/runner 4.1.11: describe, it, startTests. Контракт выполнен на node 22 · linux alpine/x64 · docker и пройден.

sha256:aeae6e1f291df323c7ea54cd7627d05240b0e642ca4a0d513a8c1f6161412aeb

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

Кейс

HOW
Цель
verify pkg:npm/%40vitest/runner@4.1.11
Пакеты
Символы
  • describe
  • it
  • startTests
Создан
2026-08-28T06:16:09Z

Контракт

  1. startTests executes test suites and passes when assertions succeed
  2. describe and it define nested suites and test cases
  3. beforeAll, beforeEach, afterEach, and afterAll lifecycle hooks execute in order
  4. failed assertions mark tests with fail state and record error details

Файлы

  • PROMPT.md
  • csx.json
  • index.mjs
  • package-lock.json
  • package.json
  • spec.json
  • 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: verify pkg:npm/%40vitest/runner@4.1.11
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40vitest/runner@4.1.11
Demonstrate these symbols/APIs:
  - describe
  - it
  - startTests

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:fd0a35b188f1ca78ccc89a857db0bec10ac14847b33137675670319571cf6aad","contract":["startTests executes test suites and passes when assertions succeed","describe and it define nested suites and test cases","beforeAll, beforeEach, afterEach, and afterAll lifecycle hooks execute in order","failed assertions mark tests with fail state and record error details"],"goal":"verify pkg:npm/%40vitest/runner@4.1.11","kind":"HOW","packages":["pkg:npm/%40vitest/runner@4.1.11"],"schemaVersion":1,"symbols":["describe","it","startTests"]},"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@4.1.11"],"schemaVersion":1,"subject":"pkg:npm/%40vitest/runner@4.1.11","symbols":["describe","it","startTests"],"verifierAdapter":"node-typescript@1"}
index.mjs
import { startTests, describe, it, test, suite, beforeAll, afterAll, beforeEach, afterEach } from '@vitest/runner';

/**
 * Creates a minimal VitestRunner instance configured for programmatic test execution.
 *
 * @param {Function} importHandler - Async function called to define or import tests
 * @param {Object} [configOverrides] - Optional configuration overrides
 * @returns {Object} A VitestRunner-compatible runner object
 */
export function createRunner(importHandler, configOverrides = {}) {
  return {
    config: {
      root: process.cwd(),
      setupFiles: [],
      name: 'in-memory-runner',
      passWithNoTests: true,
      testNamePattern: undefined,
      allowOnly: true,
      sequence: {
        seed: 12345,
        hooks: 'stack',
        setupFiles: 'list',
      },
      chaiConfig: undefined,
      maxConcurrency: 5,
      testTimeout: 5000,
      hookTimeout: 5000,
      retry: 0,
      includeTaskLocation: false,
      tags: [],
      tagsFilter: undefined,
      strictTags: false,
      ...configOverrides,
    },
    importFile: async (filepath) => {
      if (typeof importHandler === 'function') {
        await importHandler(filepath);
      }
    },
  };
}

/**
 * Executes an in-memory suite definition using @vitest/runner startTests.
 *
 * @param {Function} suiteDefinition - Function defining describe/it blocks
 * @param {string} [specName='spec.test.js'] - Virtual test spec name
 * @param {Object} [configOverrides] - Optional runner configuration overrides
 * @returns {Promise<Array>} Array of collected and executed File tasks
 */
export async function runSuite(suiteDefinition, specName = 'spec.test.js', configOverrides = {}) {
  const runner = createRunner(async () => {
    suiteDefinition();
  }, configOverrides);

  return await startTests([specName], runner);
}

export {
  startTests,
  describe,
  it,
  test,
  suite,
  beforeAll,
  afterAll,
  beforeEach,
  afterEach,
};
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": "4.1.11"
      }
    },
    "node_modules/@vitest/pretty-format": {
      "version": "4.1.11",
      "resolved": "https://registry.npmjs.org/@vitest/pretty-format/-/pretty-format-4.1.11.tgz",
      "integrity": "sha512-yiZzPbGTS9Sr/JpFl8zHrcIkAofNbFV6k21vIgQN/cY/oxZeXhJv5sc/MBJ5jFKWmWs+oJHw0UXLZjmf931+Vw==",
      "license": "MIT",
      "dependencies": {
        "tinyrainbow": "^3.1.0"
      },
      "funding": {
        "url": "https://opencollective.com/vitest"
      }
    },
    "node_modules/@vitest/runner": {
      "version": "4.1.11",
      "resolved": "https://registry.npmjs.org/@vitest/runner/-/runner-4.1.11.tgz",
      "integrity": "sha512-LztvUgdwMNJMIkj3hQnnxiC2Xy1zNxq928W/xhjCLaNCzqTZOudjwbQf6v9IntZGPw132i2Lq2rgTRZHD3JHNw==",
      "license": "MIT",
      "dependencies": {
        "@vitest/utils": "4.1.11",
        "pathe": "^2.0.3"
      },
      "funding": {
        "url": "https://opencollective.com/vitest"
      }
    },
    "node_modules/@vitest/utils": {
      "version": "4.1.11",
      "resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-4.1.11.tgz",
      "integrity": "sha512-zTCVGpyFsGWBhllOyKlTw/vnr6D9qxsfSDyfbyZmTyjHw5N/VuvzHpHoQjm2ZJzn4RJgx5w4r7V0er69CmLgPQ==",
      "license": "MIT",
      "dependencies": {
        "@vitest/pretty-format": "4.1.11",
        "convert-source-map": "^2.0.0",
        "tinyrainbow": "^3.1.0"
      },
      "funding": {
        "url": "https://opencollective.com/vitest"
      }
    },
    "node_modules/convert-source-map": {
      "version": "2.0.0",
      "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-2.0.0.tgz",
      "integrity": "sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==",
      "license": "MIT"
    },
    "node_modules/pathe": {
      "version": "2.0.3",
      "resolved": "https://registry.npmjs.org/pathe/-/pathe-2.0.3.tgz",
      "integrity": "sha512-WUjGcAqP1gQacoQe+OBJsFA7Ld4DyXuUIjZ5cc75cLHvJ7dtNsTugphxIADwspS+AraAUePCKrSVtPLFj/F88w==",
      "license": "MIT"
    },
    "node_modules/tinyrainbow": {
      "version": "3.1.1",
      "resolved": "https://registry.npmjs.org/tinyrainbow/-/tinyrainbow-3.1.1.tgz",
      "integrity": "sha512-yau8yJdTt989Mm0Bd/236QnzEiPf2xLLTqUZRUJOo/3CB078LSwzei343DgtJVmfJKJE3TMINY1u42SQsP6mXw==",
      "license": "MIT",
      "engines": {
        "node": ">=14.0.0"
      }
    }
  }
}
package.json
{
  "name": "sample-vitest-runner",
  "version": "1.0.0",
  "description": "Clean-room verification sample for @vitest/runner",
  "type": "module",
  "private": true,
  "license": "MIT-0",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "@vitest/runner": "4.1.11"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/%40vitest/runner@4.1.11",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40vitest/runner@4.1.11"
  ],
  "symbols": [
    "describe",
    "it",
    "startTests"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';
import {
  createRunner,
  runSuite,
  startTests,
  describe,
  it,
  test,
  beforeAll,
  beforeEach,
  afterEach,
  afterAll,
} from '../index.mjs';

const require = createRequire(import.meta.url);

// 1. Package metadata and exported symbols verification
{
  const pkg = require('@vitest/runner/package.json');
  assert.equal(pkg.name, '@vitest/runner');
  assert.equal(pkg.version, '4.1.11');
  assert.equal(pkg.main, './dist/index.js');
  assert.equal(pkg.types, './dist/index.d.ts');

  assert.equal(typeof startTests, 'function');
  assert.equal(typeof describe, 'function');
  assert.equal(typeof it, 'function');
  assert.equal(typeof test, 'function');
  assert.equal(typeof beforeAll, 'function');
  assert.equal(typeof afterAll, 'function');
  assert.equal(typeof beforeEach, 'function');
  assert.equal(typeof afterEach, 'function');
}

// 2. startTests executes test suites and passes when assertions succeed
{
  const files = await runSuite(() => {
    describe('Math assertions', () => {
      it('adds two numbers', () => {
        assert.equal(1 + 2, 3);
      });
      test('multiplies two numbers', () => {
        assert.equal(3 * 4, 12);
      });
    });
  }, 'math.test.js');

  assert.equal(files.length, 1);
  const file = files[0];
  assert.equal(file.result?.state, 'pass');
  assert.equal(file.tasks.length, 1);

  const suiteTask = file.tasks[0];
  assert.equal(suiteTask.type, 'suite');
  assert.equal(suiteTask.name, 'Math assertions');
  assert.equal(suiteTask.result?.state, 'pass');
  assert.equal(suiteTask.tasks.length, 2);

  assert.equal(suiteTask.tasks[0].name, 'adds two numbers');
  assert.equal(suiteTask.tasks[0].result?.state, 'pass');
  assert.equal(suiteTask.tasks[1].name, 'multiplies two numbers');
  assert.equal(suiteTask.tasks[1].result?.state, 'pass');
}

// 3. describe and it define nested suites and test cases
{
  let innerExecuted = false;
  const files = await runSuite(() => {
    describe('Parent Suite', () => {
      describe('Child Suite', () => {
        it('runs inner test and passes context', (context) => {
          innerExecuted = true;
          assert.ok(context.task);
          assert.equal(context.task.name, 'runs inner test and passes context');
        });
      });
    });
  }, 'nested.test.js');

  assert.equal(innerExecuted, true);
  assert.equal(files[0].result?.state, 'pass');

  const parentSuite = files[0].tasks[0];
  assert.equal(parentSuite.name, 'Parent Suite');
  const childSuite = parentSuite.tasks[0];
  assert.equal(childSuite.name, 'Child Suite');
  assert.equal(childSuite.tasks[0].result?.state, 'pass');
}

// 4. beforeAll, beforeEach, afterEach, and afterAll lifecycle hooks execute in order
{
  const events = [];
  const files = await runSuite(() => {
    describe('Lifecycle order', () => {
      beforeAll(() => {
        events.push('beforeAll');
      });
      beforeEach(() => {
        events.push('beforeEach');
      });
      it('test step 1', () => {
        events.push('step1');
      });
      it('test step 2', () => {
        events.push('step2');
      });
      afterEach(() => {
        events.push('afterEach');
      });
      afterAll(() => {
        events.push('afterAll');
      });
    });
  }, 'lifecycle.test.js');

  assert.equal(files[0].result?.state, 'pass');
  assert.deepEqual(events, [
    'beforeAll',
    'beforeEach',
    'step1',
    'afterEach',
    'beforeEach',
    'step2',
    'afterEach',
    'afterAll',
  ]);
}

// 5. failed assertions mark tests with fail state and record error details
{
  const files = await runSuite(() => {
    describe('Error capture suite', () => {
      it('fails with intentional assertion error', () => {
        assert.equal('actual', 'expected', 'expected values to match');
      });
      it('succeeds despite sibling failure', () => {
        assert.equal(true, true);
      });
    });
  }, 'failure.test.js');

  const file = files[0];
  assert.equal(file.result?.state, 'fail');
  const suiteTask = file.tasks[0];
  assert.equal(suiteTask.result?.state, 'fail');

  const failedTask = suiteTask.tasks[0];
  assert.equal(failedTask.name, 'fails with intentional assertion error');
  assert.equal(failedTask.result?.state, 'fail');
  assert.ok(failedTask.result?.errors && failedTask.result.errors.length > 0);
  assert.match(failedTask.result.errors[0].message, /expected values to match/);

  const passingTask = suiteTask.tasks[1];
  assert.equal(passingTask.name, 'succeeds despite sibling failure');
  assert.equal(passingTask.result?.state, 'pass');
}

console.log('All @vitest/runner contract assertions passed successfully.');

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

аноним