示例
@jest/fake-timers 29.7.0: ModernFakeTimers, LegacyFakeTimers
已验证示例 — npm @jest/fake-timers 29.7.0: ModernFakeTimers, LegacyFakeTimers. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过.
sha256:f3a9f946a4a29a2eac730a1e515046fa2eb34a226bc6d89d8cd2386cfc306339
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。
MIT-0
执行证据
声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。
- 证据依据
- 签名契约通过
- 验证回执
- 1
- 构建过它的签名密钥
- 1
声明的环境
node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10
验证运行环境
| 环境 | 契约 | 阶段 | 运行日期 |
|---|---|---|---|
| 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/%40jest/fake-timers@29.7.0
- 符号
-
- ModernFakeTimers
- LegacyFakeTimers
- 环境
- node 22.23.2
- 创建时间
- 2026-08-28T03:09:44Z
契约
- ModernFakeTimers installs fake timer mocks with useFakeTimers and restores original implementations on useRealTimers
- advanceTimersByTime advances virtual clock and executes scheduled timers synchronously
- advanceTimersByTimeAsync advances virtual clock asynchronously and resolves promises
- advanceTimersToNextTimer and advanceTimersToNextTimerAsync progress virtual time to the next pending timer
- runAllTimers and runAllTimersAsync execute all queued and recursively scheduled timers
- runOnlyPendingTimers and runOnlyPendingTimersAsync execute currently queued timers without running newly scheduled nested timers
- clearAllTimers and getTimerCount track and cancel active scheduled timers
- setSystemTime, getRealSystemTime, now, and reset manage and simulate system timestamps
- LegacyFakeTimers installs timer mocks, manages clock advancement, and executes runWithRealTimers callback
- withFakeTimers and withFakeTimersAsync helpers safely execute scoped synchronous and asynchronous callbacks
文件
- PROMPT.md
- csx.json
- index.js
- package-lock.json
- package.json
- spec.json
- test/contract.js
源代码
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/%40jest/fake-timers@29.7.0
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:npm/%40jest/fake-timers@29.7.0
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.
{"case":{"caseId":"case:sha256:10661d99357f799bcaf336b869eb88bb840578a2515bdbcb98d20acdf2b3f28f","constraints":{"executionContext":"node"},"contract":["ModernFakeTimers installs fake timer mocks with useFakeTimers and restores original implementations on useRealTimers","advanceTimersByTime advances virtual clock and executes scheduled timers synchronously","advanceTimersByTimeAsync advances virtual clock asynchronously and resolves promises","advanceTimersToNextTimer and advanceTimersToNextTimerAsync progress virtual time to the next pending timer","runAllTimers and runAllTimersAsync execute all queued and recursively scheduled timers","runOnlyPendingTimers and runOnlyPendingTimersAsync execute currently queued timers without running newly scheduled nested timers","clearAllTimers and getTimerCount track and cancel active scheduled timers","setSystemTime, getRealSystemTime, now, and reset manage and simulate system timestamps","LegacyFakeTimers installs timer mocks, manages clock advancement, and executes runWithRealTimers callback","withFakeTimers and withFakeTimersAsync helpers safely execute scoped synchronous and asynchronous callbacks"],"goal":"verify pkg:npm/%40jest/fake-timers@29.7.0","kind":"HOW","packages":["pkg:npm/%40jest/fake-timers@29.7.0"],"schemaVersion":1,"symbols":["ModernFakeTimers","LegacyFakeTimers"]},"contractCommand":["node","test/contract.js"],"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/%40jest/fake-timers@29.7.0"],"schemaVersion":1,"subject":"pkg:npm/%40jest/fake-timers@29.7.0","symbols":["ModernFakeTimers","LegacyFakeTimers"],"verifierAdapter":"node-typescript@1"}
'use strict';
const { ModernFakeTimers, LegacyFakeTimers } = require('@jest/fake-timers');
/**
* Creates and configures a ModernFakeTimers instance.
* @param {Object} [options]
* @param {Object} [options.global] - Target global object (defaults to globalThis)
* @param {Object} [options.config] - Jest project configuration
* @returns {ModernFakeTimers}
*/
function createModernFakeTimers(options = {}) {
const {
global = globalThis,
config = { rootDir: process.cwd() }
} = options;
return new ModernFakeTimers({ global, config });
}
/**
* Creates and configures a LegacyFakeTimers instance with a mock environment.
* @param {Object} [options]
* @param {Object} [options.global] - Custom global object to mock
* @param {Object} [options.config] - Stack trace configuration
* @param {Object} [options.timerConfig] - Timer ref/id mappings
* @param {Object} [options.moduleMocker] - Module mocker instance
* @param {number} [options.maxLoops] - Maximum execution loops
* @returns {{ timers: LegacyFakeTimers, global: Object }}
*/
function createLegacyFakeTimers(options = {}) {
const targetGlobal = {
clearImmediate: globalThis.clearImmediate,
clearInterval: globalThis.clearInterval,
clearTimeout: globalThis.clearTimeout,
setImmediate: globalThis.setImmediate,
setInterval: globalThis.setInterval,
setTimeout: globalThis.setTimeout,
process: { nextTick: process.nextTick },
console: console,
...(options.global || {})
};
const moduleMocker = options.moduleMocker || {
fn: (impl) => {
const mockFn = (...args) => (impl ? impl(...args) : undefined);
return mockFn;
}
};
const timerConfig = options.timerConfig || {
idToRef: (id) => id,
refToId: (ref) => ref
};
const config = options.config || { rootDir: process.cwd() };
const timers = new LegacyFakeTimers({
global: targetGlobal,
moduleMocker,
timerConfig,
config,
maxLoops: options.maxLoops
});
return { timers, global: targetGlobal };
}
/**
* Executes a callback within a scoped ModernFakeTimers context and ensures clean restoration.
* @param {Function} callback - Function receiving fake timers instance
* @param {Object} [config] - Fake timers configuration options
* @returns {*} Result of the callback
*/
function withFakeTimers(callback, config = {}) {
const fakeTimers = createModernFakeTimers();
fakeTimers.useFakeTimers(config);
try {
return callback(fakeTimers);
} finally {
fakeTimers.useRealTimers();
fakeTimers.dispose();
}
}
/**
* Executes an async callback within a scoped ModernFakeTimers context and ensures clean restoration.
* @param {Function} callback - Async function receiving fake timers instance
* @param {Object} [config] - Fake timers configuration options
* @returns {Promise<*>} Result of the async callback
*/
async function withFakeTimersAsync(callback, config = {}) {
const fakeTimers = createModernFakeTimers();
fakeTimers.useFakeTimers(config);
try {
return await callback(fakeTimers);
} finally {
fakeTimers.useRealTimers();
fakeTimers.dispose();
}
}
module.exports = {
ModernFakeTimers,
LegacyFakeTimers,
createModernFakeTimers,
createLegacyFakeTimers,
withFakeTimers,
withFakeTimersAsync
};
{
"name": "sample-jest-fake-timers",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "sample-jest-fake-timers",
"version": "1.0.0",
"license": "MIT-0",
"dependencies": {
"@jest/fake-timers": "29.7.0"
}
},
"node_modules/@babel/code-frame": {
"version": "7.29.7",
"resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.29.7.tgz",
"integrity": "sha512-Aup7aUOfpbAUg2ROOJN6Iw5f9DMBlzu0mIkm/malLQFN/YQgO48wCj0Kxa3sEHJvPVFg7siR+qRInwXd2qhQKw==",
"license": "MIT",
"dependencies": {
"@babel/helper-validator-identifier": "^7.29.7",
"js-tokens": "^4.0.0",
"picocolors": "^1.1.1"
},
"engines": {
"node": ">=6.9.0"
}
},
"node_modules/@babel/helper-validator-identifier": {
"version": "7.29.7",
"resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.29.7.tgz",
"integrity": "sha512-qehxGkRj55h/ff8EMaJ+cYhyaKlHIxqYDn682wQD7RNp9UujOQsHog2uS0r2vzr4pW+sXf90NeeayjcNaX3fFg==",
"license": "MIT",
"engines": {
"node": ">=6.9.0"
}
},
"node_modules/@jest/fake-timers": {
"version": "29.7.0",
"resolved": "https://registry.npmjs.org/@jest/fake-timers/-/fake-timers-29.7.0.tgz",
"integrity": "sha512-q4DH1Ha4TTFPdxLsqDXK1d3+ioSL7yL5oCMJZgDYm6i+6CygW5E5xVr/D1HdsGxjt1ZWSfUAs9OxSB/BNelWrQ==",
"license": "MIT",
"dependencies": {
"@jest/types": "^29.6.3",
"@sinonjs/fake-timers": "^10.0.2",
"@types/node": "*",
"jest-message-util": "^29.7.0",
"jest-mock": "^29.7.0",
"jest-util": "^29.7.0"
},
"engines": {
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
}
},
"node_modules/@jest/schemas": {
"version": "29.6.3",
"resolved": "https://registry.npmjs.org/@jest/schemas/-/schemas-29.6.3.tgz",
"integrity": "sha512-mo5j5X+jIZmJQveBKeS/clAueipV7KgiX1vMgCxam1RNYiqE1w62n0/tJJnHtjW8ZHcQco5gY85jA3mi0L+nSA==",
"license": "MIT",
"dependencies": {
"@sinclair/typebox": "^0.27.8"
},
"engines": {
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
}
},
"node_modules/@jest/types": {
"version": "29.6.3",
"resolved": "https://registry.npmjs.org/@jest/types/-/types-29.6.3.tgz",
"integrity": "sha512-u3UPsIilWKOM3F9CXtrG8LEJmNxwoCQC/XVj4IKYXvvpx7QIi/Kg1LI5uDmDpKlac62NUtX7eLjRh+jVZcLOzw==",
"license": "MIT",
"dependencies": {
"@jest/schemas": "^29.6.3",
"@types/istanbul-lib-coverage": "^2.0.0",
"@types/istanbul-reports": "^3.0.0",
"@types/node": "*",
"@types/yargs": "^17.0.8",
"chalk": "^4.0.0"
},
"engines": {
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
}
},
"node_modules/@sinclair/typebox": {
"version": "0.27.12",
"resolved": "https://registry.npmjs.org/@sinclair/typebox/-/typebox-0.27.12.tgz",
"integrity": "sha512-hhyNJ+nbR6ZR7pToHvllEFun9TL0sbL+tk/ON75lo+Xas054uez98qRbsuNt7MBCyZKK4+8Yli/OAGZhmfBZ/g==",
"license": "MIT"
},
"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/@types/istanbul-lib-coverage": {
"version": "2.0.6",
"resolved": "https://registry.npmjs.org/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.6.tgz",
"integrity": "sha512-2QF/t/auWm0lsy8XtKVPG19v3sSOQlJe/YHZgfjb/KBBHOGSV+J2q/S671rcq9uTBrLAXmZpqJiaQbMT+zNU1w==",
"license": "MIT"
},
"node_modules/@types/istanbul-lib-report": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/@types/istanbul-lib-report/-/istanbul-lib-report-3.0.3.tgz",
"integrity": "sha512-NQn7AHQnk/RSLOxrBbGyJM/aVQ+pjj5HCgasFxc0K/KhoATfQ/47AyUl15I2yBUpihjmas+a+VJBOqecrFH+uA==",
"license": "MIT",
"dependencies": {
"@types/istanbul-lib-coverage": "*"
}
},
"node_modules/@types/istanbul-reports": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/@types/istanbul-reports/-/istanbul-reports-3.0.4.tgz",
"integrity": "sha512-pk2B1NWalF9toCRu6gjBzR69syFjP4Od8WRAX+0mmf9lAjCRicLOWc+ZrxZHx/0XRjotgkF9t6iaMJ+aXcOdZQ==",
"license": "MIT",
"dependencies": {
"@types/istanbul-lib-report": "*"
}
},
"node_modules/@types/node": {
"version": "26.4.0",
"resolved": "https://registry.npmjs.org/@types/node/-/node-26.4.0.tgz",
"integrity": "sha512-faiGnoIrLH/V8cibOMEAZ8pMw6oXqSukl29ra4mN8GdaB2ZewzeaLj+INpV5N+Z1eKWzY+IzaIZH2EIR6YZRNQ==",
"license": "MIT",
"dependencies": {
"undici-types": "~8.3.0"
}
},
"node_modules/@types/stack-utils": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-2.0.3.tgz",
"integrity": "sha512-9aEbYZ3TbYMznPdcdr3SmIrLXwC/AKZXQeCf9Pgao5CKb8CyHuEX5jzWPTkvregvhRJHcpRO6BFoGW9ycaOkYw==",
"license": "MIT"
},
"node_modules/@types/yargs": {
"version": "17.0.35",
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.35.tgz",
"integrity": "sha512-qUHkeCyQFxMXg79wQfTtfndEC+N9ZZg76HJftDJp+qH2tV7Gj4OJi7l+PiWwJ+pWtW8GwSmqsDj/oymhrTWXjg==",
"license": "MIT",
"dependencies": {
"@types/yargs-parser": "*"
}
},
"node_modules/@types/yargs-parser": {
"version": "21.0.3",
"resolved": "https://registry.npmjs.org/@types/yargs-parser/-/yargs-parser-21.0.3.tgz",
"integrity": "sha512-I4q9QU9MQv4oEOz4tAHJtNz1cwuLxn2F3xcc2iV5WdqLPpUnj30aUuxt1mAxYTG+oe8CZMV/+6rU4S4gRDzqtQ==",
"license": "MIT"
},
"node_modules/ansi-styles": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
"license": "MIT",
"dependencies": {
"color-convert": "^2.0.1"
},
"engines": {
"node": ">=8"
},
"funding": {
"url": "https://github.com/chalk/ansi-styles?sponsor=1"
}
},
"node_modules/braces": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz",
"integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==",
"license": "MIT",
"dependencies": {
"fill-range": "^7.1.1"
},
"engines": {
"node": ">=8"
}
},
"node_modules/chalk": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
"integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
"license": "MIT",
"dependencies": {
"ansi-styles": "^4.1.0",
"supports-color": "^7.1.0"
},
"engines": {
"node": ">=10"
},
"funding": {
"url": "https://github.com/chalk/chalk?sponsor=1"
}
},
"node_modules/ci-info": {
"version": "3.9.0",
"resolved": "https://registry.npmjs.org/ci-info/-/ci-info-3.9.0.tgz",
"integrity": "sha512-NIxF55hv4nSqQswkAeiOi1r83xy8JldOFDTWiug55KBu9Jnblncd2U6ViHmYgHf01TPZS77NJBhBMKdWj9HQMQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/sibiraj-s"
}
],
"license": "MIT",
"engines": {
"node": ">=8"
}
},
"node_modules/color-convert": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
"license": "MIT",
"dependencies": {
"color-name": "~1.1.4"
},
"engines": {
"node": ">=7.0.0"
}
},
"node_modules/color-name": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
"license": "MIT"
},
"node_modules/escape-string-regexp": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-2.0.0.tgz",
"integrity": "sha512-UpzcLCXolUWcNu5HtVMHYdXJjArjsF9C0aNnquZYY4uW/Vu0miy5YoWvbV345HauVvcAUnpRuhMMcqTcGOY2+w==",
"license": "MIT",
"engines": {
"node": ">=8"
}
},
"node_modules/fill-range": {
"version": "7.1.1",
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz",
"integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==",
"license": "MIT",
"dependencies": {
"to-regex-range": "^5.0.1"
},
"engines": {
"node": ">=8"
}
},
"node_modules/graceful-fs": {
"version": "4.2.11",
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz",
"integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==",
"license": "ISC"
},
"node_modules/has-flag": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
"license": "MIT",
"engines": {
"node": ">=8"
}
},
"node_modules/is-number": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz",
"integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==",
"license": "MIT",
"engines": {
"node": ">=0.12.0"
}
},
"node_modules/jest-message-util": {
"version": "29.7.0",
"resolved": "https://registry.npmjs.org/jest-message-util/-/jest-message-util-29.7.0.tgz",
"integrity": "sha512-GBEV4GRADeP+qtB2+6u61stea8mGcOT4mCtrYISZwfu9/ISHFJ/5zOMXYbpBE9RsS5+Gb63DW4FgmnKJ79Kf6w==",
"license": "MIT",
"dependencies": {
"@babel/code-frame": "^7.12.13",
"@jest/types": "^29.6.3",
"@types/stack-utils": "^2.0.0",
"chalk": "^4.0.0",
"graceful-fs": "^4.2.9",
"micromatch": "^4.0.4",
"pretty-format": "^29.7.0",
"slash": "^3.0.0",
"stack-utils": "^2.0.3"
},
"engines": {
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
}
},
"node_modules/jest-mock": {
"version": "29.7.0",
"resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-29.7.0.tgz",
"integrity": "sha512-ITOMZn+UkYS4ZFh83xYAOzWStloNzJFO2s8DWrE4lhtGD+AorgnbkiKERe4wQVBydIGPx059g6riW5Btp6Llnw==",
"license": "MIT",
"dependencies": {
"@jest/types": "^29.6.3",
"@types/node": "*",
"jest-util": "^29.7.0"
},
"engines": {
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
}
},
"node_modules/jest-util": {
"version": "29.7.0",
"resolved": "https://registry.npmjs.org/jest-util/-/jest-util-29.7.0.tgz",
"integrity": "sha512-z6EbKajIpqGKU56y5KBUgy1dt1ihhQJgWzUlZHArA/+X2ad7Cb5iF+AK1EWVL/Bo7Rz9uurpqw6SiBCefUbCGA==",
"license": "MIT",
"dependencies": {
"@jest/types": "^29.6.3",
"@types/node": "*",
"chalk": "^4.0.0",
"ci-info": "^3.2.0",
"graceful-fs": "^4.2.9",
"picomatch": "^2.2.3"
},
"engines": {
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
}
},
"node_modules/js-tokens": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",
"integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==",
"license": "MIT"
},
"node_modules/micromatch": {
"version": "4.0.8",
"resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz",
"integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==",
"license": "MIT",
"dependencies": {
"braces": "^3.0.3",
"picomatch": "^2.3.1"
},
"engines": {
"node": ">=8.6"
}
},
"node_modules/picocolors": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz",
"integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==",
"license": "ISC"
},
"node_modules/picomatch": {
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.2.tgz",
"integrity": "sha512-V7+vQEJ06Z+c5tSye8S+nHUfI51xoXIXjHQ99cQtKUkQqqO1kO/KCJUfZXuB47h/YBlDhah2H3hdUGXn8ie0oA==",
"license": "MIT",
"engines": {
"node": ">=8.6"
},
"funding": {
"url": "https://github.com/sponsors/jonschlinkert"
}
},
"node_modules/pretty-format": {
"version": "29.7.0",
"resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-29.7.0.tgz",
"integrity": "sha512-Pdlw/oPxN+aXdmM9R00JVC9WVFoCLTKJvDVLgmJ+qAffBMxsV85l/Lu7sNx4zSzPyoL2euImuEwHhOXdEgNFZQ==",
"license": "MIT",
"dependencies": {
"@jest/schemas": "^29.6.3",
"ansi-styles": "^5.0.0",
"react-is": "^18.0.0"
},
"engines": {
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
}
},
"node_modules/pretty-format/node_modules/ansi-styles": {
"version": "5.2.0",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-5.2.0.tgz",
"integrity": "sha512-Cxwpt2SfTzTtXcfOlzGEee8O+c+MmUgGrNiBcXnuWxuFJHe6a5Hz7qwhwe5OgaSYI0IJvkLqWX1ASG+cJOkEiA==",
"license": "MIT",
"engines": {
"node": ">=10"
},
"funding": {
"url": "https://github.com/chalk/ansi-styles?sponsor=1"
}
},
"node_modules/react-is": {
"version": "18.3.1",
"resolved": "https://registry.npmjs.org/react-is/-/react-is-18.3.1.tgz",
"integrity": "sha512-/LLMVyas0ljjAtoYiPqYiL8VWXzUUdThrmU5+n20DZv+a+ClRoevUzw5JxU+Ieh5/c87ytoTBV9G1FiKfNJdmg==",
"license": "MIT"
},
"node_modules/slash": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/slash/-/slash-3.0.0.tgz",
"integrity": "sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==",
"license": "MIT",
"engines": {
"node": ">=8"
}
},
"node_modules/stack-utils": {
"version": "2.0.6",
"resolved": "https://registry.npmjs.org/stack-utils/-/stack-utils-2.0.6.tgz",
"integrity": "sha512-XlkWvfIm6RmsWtNJx+uqtKLS8eqFbxUg0ZzLXqY0caEy9l7hruX8IpiDnjsLavoBgqCCR71TqWO8MaXYheJ3RQ==",
"license": "MIT",
"dependencies": {
"escape-string-regexp": "^2.0.0"
},
"engines": {
"node": ">=10"
}
},
"node_modules/supports-color": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
"integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
"license": "MIT",
"dependencies": {
"has-flag": "^4.0.0"
},
"engines": {
"node": ">=8"
}
},
"node_modules/to-regex-range": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz",
"integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==",
"license": "MIT",
"dependencies": {
"is-number": "^7.0.0"
},
"engines": {
"node": ">=8.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"
}
},
"node_modules/undici-types": {
"version": "8.3.0",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-8.3.0.tgz",
"integrity": "sha512-j375ScV60dom+YkPFIfTLcOiPxkN/buHz5GobjLhixFuANaNs3C9l4GmrWqejgXWJ7BbJcFYpTEUkS1Ge8bpZQ==",
"license": "MIT"
}
}
}
{
"name": "sample-jest-fake-timers",
"version": "1.0.0",
"private": true,
"description": "verify pkg:npm/%40jest/fake-timers@29.7.0",
"main": "index.js",
"scripts": {
"test": "node test/contract.js"
},
"dependencies": {
"@jest/fake-timers": "29.7.0"
},
"license": "MIT-0"
}
{
"schemaVersion": 1,
"goal": "verify pkg:npm/%40jest/fake-timers@29.7.0",
"kind": "HOW",
"packages": [
"pkg:npm/%40jest/fake-timers@29.7.0"
],
"constraints": {
"executionContext": "node"
},
"runtimeConditions": {
"ecosystem": "npm",
"language": "javascript",
"moduleSystem": "cjs",
"packageManager": "npm@10.9.8",
"runtime": "node@22.23.2"
}
}
'use strict';
const assert = require('assert');
const {
ModernFakeTimers,
LegacyFakeTimers,
createModernFakeTimers,
createLegacyFakeTimers,
withFakeTimers,
withFakeTimersAsync
} = require('../index.js');
async function runContractTests() {
// 1. ModernFakeTimers installs fake timer mocks with useFakeTimers and restores original implementations on useRealTimers
{
const originalSetTimeout = globalThis.setTimeout;
const originalClearTimeout = globalThis.clearTimeout;
const originalDate = globalThis.Date;
const fake = createModernFakeTimers();
fake.useFakeTimers();
assert.notStrictEqual(globalThis.setTimeout, originalSetTimeout);
assert.notStrictEqual(globalThis.clearTimeout, originalClearTimeout);
assert.notStrictEqual(globalThis.Date, originalDate);
fake.useRealTimers();
assert.strictEqual(globalThis.setTimeout, originalSetTimeout);
assert.strictEqual(globalThis.clearTimeout, originalClearTimeout);
assert.strictEqual(globalThis.Date, originalDate);
fake.dispose();
}
// 2. advanceTimersByTime advances virtual clock and executes scheduled timers synchronously
{
const fake = createModernFakeTimers();
fake.useFakeTimers();
const log = [];
globalThis.setTimeout(() => log.push('first'), 100);
globalThis.setTimeout(() => log.push('second'), 300);
globalThis.setInterval(() => log.push('interval'), 150);
fake.advanceTimersByTime(100);
assert.deepStrictEqual(log, ['first']);
fake.advanceTimersByTime(100);
assert.deepStrictEqual(log, ['first', 'interval']);
fake.advanceTimersByTime(100);
assert.deepStrictEqual(log, ['first', 'interval', 'second', 'interval']);
fake.useRealTimers();
fake.dispose();
}
// 3. advanceTimersByTimeAsync advances virtual clock asynchronously and resolves promises
{
const fake = createModernFakeTimers();
fake.useFakeTimers();
let resolved = false;
const promise = new Promise((resolve) => {
globalThis.setTimeout(() => {
resolved = true;
resolve('done');
}, 500);
});
assert.strictEqual(resolved, false);
await fake.advanceTimersByTimeAsync(500);
const result = await promise;
assert.strictEqual(resolved, true);
assert.strictEqual(result, 'done');
fake.useRealTimers();
fake.dispose();
}
// 4. advanceTimersToNextTimer and advanceTimersToNextTimerAsync progress virtual time to the next pending timer
{
const fake = createModernFakeTimers();
fake.useFakeTimers();
const events = [];
globalThis.setTimeout(() => events.push('a'), 100);
globalThis.setTimeout(() => events.push('b'), 250);
globalThis.setTimeout(() => events.push('c'), 400);
assert.strictEqual(fake.getTimerCount(), 3);
fake.advanceTimersToNextTimer();
assert.deepStrictEqual(events, ['a']);
assert.strictEqual(fake.getTimerCount(), 2);
await fake.advanceTimersToNextTimerAsync(2);
assert.deepStrictEqual(events, ['a', 'b', 'c']);
assert.strictEqual(fake.getTimerCount(), 0);
fake.useRealTimers();
fake.dispose();
}
// 5. runAllTimers and runAllTimersAsync execute all queued and recursively scheduled timers
{
const fake = createModernFakeTimers();
fake.useFakeTimers();
const nestedOrder = [];
globalThis.setTimeout(() => {
nestedOrder.push('level-1');
globalThis.setTimeout(() => {
nestedOrder.push('level-2');
globalThis.setTimeout(() => {
nestedOrder.push('level-3');
}, 50);
}, 50);
}, 50);
fake.runAllTimers();
assert.deepStrictEqual(nestedOrder, ['level-1', 'level-2', 'level-3']);
assert.strictEqual(fake.getTimerCount(), 0);
const asyncOrder = [];
globalThis.setTimeout(() => {
asyncOrder.push('async-1');
globalThis.setTimeout(() => {
asyncOrder.push('async-2');
}, 20);
}, 20);
await fake.runAllTimersAsync();
assert.deepStrictEqual(asyncOrder, ['async-1', 'async-2']);
assert.strictEqual(fake.getTimerCount(), 0);
fake.useRealTimers();
fake.dispose();
}
// 6. runOnlyPendingTimers and runOnlyPendingTimersAsync execute currently queued timers without running newly scheduled nested timers
{
const fake = createModernFakeTimers();
fake.useFakeTimers();
const pendingLog = [];
globalThis.setTimeout(() => {
pendingLog.push('parent-1');
globalThis.setTimeout(() => pendingLog.push('child-1'), 100);
}, 50);
globalThis.setTimeout(() => {
pendingLog.push('parent-2');
}, 80);
fake.runOnlyPendingTimers();
assert.deepStrictEqual(pendingLog, ['parent-1', 'parent-2']);
assert.strictEqual(fake.getTimerCount(), 1);
await fake.runOnlyPendingTimersAsync();
assert.deepStrictEqual(pendingLog, ['parent-1', 'parent-2', 'child-1']);
assert.strictEqual(fake.getTimerCount(), 0);
fake.useRealTimers();
fake.dispose();
}
// 7. clearAllTimers and getTimerCount track and cancel active scheduled timers
{
const fake = createModernFakeTimers();
fake.useFakeTimers();
globalThis.setTimeout(() => {}, 100);
globalThis.setTimeout(() => {}, 200);
globalThis.setInterval(() => {}, 300);
assert.strictEqual(fake.getTimerCount(), 3);
fake.clearAllTimers();
assert.strictEqual(fake.getTimerCount(), 0);
fake.useRealTimers();
fake.dispose();
}
// 8. setSystemTime, getRealSystemTime, now, and reset manage and simulate system timestamps
{
const fake = createModernFakeTimers();
fake.useFakeTimers();
const targetDate = new Date('2026-08-28T00:00:00.000Z');
fake.setSystemTime(targetDate);
assert.strictEqual(fake.now(), targetDate.getTime());
assert.strictEqual(globalThis.Date.now(), targetDate.getTime());
const realTime = fake.getRealSystemTime();
assert.strictEqual(typeof realTime, 'number');
assert.ok(realTime > 0);
fake.reset();
fake.useRealTimers();
fake.dispose();
}
// 9. LegacyFakeTimers installs timer mocks, manages clock advancement, and executes runWithRealTimers callback
{
const { timers, global } = createLegacyFakeTimers();
timers.useFakeTimers();
let legacyCalled = false;
global.setTimeout(() => {
legacyCalled = true;
}, 100);
assert.strictEqual(timers.getTimerCount(), 1);
timers.advanceTimersByTime(100);
assert.strictEqual(legacyCalled, true);
assert.strictEqual(timers.getTimerCount(), 0);
let realTimerExecuted = false;
timers.runWithRealTimers(() => {
realTimerExecuted = true;
});
assert.strictEqual(realTimerExecuted, true);
timers.useRealTimers();
timers.dispose();
}
// 10. withFakeTimers and withFakeTimersAsync helpers safely execute scoped synchronous and asynchronous callbacks
{
const originalSetTimeout = globalThis.setTimeout;
const syncResult = withFakeTimers((fake) => {
assert.notStrictEqual(globalThis.setTimeout, originalSetTimeout);
let executed = false;
globalThis.setTimeout(() => {
executed = true;
}, 50);
fake.runAllTimers();
assert.strictEqual(executed, true);
return 42;
});
assert.strictEqual(syncResult, 42);
assert.strictEqual(globalThis.setTimeout, originalSetTimeout);
const asyncResult = await withFakeTimersAsync(async (fake) => {
assert.notStrictEqual(globalThis.setTimeout, originalSetTimeout);
let executed = false;
globalThis.setTimeout(() => {
executed = true;
}, 100);
await fake.runAllTimersAsync();
assert.strictEqual(executed, true);
return 84;
});
assert.strictEqual(asyncResult, 84);
assert.strictEqual(globalThis.setTimeout, originalSetTimeout);
}
console.log('All @jest/fake-timers contract assertions passed successfully.');
}
runContractTests().catch((err) => {
console.error('Contract test failure:', err);
process.exit(1);
});
原始种子者
匿名