CodeSampleX

サンプル

@sinonjs/fake-timers 10.3.0: install and advance timers using FakeTimers clock

検証済みサンプル — npm @sinonjs/fake-timers 10.3.0: install and advance timers using FakeTimers clock. node 22 · linux alpine/x64 · docker で contract を実行し、成功しました.

sha256:b3ab76bfdef251e134e8dc5ed5cddad8be6ffebf31eddfd3fbc5dfdafc732c0c

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 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
ゴール
install and advance timers using FakeTimers clock
パッケージ
シンボル
  • Clock.tick
  • Clock.uninstall
  • install
作成日
2026-08-26T07:42:35Z

コントラクト

  1. FakeTimers.install replaces global timer functions and initializes now to the configured timestamp
  2. clock.tick advances time and triggers scheduled setTimeout callbacks synchronously
  3. clock.tick handles repeating setInterval callbacks and stops after clearInterval
  4. clock.runAll executes all scheduled timers until the queue is empty
  5. clock.setSystemTime adjusts current clock time without executing intermediate scheduled timers
  6. FakeTimers.createClock creates an isolated clock instance without mutating globals
  7. clock.uninstall restores all overridden globals cleanly

ファイル

  • PROMPT.md
  • csx.json
  • 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: install and advance timers using FakeTimers clock
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40sinonjs/fake-timers@10.3.0
Demonstrate these symbols/APIs:
  - Clock.tick
  - Clock.uninstall
  - install

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:ed62146403969e3d88d7669a0d5040adaddcd3ba3f7c3a34e5d9420e202a9ac4","contract":["FakeTimers.install replaces global timer functions and initializes now to the configured timestamp","clock.tick advances time and triggers scheduled setTimeout callbacks synchronously","clock.tick handles repeating setInterval callbacks and stops after clearInterval","clock.runAll executes all scheduled timers until the queue is empty","clock.setSystemTime adjusts current clock time without executing intermediate scheduled timers","FakeTimers.createClock creates an isolated clock instance without mutating globals","clock.uninstall restores all overridden globals cleanly"],"goal":"install and advance timers using FakeTimers clock","kind":"HOW","packages":["pkg:npm/%40sinonjs/fake-timers@10.3.0"],"schemaVersion":1,"symbols":["Clock.tick","Clock.uninstall","install"]},"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/%40sinonjs/fake-timers@10.3.0"],"schemaVersion":1,"subject":"pkg:npm/%40sinonjs/fake-timers@10.3.0","symbols":["Clock.tick","Clock.uninstall","install"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-fake-timers",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-fake-timers",
      "version": "1.0.0",
      "dependencies": {
        "@sinonjs/fake-timers": "10.3.0"
      }
    },
    "node_modules/@sinonjs/commons": {
      "version": "3.0.1",
      "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-3.0.1.tgz",
      "integrity": "sha512-K3mCHKQ9sVh8o1C9cxkwxaOmXoAMlDxC1mYyHrjqOWEcBjYr76t96zL2zlj5dUGZ3HSw240X1qgH3Mjf1yJWpQ==",
      "license": "BSD-3-Clause",
      "dependencies": {
        "type-detect": "4.0.8"
      }
    },
    "node_modules/@sinonjs/fake-timers": {
      "version": "10.3.0",
      "resolved": "https://registry.npmjs.org/@sinonjs/fake-timers/-/fake-timers-10.3.0.tgz",
      "integrity": "sha512-V4BG07kuYSUkTCSBHG8G8TNhM+F19jXFWnQtzj+we8DrkpSBCee9Z3Ms8yiGer/dlmhe35/Xdgyo3/0rQKg7YA==",
      "license": "BSD-3-Clause",
      "dependencies": {
        "@sinonjs/commons": "^3.0.0"
      }
    },
    "node_modules/type-detect": {
      "version": "4.0.8",
      "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz",
      "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==",
      "license": "MIT",
      "engines": {
        "node": ">=4"
      }
    }
  }
}
package.json
{
  "name": "sample-fake-timers",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "dependencies": {
    "@sinonjs/fake-timers": "10.3.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "install and advance timers using FakeTimers clock",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40sinonjs/fake-timers@10.3.0"
  ],
  "symbols": [
    "Clock.tick",
    "Clock.uninstall",
    "install"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import FakeTimers from '@sinonjs/fake-timers';

// 1. FakeTimers.install replaces global timer functions and initializes now to the configured timestamp
{
  const initialTime = 1600000000000;
  const originalSetTimeout = globalThis.setTimeout;
  const clock = FakeTimers.install({ now: initialTime, toFake: ['setTimeout', 'clearTimeout', 'Date'] });

  assert.notEqual(globalThis.setTimeout, originalSetTimeout);
  assert.equal(Date.now(), initialTime);
  assert.equal(clock.now, initialTime);

  clock.uninstall();
  assert.equal(globalThis.setTimeout, originalSetTimeout);
}

// 2. clock.tick advances time and triggers scheduled setTimeout callbacks synchronously
{
  const clock = FakeTimers.install();
  let called = false;
  let callTime = 0;

  setTimeout(() => {
    called = true;
    callTime = clock.now;
  }, 150);

  clock.tick(100);
  assert.equal(called, false);
  assert.equal(clock.now, 100);

  clock.tick(50);
  assert.equal(called, true);
  assert.equal(callTime, 150);
  assert.equal(clock.now, 150);

  clock.uninstall();
}

// 3. clock.tick handles repeating setInterval callbacks and stops after clearInterval
{
  const clock = FakeTimers.install();
  let count = 0;

  const intervalId = setInterval(() => {
    count++;
  }, 50);

  clock.tick(150);
  assert.equal(count, 3);

  clearInterval(intervalId);
  clock.tick(100);
  assert.equal(count, 3);

  clock.uninstall();
}

// 4. clock.runAll executes all scheduled timers until the queue is empty
{
  const clock = FakeTimers.install();
  const order = [];

  setTimeout(() => order.push('second'), 200);
  setTimeout(() => order.push('first'), 100);

  clock.runAll();
  assert.deepEqual(order, ['first', 'second']);
  assert.equal(clock.now, 200);

  clock.uninstall();
}

// 5. clock.setSystemTime adjusts current clock time without executing intermediate scheduled timers
{
  const clock = FakeTimers.install({ now: 1000 });
  let called = false;

  setTimeout(() => {
    called = true;
  }, 500);

  clock.setSystemTime(2000);
  assert.equal(clock.now, 2000);
  assert.equal(called, false);

  clock.tick(500);
  assert.equal(called, true);

  clock.uninstall();
}

// 6. FakeTimers.createClock creates an isolated clock instance without mutating globals
{
  const originalSetTimeout = globalThis.setTimeout;
  const clock = FakeTimers.createClock(500);

  assert.equal(globalThis.setTimeout, originalSetTimeout);
  assert.equal(clock.now, 500);

  let executed = false;
  clock.setTimeout(() => {
    executed = true;
  }, 100);

  clock.tick(100);
  assert.equal(executed, true);
  assert.equal(clock.now, 600);
}

// 7. clock.uninstall restores all overridden globals cleanly
{
  const originalDate = globalThis.Date;
  const originalClearTimeout = globalThis.clearTimeout;
  const clock = FakeTimers.install();

  assert.notEqual(globalThis.Date, originalDate);
  assert.notEqual(globalThis.clearTimeout, originalClearTimeout);

  clock.uninstall();
  assert.equal(globalThis.Date, originalDate);
  assert.equal(globalThis.clearTimeout, originalClearTimeout);
}

オリジンシーダー

匿名