CodeSampleX

샘플

@vitest/snapshot 4.1.11: SnapshotState

검증된 샘플 — npm @vitest/snapshot 4.1.11: SnapshotState. node 22 · linux debian/x64 · docker에서 contract를 실행해 통과했습니다: SnapshotState.create initializes a state…

sha256:d5365bc9abeece918b15736e46ec655a73529875e05a639896e68d9354af4d78

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. MIT-0

실행 증거

선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.

증거 기준
서명된 컨트랙트 통과
검증 영수증
1
빌드한 서명 키
1
선언된 환경 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

검증 실행 환경

환경 컨트랙트 단계 실행일
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-06

케이스

HOW
목표
verify @vitest/snapshot.SnapshotState in pkg:npm/%40vitest/snapshot@4.1.11
패키지
심벌
  • @vitest/snapshot.SnapshotState
환경
node 22.23.2
생성일
2026-09-06T10:59:01Z

컨트랙트

  1. SnapshotState.create initializes a state with given testFilePath and snapshot environment options
  2. SnapshotState.prototype.match records new snapshots and increments added counter when updating snapshots
  3. SnapshotState.prototype.match verifies matching snapshot values and records mismatches
  4. SnapshotState.prototype.save persists snapshot file content via the snapshot environment
  5. SnapshotState.prototype.pack returns structured summary with added, matched, unmatched, and unchecked counts

