CodeSampleX

Sample

@sinonjs/fake-timers 10.3.0: withGlobal

Verified sample for npm @sinonjs/fake-timers 10.3.0: withGlobal. The contract ran on node 22 · linux alpine/x64 · docker and passed.

sha256:74c73af412a84b53fc22939a8ba115de693938a7869da88466963307183bc743

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
1
Signing keys that built it
1
Declared environment node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Verification-run environments

Environment Contract Stages Run
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

Case

HOW
Goal
verify withGlobal in pkg:npm/%40sinonjs/fake-timers@10.3.0
Packages
Symbols
  • withGlobal
Environment
node 22.23.2
Created
2026-08-26T10:36:43Z

Contract

  1. withGlobal returns a FakeTimers instance bound to the provided custom global object
  2. install on withGlobal context overrides timer functions on custom global without modifying ambient globalThis
  3. custom global setTimeout executes callbacks when clock is advanced with tick
  4. custom global setInterval executes repeatedly at intervals until clearInterval
  5. clock runAll exhausts all pending timers on the bound custom global context
  6. clock setSystemTime adjusts custom global Date now without triggering pending timers
  7. clock.uninstall restores original timer functions on the custom global object
  8. withGlobal createClock creates an independent clock within the custom global context
  9. withGlobal allows re-binding to a different target global context

Files

  • PROMPT.md
  • csx.json
  • index.mjs
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs

Download the source artifact (tar.gz)

Source

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 withGlobal in pkg:npm/%40sinonjs/fake-timers@10.3.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40sinonjs/fake-timers@10.3.0
Demonstrate these symbols/APIs:
  - withGlobal
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:68872acf4aa00a124f8929fdbdcfe2da23e1df9418ab9058fccc2beec9b49e30","constraints":{"executionContext":"node"},"contract":["withGlobal returns a FakeTimers instance bound to the provided custom global object","install on withGlobal context overrides timer functions on custom global without modifying ambient globalThis","custom global setTimeout executes callbacks when clock is advanced with tick","custom global setInterval executes repeatedly at intervals until clearInterval","clock runAll exhausts all pending timers on the bound custom global context","clock setSystemTime adjusts custom global Date now without triggering pending timers","clock.uninstall restores original timer functions on the custom global object","withGlobal createClock creates an independent clock within the custom global context","withGlobal allows re-binding to a different target global context"],"goal":"verify withGlobal in pkg:npm/%40sinonjs/fake-timers@10.3.0","kind":"HOW","packages":["pkg:npm/%40sinonjs/fake-timers@10.3.0"],"schemaVersion":1,"symbols":["withGlobal"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","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":["withGlobal"],"verifierAdapter":"node-typescript@1"}
index.mjs
import FakeTimers from '@sinonjs/fake-timers';

export const { withGlobal } = FakeTimers;

/**
 * Creates a scoped FakeTimers instance bound to a custom global context.
 *
 * @param {object} customGlobal - Global object to bind timers to
 * @returns {object} Scoped FakeTimers instance with install, createClock, withGlobal
 */
export function bindCustomGlobal(customGlobal) {
  return withGlobal(customGlobal);
}

