CodeSampleX

示例

@vitest/mocker 3.2.7: RedirectedModule

已验证示例 — npm @vitest/mocker 3.2.7: RedirectedModule. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: RedirectedModule creates instance with raw, id…

sha256:f3bd66dec3b1bb300fa5c56b459527e8a4a1fca0af3c91e5794b255a2f82ef25

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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-23

案例

HOW
目标
verify @vitest/mocker.RedirectedModule in pkg:npm/%40vitest/mocker@3.2.7
包
符号
  • @vitest/mocker.RedirectedModule
创建时间
2026-08-23T14:53:30Z

契约

  1. RedirectedModule creates instance with raw, id, url, and redirect properties and type 'redirect'
  2. RedirectedModule toJSON serializes module metadata including raw, id, url, redirect, and type
  3. RedirectedModule fromJSON reconstructs a RedirectedModule instance from serialized data
  4. MockerRegistry registers and resolves RedirectedModule instances by url and id

文件

  • 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/mocker.RedirectedModule in pkg:npm/%40vitest/mocker@3.2.7
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40vitest/mocker@3.2.7
Demonstrate these symbols/APIs:
  - @vitest/mocker.RedirectedModule

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:175d879f1656ded2dd5233447ba14bbe44f0892925d54b2a63c6545ee96243c4","contract":["RedirectedModule creates instance with raw, id, url, and redirect properties and type 'redirect'","RedirectedModule toJSON serializes module metadata including raw, id, url, redirect, and type","RedirectedModule fromJSON reconstructs a RedirectedModule instance from serialized data","MockerRegistry registers and resolves RedirectedModule instances by url and id"],"goal":"verify @vitest/mocker.RedirectedModule in pkg:npm/%40vitest/mocker@3.2.7","kind":"HOW","packages":["pkg:npm/%40vitest/mocker@3.2.7"],"schemaVersion":1,"symbols":["@vitest/mocker.RedirectedModule"]},"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/mocker@3.2.7"],"schemaVersion":1,"subject":"pkg:npm/%40vitest/mocker@3.2.7","symbols":["@vitest/mocker.RedirectedModule"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-vitest-mocker-redirected-module",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-vitest-mocker-redirected-module",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "@vitest/mocker": "3.2.7"
      }
    },
    "node_modules/@jridgewell/sourcemap-codec": {
      "version": "1.5.5",
      "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.5.5.tgz",
      "integrity": "sha512-cYQ9310grqxueWbl+WuIUIaiUaDcj7WOq5fVhEljNVgRfOUhY9fy2zTvfoqWsnebh8Sl70VScFbICvJnLKB0Og==",
      "license": "MIT"
    },
    "node_modules/@types/estree": {
      "version": "1.0.9",
      "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.9.tgz",
      "integrity": "sha512-GhdPgy1el4/ImP05X05Uw4cw2/M93BCUmnEvWZNStlCzEKME4Fkk+YpoA5OiHNQmoS7Cafb8Xa3Pya8m1Qrzeg==",
      "license": "MIT"
    },
    "node_modules/@vitest/mocker": {
      "version": "3.2.7",
      "resolved": "https://registry.npmjs.org/@vitest/mocker/-/mocker-3.2.7.tgz",
      "integrity": "sha512-Trr0hYO9CM3Wj6ksWHRhK9IZpIY6wTMO5u/MqXurMxT57sWBaOPEtP3Oq60ihZuh5JsiagKfz95OcxdEP6dBrA==",
      "license": "MIT",
      "dependencies": {
        "@vitest/spy": "3.2.7",
        "estree-walker": "^3.0.3",
        "magic-string": "^0.30.17"
      },
      "funding": {
        "url": "https://opencollective.com/vitest"
      },
      "peerDependencies": {
        "msw": "^2.4.9",
        "vite": "^5.0.0 || ^6.0.0 || ^7.0.0-0"
      },
      "peerDependenciesMeta": {
        "msw": {
          "optional": true
        },
        "vite": {
          "optional": true
        }
      }
    },
    "node_modules/@vitest/spy": {
      "version": "3.2.7",
      "resolved": "https://registry.npmjs.org/@vitest/spy/-/spy-3.2.7.tgz",
      "integrity": "sha512-Q2eQGI6d2L/hBtZ0qNuKcAGid68XK6cv1xsoaIma6PaJhHPoqcEJhYpXZ/5myCMqkNgtP6UKuBhbc0nHKnrkuQ==",
      "license": "MIT",
      "dependencies": {
        "tinyspy": "^4.0.3"
      },
      "funding": {
        "url": "https://opencollective.com/vitest"
      }
    },
    "node_modules/estree-walker": {
      "version": "3.0.3",
      "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-3.0.3.tgz",
      "integrity": "sha512-7RUKfXgSMMkzt6ZuXmqapOurLGPPfgj6l9uRZ7lRGolvk0y2yocc35LdcxKC5PQZdn2DMqioAQ2NoWcrTKmm6g==",
      "license": "MIT",
      "dependencies": {
        "@types/estree": "^1.0.0"
      }
    },
    "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/tinyspy": {
      "version": "4.0.4",
      "resolved": "https://registry.npmjs.org/tinyspy/-/tinyspy-4.0.4.tgz",
      "integrity": "sha512-azl+t0z7pw/z958Gy9svOTuzqIk6xq+NSheJzn5MMWtWTFywIacg2wUlzKFGtt3cthx0r2SxMK0yzJOR0IES7Q==",
      "license": "MIT",
      "engines": {
        "node": ">=14.0.0"
      }
    }
  }
}
package.json
{
  "name": "sample-vitest-mocker-redirected-module",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "@vitest/mocker": "3.2.7"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify @vitest/mocker.RedirectedModule in pkg:npm/%40vitest/mocker@3.2.7",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40vitest/mocker@3.2.7"
  ],
  "symbols": [
    "@vitest/mocker.RedirectedModule"
  ]
}
src/index.mjs
import { RedirectedModule, MockerRegistry } from "@vitest/mocker";