파일

  • 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: verify @vitest/snapshot.SnapshotState in pkg:npm/%40vitest/snapshot@4.1.11
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40vitest/snapshot@4.1.11
Demonstrate these symbols/APIs:
  - @vitest/snapshot.SnapshotState
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:77667a44a8ea7913a40add3efbf6808cc4268a06a876859ab6831b4d6ad3f9ed","constraints":{"executionContext":"node"},"contract":["SnapshotState.create initializes a state with given testFilePath and snapshot environment options","SnapshotState.prototype.match records new snapshots and increments added counter when updating snapshots","SnapshotState.prototype.match verifies matching snapshot values and records mismatches","SnapshotState.prototype.save persists snapshot file content via the snapshot environment","SnapshotState.prototype.pack returns structured summary with added, matched, unmatched, and unchecked counts"],"goal":"verify @vitest/snapshot.SnapshotState in pkg:npm/%40vitest/snapshot@4.1.11","kind":"HOW","packages":["pkg:npm/%40vitest/snapshot@4.1.11"],"schemaVersion":1,"symbols":["@vitest/snapshot.SnapshotState"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"esm","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/%40vitest/snapshot@4.1.11"],"schemaVersion":1,"subject":"pkg:npm/%40vitest/snapshot@4.1.11","symbols":["@vitest/snapshot.SnapshotState"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-vitest-snapshot-snapshot-state",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-vitest-snapshot-snapshot-state",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "@vitest/snapshot": "4.1.11"
      }
    },
    "node_modules/@jridgewell/sourcemap-codec": {
      "version": "1.6.0",
      "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.6.0.tgz",
      "integrity": "sha512-T7jf+5zgsZHwNJ4lvQ7/aezbyk0nNX+zJVWpmHA7VYsEx7a7qr5Rg5IbtJFqkgze5Y2sruq1RUY8Q837Od7iFw==",
      "license": "MIT"
    },
    "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/snapshot": {
      "version": "4.1.11",
      "resolved": "https://registry.npmjs.org/@vitest/snapshot/-/snapshot-4.1.11.tgz",
      "integrity": "sha512-pN7ikn1ON7h8ee4gIAp4AzyK+zBtJPzVbqOgu5LCEh4VaJVbPQcgYQYJIMGQPXVeJJq1fnfazis7a5pFNPahog==",
      "license": "MIT",
      "dependencies": {
        "@vitest/pretty-format": "4.1.11",
        "@vitest/utils": "4.1.11",
        "magic-string": "^0.30.21",
        "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/magic-string": {
      "version": "0.30.21",
      "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.21.tgz",
      "integrity": "sha512-vd2F4YUyEXKGcLHoq+TEyCjxueSeHnFxyyjNp80yg0XV4vUhnDer/lvvlqM/arB5bXQN5K2/3oinyCRyx8T2CQ==",
      "license": "MIT",
      "dependencies": {
        "@jridgewell/sourcemap-codec": "^1.5.5"
      }
    },
    "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-snapshot-snapshot-state",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "@vitest/snapshot": "4.1.11"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify @vitest/snapshot.SnapshotState in pkg:npm/%40vitest/snapshot@4.1.11",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40vitest/snapshot@4.1.11"
  ],
  "symbols": [
    "@vitest/snapshot.SnapshotState"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/index.mjs
import { SnapshotState } from "@vitest/snapshot";

export async function createSnapshotState(testFilePath, options) {
  return await SnapshotState.create(testFilePath, options);
}

export function matchSnapshot(state, options) {
  return state.match(options);
}

export async function saveSnapshotState(state) {
  return await state.save();
}

export async function packSnapshotResult(state) {
  return await state.pack();
}

export { SnapshotState };
test/contract.mjs
import assert from "node:assert/strict";
import { SnapshotState } from "@vitest/snapshot";
import {
  createSnapshotState,
  matchSnapshot,
  saveSnapshotState,
  packSnapshotResult
} from "../src/index.mjs";

function createInMemorySnapshotEnvironment() {
  const store = new Map();
  return {
    store,
    env: {
      getVersion: () => "1",
      getHeader: () => "// Vitest Snapshot v1\n",
      resolveRawPath: async (testPath, rawPath) => `${testPath}.${rawPath}`,
      resolvePath: async (filepath) => `${filepath}.snap`,
      prepareDirectory: async () => {},
      saveSnapshotFile: async (filepath, snapshot) => {
        store.set(filepath, snapshot);
      },
      readSnapshotFile: async (filepath) => {
        return store.get(filepath) || null;
      },
      removeSnapshotFile: async (filepath) => {
        store.delete(filepath);
      },
    },
  };
}

// Test 1: SnapshotState.create initializes a state with given testFilePath and snapshot environment options
{
  const { env } = createInMemorySnapshotEnvironment();
  const testFilePath = "/workspace/example.test.js";
  const state = await createSnapshotState(testFilePath, {
    updateSnapshot: "all",
    snapshotEnvironment: env,
  });

  assert.equal(state instanceof SnapshotState, true);
  assert.equal(state.testFilePath, testFilePath);
  assert.equal(state.snapshotPath, `${testFilePath}.snap`);
  assert.equal(state.added.total(), 0);
  assert.equal(state.matched.total(), 0);
  assert.equal(state.unmatched.total(), 0);
}

// Test 2: SnapshotState.prototype.match records new snapshots and increments added counter when updating snapshots
{
  const { env } = createInMemorySnapshotEnvironment();
  const testFilePath = "/workspace/example.test.js";
  const state = await createSnapshotState(testFilePath, {
    updateSnapshot: "all",
    snapshotEnvironment: env,
  });

  const result = matchSnapshot(state, {
    testId: "suite > test1",
    testName: "suite > test1",
    received: { status: "success", count: 3 },
    isInline: false,
  });

  assert.equal(result.pass, true);
  assert.equal(result.key, "suite > test1 1");
  assert.equal(state.added.total(), 1);
  assert.equal(state.matched.total(), 0);
}

// Test 3: SnapshotState.prototype.match verifies matching snapshot values and records mismatches
{
  const { env } = createInMemorySnapshotEnvironment();
  const testFilePath = "/workspace/example.test.js";

  // First pass: create and save snapshot
  const state1 = await createSnapshotState(testFilePath, {
    updateSnapshot: "all",
    snapshotEnvironment: env,
  });
  state1.match({
    testId: "suite > test2",
    testName: "suite > test2",
    received: { message: "hello" },
    isInline: false,
  });
  await saveSnapshotState(state1);

  // Second pass: load saved snapshot with updateSnapshot: "none"
  const state2 = await createSnapshotState(testFilePath, {
    updateSnapshot: "none",
    snapshotEnvironment: env,
  });

  // Successful match
  const matchResult = state2.match({
    testId: "suite > test2",
    testName: "suite > test2",
    received: { message: "hello" },
    isInline: false,
  });
  assert.equal(matchResult.pass, true);
  assert.equal(state2.matched.total(), 1);
  assert.equal(state2.unmatched.total(), 0);

  // Mismatching received value
  const mismatchResult = state2.match({
    testId: "suite > test2",
    testName: "suite > test2",
    received: { message: "world" },
    isInline: false,
  });
  assert.equal(mismatchResult.pass, false);
  assert.equal(state2.unmatched.total(), 1);
}

// Test 4: SnapshotState.prototype.save persists snapshot file content via the snapshot environment
{
  const { env, store } = createInMemorySnapshotEnvironment();
  const testFilePath = "/workspace/save-test.test.js";
  const state = await createSnapshotState(testFilePath, {
    updateSnapshot: "all",
    snapshotEnvironment: env,
  });

  state.match({
    testId: "test_save",
    testName: "test_save",
    received: [1, 2, 3],
    isInline: false,
  });

  const saveStatus = await saveSnapshotState(state);
  assert.equal(saveStatus.saved, true);
  assert.equal(saveStatus.deleted, false);
  assert.equal(store.has(`${testFilePath}.snap`), true);
  assert.match(store.get(`${testFilePath}.snap`), /exports\[`test_save 1`\] = `\s*\[\s*1,\s*2,\s*3,\s*\]\s*`;/);
}

// Test 5: SnapshotState.prototype.pack returns structured summary with added, matched, unmatched, and unchecked counts
{
  const { env } = createInMemorySnapshotEnvironment();
  const testFilePath = "/workspace/pack-test.test.js";
  const state = await createSnapshotState(testFilePath, {
    updateSnapshot: "all",
    snapshotEnvironment: env,
  });

  state.match({
    testId: "test_pack",
    testName: "test_pack",
    received: "test-value",
    isInline: false,
  });

  const packResult = await packSnapshotResult(state);
  assert.equal(packResult.filepath, testFilePath);
  assert.equal(packResult.added, 1);
  assert.equal(packResult.matched, 0);
  assert.equal(packResult.unmatched, 0);
  assert.equal(packResult.fileDeleted, false);
  assert.equal(Array.isArray(packResult.uncheckedKeys), true);
}

console.log("All @vitest/snapshot.SnapshotState contract tests passed successfully.");

오리진 시더

익명