export default {
  withGlobal,
  bindCustomGlobal,
};
package-lock.json
{
  "name": "sample-fake-timers-with-global",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-fake-timers-with-global",
      "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-with-global",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "dependencies": {
    "@sinonjs/fake-timers": "10.3.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify withGlobal in pkg:npm/%40sinonjs/fake-timers@10.3.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40sinonjs/fake-timers@10.3.0"
  ],
  "symbols": [
    "withGlobal"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract.mjs
import assert from 'node:assert/strict';
import FakeTimers, { withGlobal, bindCustomGlobal } from '../index.mjs';

function createMockGlobal() {
  return {
    Date: globalThis.Date,
    setTimeout: globalThis.setTimeout,
    clearTimeout: globalThis.clearTimeout,
    setInterval: globalThis.setInterval,
    clearInterval: globalThis.clearInterval,
    setImmediate: globalThis.setImmediate,
    clearImmediate: globalThis.clearImmediate,
    hrtime: globalThis.process.hrtime,
    performance: globalThis.performance,
  };
}

// Contract 1: withGlobal returns a FakeTimers instance bound to the provided custom global object
{
  const mockGlobal = createMockGlobal();
  const bound = withGlobal(mockGlobal);
  assert.strictEqual(typeof withGlobal, 'function');
  assert.strictEqual(typeof bound, 'object');
  assert.strictEqual(typeof bound.install, 'function');
  assert.strictEqual(typeof bound.createClock, 'function');
  assert.strictEqual(typeof bound.withGlobal, 'function');
  assert.strictEqual(typeof bound.timers, 'object');
}

// Contract 2: install on withGlobal context overrides timer functions on custom global without modifying ambient globalThis
{
  const mockGlobal = createMockGlobal();
  const originalGlobalSetTimeout = globalThis.setTimeout;
  const originalMockSetTimeout = mockGlobal.setTimeout;

  const bound = bindCustomGlobal(mockGlobal);
  const clock = bound.install({ now: 2000 });

  assert.notStrictEqual(mockGlobal.setTimeout, originalMockSetTimeout);
  assert.strictEqual(globalThis.setTimeout, originalGlobalSetTimeout);
  assert.strictEqual(mockGlobal.Date.now(), 2000);

  clock.uninstall();
  assert.strictEqual(mockGlobal.setTimeout, originalMockSetTimeout);
}

// Contract 3: custom global setTimeout executes callbacks when clock is advanced with tick
{
  const mockGlobal = createMockGlobal();
  const bound = withGlobal(mockGlobal);
  const clock = bound.install({ now: 1000 });

  let called = false;
  let receivedArgs = [];
  mockGlobal.setTimeout((a, b) => {
    called = true;
    receivedArgs = [a, b];
  }, 300, 'val1', 42);

  assert.strictEqual(called, false);
  clock.tick(299);
  assert.strictEqual(called, false);
  assert.strictEqual(mockGlobal.Date.now(), 1299);

  clock.tick(1);
  assert.strictEqual(called, true);
  assert.deepEqual(receivedArgs, ['val1', 42]);
  assert.strictEqual(mockGlobal.Date.now(), 1300);

  clock.uninstall();
}

// Contract 4: custom global setInterval executes repeatedly at intervals until clearInterval
{
  const mockGlobal = createMockGlobal();
  const bound = withGlobal(mockGlobal);
  const clock = bound.install({ now: 0 });

  let counter = 0;
  const intervalId = mockGlobal.setInterval(() => {
    counter++;
  }, 100);

  clock.tick(250);
  assert.strictEqual(counter, 2);

  mockGlobal.clearInterval(intervalId);
  clock.tick(300);
  assert.strictEqual(counter, 2);

  clock.uninstall();
}

// Contract 5: clock runAll exhausts all pending timers on the bound custom global context
{
  const mockGlobal = createMockGlobal();
  const bound = withGlobal(mockGlobal);
  const clock = bound.install({ now: 0 });

  const executionLog = [];
  mockGlobal.setTimeout(() => executionLog.push('second'), 200);
  mockGlobal.setTimeout(() => executionLog.push('first'), 50);
  mockGlobal.setTimeout(() => executionLog.push('third'), 350);

  clock.runAll();
  assert.deepEqual(executionLog, ['first', 'second', 'third']);
  assert.strictEqual(mockGlobal.Date.now(), 350);

  clock.uninstall();
}

// Contract 6: clock setSystemTime adjusts custom global Date now without triggering pending timers
{
  const mockGlobal = createMockGlobal();
  const bound = withGlobal(mockGlobal);
  const clock = bound.install({ now: 1000 });

  let triggered = false;
  mockGlobal.setTimeout(() => {
    triggered = true;
  }, 500);

  clock.setSystemTime(5000);
  assert.strictEqual(mockGlobal.Date.now(), 5000);
  assert.strictEqual(triggered, false);

  clock.tick(500);
  assert.strictEqual(triggered, true);

  clock.uninstall();
}

// Contract 7: clock.uninstall restores original timer functions on the custom global object
{
  const mockGlobal = createMockGlobal();
  const originalDate = mockGlobal.Date;
  const originalSetTimeout = mockGlobal.setTimeout;
  const originalClearTimeout = mockGlobal.clearTimeout;
  const originalSetInterval = mockGlobal.setInterval;
  const originalClearInterval = mockGlobal.clearInterval;

  const bound = withGlobal(mockGlobal);
  const clock = bound.install();

  assert.notStrictEqual(mockGlobal.Date, originalDate);
  assert.notStrictEqual(mockGlobal.setTimeout, originalSetTimeout);

  clock.uninstall();

  assert.strictEqual(mockGlobal.Date, originalDate);
  assert.strictEqual(mockGlobal.setTimeout, originalSetTimeout);
  assert.strictEqual(mockGlobal.clearTimeout, originalClearTimeout);
  assert.strictEqual(mockGlobal.setInterval, originalSetInterval);
  assert.strictEqual(mockGlobal.clearInterval, originalClearInterval);
}

// Contract 8: withGlobal createClock creates an independent clock within the custom global context
{
  const mockGlobal = createMockGlobal();
  const bound = withGlobal(mockGlobal);
  const clock = bound.createClock(1500);

  assert.strictEqual(clock.now, 1500);
  let ran = false;
  clock.setTimeout(() => {
    ran = true;
  }, 100);

  clock.tick(100);
  assert.strictEqual(ran, true);
  assert.strictEqual(clock.now, 1600);
}

// Contract 9: withGlobal allows re-binding to a different target global context
{
  const contextA = createMockGlobal();
  const contextB = createMockGlobal();

  const boundA = withGlobal(contextA);
  const boundB = boundA.withGlobal(contextB);

  const clockB = boundB.install({ now: 3000 });
  assert.strictEqual(contextB.Date.now(), 3000);
  assert.strictEqual(contextA.Date, globalThis.Date);

  clockB.uninstall();
}

console.log('All contracts passed.');

Origin Seeder

anonymous