CodeSampleX

샘플

@jest/fake-timers 29.7.0: LegacyFakeTimers

검증된 샘플 — npm @jest/fake-timers 29.7.0: LegacyFakeTimers. node 22 · linux alpine/x64 · docker에서 contract를 실행해 통과했습니다: LegacyFakeTimers installs fake timer…

sha256:c6a1af5594858c37f532883ff6636bbc0a1d78dad55a7b2a768c7772880b60f7

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

케이스

HOW
목표
verify LegacyFakeTimers in pkg:npm/%40jest/fake-timers@29.7.0
패키지
심벌
  • LegacyFakeTimers
환경
node 22.23.2
생성일
2026-08-26T04:22:58Z

컨트랙트

  1. LegacyFakeTimers installs fake timer mocks onto the target global object with useFakeTimers
  2. LegacyFakeTimers tracks and reports pending timer counts via getTimerCount
  3. LegacyFakeTimers advances virtual time and triggers timer callbacks via advanceTimersByTime
  4. LegacyFakeTimers advances virtual clock to subsequent timer expiries with advanceTimersToNextTimer
  5. LegacyFakeTimers executes all queued timers, ticks, and immediates with runAllTimers
  6. LegacyFakeTimers executes only currently scheduled timers with runOnlyPendingTimers
  7. LegacyFakeTimers processes queued process.nextTick and setImmediate callbacks with runAllTicks and runAllImmediates
  8. LegacyFakeTimers cancels pending timers and resets internal clock with clearAllTimers and reset
  9. LegacyFakeTimers temporarily executes synchronous callback with real timers using runWithRealTimers
  10. LegacyFakeTimers cleans up timers and marks instance disposed with dispose and restores original functions with useRealTimers

파일

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

소스 아티팩트 내려받기 (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 LegacyFakeTimers in 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
Demonstrate these symbols/APIs:
  - LegacyFakeTimers
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:eee57062291eb9590558a737e1f0763bb97bd150ade748758bc48ac095fec582","constraints":{"executionContext":"node"},"contract":["LegacyFakeTimers installs fake timer mocks onto the target global object with useFakeTimers","LegacyFakeTimers tracks and reports pending timer counts via getTimerCount","LegacyFakeTimers advances virtual time and triggers timer callbacks via advanceTimersByTime","LegacyFakeTimers advances virtual clock to subsequent timer expiries with advanceTimersToNextTimer","LegacyFakeTimers executes all queued timers, ticks, and immediates with runAllTimers","LegacyFakeTimers executes only currently scheduled timers with runOnlyPendingTimers","LegacyFakeTimers processes queued process.nextTick and setImmediate callbacks with runAllTicks and runAllImmediates","LegacyFakeTimers cancels pending timers and resets internal clock with clearAllTimers and reset","LegacyFakeTimers temporarily executes synchronous callback with real timers using runWithRealTimers","LegacyFakeTimers cleans up timers and marks instance disposed with dispose and restores original functions with useRealTimers"],"goal":"verify LegacyFakeTimers in pkg:npm/%40jest/fake-timers@29.7.0","kind":"HOW","packages":["pkg:npm/%40jest/fake-timers@29.7.0"],"schemaVersion":1,"symbols":["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":["LegacyFakeTimers"],"verifierAdapter":"node-typescript@1"}
index.js
'use strict';

const { LegacyFakeTimers, ModernFakeTimers } = require('@jest/fake-timers');

/**
 * Helper to create and initialize a LegacyFakeTimers instance with a mock environment.
 */
function createLegacyFakeTimers(customGlobal = {}, customOptions = {}) {
  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,
    ...customGlobal
  };

  const moduleMocker = {
    fn: (impl) => {
      const mockFn = (...args) => impl(...args);
      return mockFn;
    }
  };

  const timerConfig = {
    idToRef: (id) => id,
    refToId: (ref) => ref
  };

  const config = { rootDir: '/' };

  const timers = new LegacyFakeTimers({
    global: targetGlobal,
    moduleMocker,
    timerConfig,
    config,
    ...customOptions
  });

  return { timers, global: targetGlobal };
}

