{"id":34617986,"url":"https://github.com/lbfalvy/mockable-timer","last_synced_at":"2026-05-26T12:07:40.461Z","repository":{"id":57300147,"uuid":"466876328","full_name":"lbfalvy/mockable-timer","owner":"lbfalvy","description":"Dependency injection for Date.now, setTImeout and setInterval","archived":false,"fork":false,"pushed_at":"2022-03-07T00:35:58.000Z","size":111,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-02T02:48:59.806Z","etag":null,"topics":["dependency-injection","mock","now","setinterval","settimeout","testing"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/mockable-timer","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lbfalvy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-03-06T22:51:49.000Z","updated_at":"2022-08-28T23:44:48.000Z","dependencies_parsed_at":"2022-08-26T18:12:36.967Z","dependency_job_id":null,"html_url":"https://github.com/lbfalvy/mockable-timer","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/lbfalvy/mockable-timer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lbfalvy%2Fmockable-timer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lbfalvy%2Fmockable-timer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lbfalvy%2Fmockable-timer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lbfalvy%2Fmockable-timer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lbfalvy","download_url":"https://codeload.github.com/lbfalvy/mockable-timer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lbfalvy%2Fmockable-timer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33519280,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T03:12:49.672Z","status":"ssl_error","status_checked_at":"2026-05-26T03:12:47.976Z","response_time":63,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dependency-injection","mock","now","setinterval","settimeout","testing"],"created_at":"2025-12-24T14:53:58.979Z","updated_at":"2026-05-26T12:07:40.450Z","avatar_url":"https://github.com/lbfalvy.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mockable Timer\n\nA thin wrapper over Date.now(), setTimeout and setInterval, along with an\nelaborate mock\n\nDesigned for dependency injection, and for wrapping further to support\nany other interface\n\nNOT designed for replacing the original Date.now(), setTimeout and\nsetInterval for testing. It will probably work but it isn't an intended\nuse case. Unit testing implies dependency injection\n\n## Usage\n\n### Client\n\nImport the default implementation which depends on `Date.now()`,\n`setTimeout`, `setInterval`, `clearTimeout` and `clearInterval`\n\n```ts\nimport { time } from 'mockable-timer'\n```\n\nGet the exact UNIX timestamp in seconds. It's not in milliseconds because\nmost platforms zero out the two last digits anyway to prevent cache\ntiming attacks so the real part is inaccurate anyway, and this way\ndurations within human perception range are easier to read and compare.\n\nThe part after the decimal point still contains the 100s part of Date.now(),\nif you want to use it for benchmarking.\n\n```ts\ntime.now()\n```\n\nSet a timeout. The time is in seconds for consistency. `wait` returns a\nfunction, calling this function cancels the timeout.\n\n```ts\nconst cancel = time.wait(60, () =\u003e alert('Are you still there?'))\ncancel()\n```\n\nSet an interval. The third parameter is a boolean that defaults to\n`false`.\n\n```ts\ntime.wait(60, () =\u003e alert('Another minute passed'), true)\n```\n\n### Mock\n\nThe time manager defines the following methods:\n\n```ts\nimport { mockTime } from 'mockable-timer'\n\nconst [time, timeManager] = mockTime()\n\nexport type ScheduleEntry = [number, () =\u003e void]\nexport interface TimeManager {\n    progress(s: number): Promise\u003cvoid\u003e\n    progressTo(s: number): Promise\u003cvoid\u003e\n    flushMtq(): Promise\u003cvoid\u003e\n    runAll(limit?: number): Promise\u003cvoid\u003e\n    getNext(): ScheduleEntry | undefined\n    getQueue(): ScheduleEntry[]\n}\n```\n\n\n- **`progress`**: Moves time forward by the specified number of seconds\n- **`progressTo`**: Moves time to the specified point, throws if the\n\tpoint is in the psat\n- **`flushMtq`**: Flush all microtasks using `setTimeout` with a duration\n\tof 0. This is done after every enqueued timer by the mock\n- **`runAll`**: Run the timer until the queue is empty. Throws if there\n\tare still more after 10K executed timers. Note that this can get\n\tstuck on intervals\n- **`getNext`**: Return a tuple of the next task and its due timestamp.\n\tReturns undefined if the queue is empty\n- **`getQueue`**: Return an array of all enqueued tasks with their due\n\ttimestamps\n\n## Todo\n\n- Make runAll halt if there are only repeating timers left","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flbfalvy%2Fmockable-timer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flbfalvy%2Fmockable-timer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flbfalvy%2Fmockable-timer/lists"}