export function createRedirectedModule(raw, id, url, redirect) {
  return new RedirectedModule(raw, id, url, redirect);
}

export function serializeRedirectedModule(module) {
  return module.toJSON();
}

export function deserializeRedirectedModule(data) {
  return RedirectedModule.fromJSON(data);
}

export function registerRedirect(registry, raw, id, url, redirect) {
  return registry.register("redirect", raw, id, url, redirect);
}

export { RedirectedModule, MockerRegistry };
test/contract.mjs
import assert from "node:assert/strict";
import { RedirectedModule, MockerRegistry } from "@vitest/mocker";
import {
  createRedirectedModule,
  serializeRedirectedModule,
  deserializeRedirectedModule,
  registerRedirect
} from "../src/index.mjs";

// Test 1: RedirectedModule creates instance with raw, id, url, and redirect properties and type 'redirect'
{
  const raw = "axios";
  const id = "/mock/path/node_modules/axios/index.js";
  const url = "/@mock/axios";
  const redirect = "/mock/path/mocks/axios.js";

  const module = createRedirectedModule(raw, id, url, redirect);
  assert.equal(module instanceof RedirectedModule, true);
  assert.equal(module.type, "redirect");
  assert.equal(module.raw, raw);
  assert.equal(module.id, id);
  assert.equal(module.url, url);
  assert.equal(module.redirect, redirect);
}

// Test 2: RedirectedModule toJSON serializes module metadata including raw, id, url, redirect, and type
{
  const raw = "lodash";
  const id = "/project/node_modules/lodash/index.js";
  const url = "/@mock/lodash";
  const redirect = "/project/mocks/lodash.js";

  const module = new RedirectedModule(raw, id, url, redirect);
  const serialized = serializeRedirectedModule(module);

  assert.deepEqual(serialized, {
    type: "redirect",
    url: "/@mock/lodash",
    raw: "lodash",
    id: "/project/node_modules/lodash/index.js",
    redirect: "/project/mocks/lodash.js"
  });
}

// Test 3: RedirectedModule fromJSON reconstructs a RedirectedModule instance from serialized data
{
  const serialized = {
    type: "redirect",
    url: "/@mock/service",
    raw: "my-service",
    id: "/app/src/service.js",
    redirect: "/app/src/__mocks__/service.js"
  };

  const reconstructed = deserializeRedirectedModule(serialized);
  assert.equal(reconstructed instanceof RedirectedModule, true);
  assert.equal(reconstructed.type, "redirect");
  assert.equal(reconstructed.raw, serialized.raw);
  assert.equal(reconstructed.id, serialized.id);
  assert.equal(reconstructed.url, serialized.url);
  assert.equal(reconstructed.redirect, serialized.redirect);
}

// Test 4: MockerRegistry registers and resolves RedirectedModule instances by url and id
{
  const registry = new MockerRegistry();
  const raw = "database";
  const id = "/app/src/db.js";
  const url = "/@mock/db";
  const redirect = "/app/test/mock-db.js";

  const registered = registerRedirect(registry, raw, id, url, redirect);
  assert.equal(registered instanceof RedirectedModule, true);
  assert.equal(registered.type, "redirect");

  assert.equal(registry.has(url), true);
  assert.equal(registry.get(url), registered);
  assert.equal(registry.getById(id), registered);

  // Deserialized registration
  const serialized = registered.toJSON();
  const registry2 = new MockerRegistry();
  const registeredFromJSON = registry2.register(serialized);
  assert.equal(registeredFromJSON instanceof RedirectedModule, true);
  assert.equal(registry2.has(url), true);
  assert.equal(registry2.get(url)?.redirect, redirect);
  assert.equal(registry2.getById(id)?.redirect, redirect);
}

console.log("All @vitest/mocker.RedirectedModule contract tests passed successfully.");

原始种子者

匿名