module.exports = {
  LegacyFakeTimers,
  ModernFakeTimers,
  createLegacyFakeTimers
};
package-lock.json
{
  "name": "sample-jest-fake-timers-legacy",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-jest-fake-timers-legacy",
      "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.3.0",
      "resolved": "https://registry.npmjs.org/@types/node/-/node-26.3.0.tgz",
      "integrity": "sha512-L3fgrnchriRC2ExBflb8j4uZZURHZfQsmQeyVzhjcHW4kkwVyo8/0h1B2MVzMTrYUJYu6G7EWs14hW/L9putqw==",
      "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"
    }
  }
}
package.json
{
  "name": "sample-jest-fake-timers-legacy",
  "version": "1.0.0",
  "private": true,
  "description": "verify LegacyFakeTimers in 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"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify LegacyFakeTimers in pkg:npm/%40jest/fake-timers@29.7.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40jest/fake-timers@29.7.0"
  ],
  "symbols": [
    "LegacyFakeTimers"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract.js
'use strict';

const assert = require('assert');
const { LegacyFakeTimers, createLegacyFakeTimers } = require('../index.js');

function testUseFakeTimersAndMockReplacement() {
  const { timers, global } = createLegacyFakeTimers();
  const originalSetTimeout = global.setTimeout;
  const originalClearTimeout = global.clearTimeout;

  timers.useFakeTimers();

  assert.notStrictEqual(global.setTimeout, originalSetTimeout);
  assert.notStrictEqual(global.clearTimeout, originalClearTimeout);
  assert.strictEqual(typeof global.setTimeout, 'function');
  assert.strictEqual(typeof global.setInterval, 'function');
  assert.strictEqual(typeof global.setImmediate, 'function');
  assert.strictEqual(typeof global.clearImmediate, 'function');
  assert.strictEqual(typeof global.clearInterval, 'function');
  assert.strictEqual(typeof global.clearTimeout, 'function');
  assert.strictEqual(typeof global.process.nextTick, 'function');

  timers.useRealTimers();
  assert.strictEqual(global.setTimeout, originalSetTimeout);
  assert.strictEqual(global.clearTimeout, originalClearTimeout);
}

function testTimerCountAndVirtualClock() {
  const { timers, global } = createLegacyFakeTimers();
  timers.useFakeTimers();

  assert.strictEqual(timers.now(), 0);
  assert.strictEqual(timers.getTimerCount(), 0);

  global.setTimeout(() => {}, 100);
  global.setInterval(() => {}, 200);
  global.setImmediate(() => {});
  global.process.nextTick(() => {});

  assert.strictEqual(timers.getTimerCount(), 4);

  timers.useRealTimers();
}

function testAdvanceTimersByTime() {
  const { timers, global } = createLegacyFakeTimers();
  timers.useFakeTimers();

  const executionLog = [];

  global.setTimeout((arg) => {
    executionLog.push(`timeout-100:${arg}`);
  }, 100, 'first');

  global.setTimeout((arg) => {
    executionLog.push(`timeout-250:${arg}`);
  }, 250, 'second');

  global.setInterval(() => {
    executionLog.push('interval-100');
  }, 100);

  timers.advanceTimersByTime(150);
  assert.strictEqual(timers.now(), 150);
  assert.deepStrictEqual(executionLog, ['timeout-100:first', 'interval-100']);

  timers.advanceTimersByTime(150);
  assert.strictEqual(timers.now(), 300);
  assert.deepStrictEqual(executionLog, [
    'timeout-100:first',
    'interval-100',
    'interval-100',
    'timeout-250:second',
    'interval-100'
  ]);

  timers.useRealTimers();
}

function testAdvanceTimersToNextTimer() {
  const { timers, global } = createLegacyFakeTimers();
  timers.useFakeTimers();

  const executed = [];
  global.setTimeout(() => executed.push(50), 50);
  global.setTimeout(() => executed.push(120), 120);
  global.setTimeout(() => executed.push(300), 300);

  timers.advanceTimersToNextTimer(1);
  assert.strictEqual(timers.now(), 50);
  assert.deepStrictEqual(executed, [50]);

  timers.advanceTimersToNextTimer(2);
  assert.strictEqual(timers.now(), 300);
  assert.deepStrictEqual(executed, [50, 120, 300]);

  timers.useRealTimers();
}

function testRunAllTimers() {
  const { timers, global } = createLegacyFakeTimers();
  timers.useFakeTimers();

  const results = [];
  global.setTimeout(() => {
    results.push('t1');
    global.setTimeout(() => {
      results.push('t2');
    }, 50);
  }, 50);

  global.setImmediate(() => {
    results.push('imm1');
  });

  global.process.nextTick(() => {
    results.push('tick1');
  });

  timers.runAllTimers();
  assert.deepStrictEqual(results, ['tick1', 'imm1', 't1', 't2']);
  assert.strictEqual(timers.getTimerCount(), 0);

  timers.useRealTimers();
}

function testRunOnlyPendingTimers() {
  const { timers, global } = createLegacyFakeTimers();
  timers.useFakeTimers();

  const executed = [];
  global.setTimeout(() => {
    executed.push('pending1');
    global.setTimeout(() => {
      executed.push('nested1');
    }, 10);
  }, 100);

  global.setTimeout(() => {
    executed.push('pending2');
  }, 200);

  timers.runOnlyPendingTimers();
  assert.deepStrictEqual(executed, ['pending1', 'pending2']);
  assert.strictEqual(timers.getTimerCount(), 1);

  timers.advanceTimersByTime(10);
  assert.deepStrictEqual(executed, ['pending1', 'pending2', 'nested1']);
  assert.strictEqual(timers.getTimerCount(), 0);

  timers.useRealTimers();
}

function testRunAllTicksAndImmediates() {
  const { timers, global } = createLegacyFakeTimers();
  timers.useFakeTimers();

  const tickResults = [];
  const immediateResults = [];

  global.process.nextTick(() => tickResults.push('tick-a'));
  global.process.nextTick(() => tickResults.push('tick-b'));
  global.setImmediate(() => immediateResults.push('imm-a'));
  global.setImmediate(() => immediateResults.push('imm-b'));

  assert.strictEqual(timers.getTimerCount(), 4);

  timers.runAllTicks();
  assert.deepStrictEqual(tickResults, ['tick-a', 'tick-b']);
  assert.strictEqual(immediateResults.length, 0);

  timers.runAllImmediates();
  assert.deepStrictEqual(immediateResults, ['imm-a', 'imm-b']);
  assert.strictEqual(timers.getTimerCount(), 0);

  timers.useRealTimers();
}

function testClearAllTimersAndReset() {
  const { timers, global } = createLegacyFakeTimers();
  timers.useFakeTimers();

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

  global.setImmediate(() => {
    executed = true;
  });

  assert.strictEqual(timers.getTimerCount(), 2);
  timers.clearAllTimers();
  assert.strictEqual(timers.getTimerCount(), 0);

  timers.advanceTimersByTime(500);
  assert.strictEqual(executed, false);

  global.setTimeout(() => {}, 200);
  timers.advanceTimersByTime(100);
  assert.strictEqual(timers.now(), 600);

  timers.reset();
  assert.strictEqual(timers.now(), 0);
  assert.strictEqual(timers.getTimerCount(), 0);

  timers.useRealTimers();
}

function testRunWithRealTimers() {
  const { timers, global } = createLegacyFakeTimers();
  timers.useFakeTimers();

  const fakeSetTimeout = global.setTimeout;
  let executedSynchronously = false;

  timers.runWithRealTimers(() => {
    assert.notStrictEqual(global.setTimeout, fakeSetTimeout);
    executedSynchronously = true;
  });

  assert.strictEqual(executedSynchronously, true);
  assert.strictEqual(global.setTimeout, fakeSetTimeout);

  timers.useRealTimers();
}

function testDispose() {
  const { timers, global } = createLegacyFakeTimers();
  timers.useFakeTimers();

  let ran = false;
  global.setTimeout(() => {
    ran = true;
  }, 100);

  timers.dispose();
  assert.strictEqual(timers.getTimerCount(), 0);

  // Calling timer APIs after dispose should return null / noop
  const timerHandle = global.setTimeout(() => {}, 100);
  assert.strictEqual(timerHandle, null);

  timers.useRealTimers();
}

function runAll() {
  testUseFakeTimersAndMockReplacement();
  testTimerCountAndVirtualClock();
  testAdvanceTimersByTime();
  testAdvanceTimersToNextTimer();
  testRunAllTimers();
  testRunOnlyPendingTimers();
  testRunAllTicksAndImmediates();
  testClearAllTimersAndReset();
  testRunWithRealTimers();
  testDispose();

  console.log('All LegacyFakeTimers contract assertions passed successfully.');
}

runAll();

오리진 시더

익명