{"id":20253554,"url":"https://github.com/graphile/jest-time-helpers","last_synced_at":"2025-04-10T23:43:41.224Z","repository":{"id":55009582,"uuid":"329906683","full_name":"graphile/jest-time-helpers","owner":"graphile","description":"Helpers you can use in tests that relate to the passage of time (i.e. code that involves setTimeout, setInterval, new Date(), Date.now(), etc)","archived":false,"fork":false,"pushed_at":"2024-11-18T16:25:07.000Z","size":182,"stargazers_count":11,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-10T23:43:36.524Z","etag":null,"topics":["fake","jest","test","testing","time"],"latest_commit_sha":null,"homepage":"https://www.graphile.org/jest-time-helpers/","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/graphile.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"Benjie"}},"created_at":"2021-01-15T12:31:53.000Z","updated_at":"2024-11-18T16:25:11.000Z","dependencies_parsed_at":"2024-12-04T22:43:03.462Z","dependency_job_id":null,"html_url":"https://github.com/graphile/jest-time-helpers","commit_stats":{"total_commits":14,"total_committers":1,"mean_commits":14.0,"dds":0.0,"last_synced_commit":"a76f55af48a1cdf88aa0f886af540c851b287218"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphile%2Fjest-time-helpers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphile%2Fjest-time-helpers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphile%2Fjest-time-helpers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphile%2Fjest-time-helpers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/graphile","download_url":"https://codeload.github.com/graphile/jest-time-helpers/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248317726,"owners_count":21083527,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["fake","jest","test","testing","time"],"created_at":"2024-11-14T10:25:39.875Z","updated_at":"2025-04-10T23:43:41.207Z","avatar_url":"https://github.com/graphile.png","language":"TypeScript","funding_links":["https://github.com/sponsors/Benjie"],"categories":[],"sub_categories":[],"readme":"# jest-time-helpers\n\nHelpers you can use in tests that relate to the passage of time (i.e. code that\ninvolves `setTimeout`, `setInterval`, `new Date()`, `Date.now()`, etc.). Allows\nyou to \"set the clock\" to a particular point in time, advancing any `setTimeout`\nor `setInterval` calls that need advancing at the same time. Makes it possible\n(even pleasant!) to test code that would normally be dependent on the passage of\ntime, such as scheduled events.\n\n[Documentation](https://www.graphile.org/jest-time-helpers/).\n\nThis helper library was born out of\n[adding cron functionality](https://github.com/graphile/worker/pull/163) to\n[Graphile Worker](https://github.com/graphile/worker) and needing a reliable way\nto test it.\n\nIf you find this useful, please give it a star ⭐\n\n## Methods\n\nFor [full documentation](https://www.graphile.org/jest-time-helpers/), please\nclick the headers. The following acts as a quick-reference.\n\n### [`setupFakeTimers()`](https://www.graphile.org/jest-time-helpers/modules.html#setupfaketimers)\n\nImport and call the `setupFakeTimers` helper at the top of your test file:\n\n```js\nimport { setupFakeTimers } from \"jest-time-helpers\";\nconst { setTime } = setupFakeTimers();\n```\n\nThen inside your tests you can call `setTime(timestamp)` to pretend that the\nsystem time is the timestamp you gave. When you set the timestamp to a value\nafter the previous timestamp, all timers (`setTimeout`, `setInterval`, etc) will\nbe advanced by that amount, as well as the \"clock\". When you set the timestamp\nto a value before the previous timestamp, only the clock will be updated and no\ntimeouts/intervals will be effected.\n\nExample:\n\n```js\nconst REFERENCE_TIMESTAMP = 950536800000; /* 14th February 2000, 2pm UTC */\n\ntest(\"new Date().toISOString() returns the expected timestamp\", () =\u003e {\n  setTime(REFERENCE_TIMESTAMP);\n  // Note, time may have advanced a millisecond or two, so we can't be too precise\n  expect(new Date().toISOString()).toMatch(/^2000-02-14T14:00:0.*Z$/);\n});\n```\n\n### [`sleep(ms: number)`](https://www.graphile.org/jest-time-helpers/modules.html#sleep)\n\nA trivial method that returns a promise that resolves after the given number of\nreal-time milliseconds. The important part about this method (as opposed to one\nthat you might write yourself) is that it is not inhibited or influenced by fake\ntimers.\n\n### [`sleepUntil(condition: () =\u003e void, maxDuration = 2000, pollInterval = 2)`](https://www.graphile.org/jest-time-helpers/modules.html#sleepuntil)\n\nPolls the `condition` callback every `pollInterval` milliseconds, and resolves\nsuccess when `condition()` returns a truthy value. If `maxDuration` elapses\nbefore `condition()` returns true, returns a rejected promise.\n\nUseful for waiting on external actions without putting arbitrary sleeps in your\ncode (e.g. waiting for a database record to be created).\n\nLike `sleep()`, it is not inhibited or influenced by fake timers.\n\n## Constants\n\n- `SECOND` - number of milliseconds in a second\n- `MINUTE` - number of milliseconds in a minute\n- `HOUR` - number of milliseconds in a hour\n- `DAY` - number of milliseconds in a day\n- `WEEK` - number of milliseconds in a week\n\n## Crowd-funded open-source software\n\nTo help us develop software sustainably under the MIT license, we ask all\nindividuals and businesses that use our software to help support its ongoing\nmaintenance and development via sponsorship.\n\n### [Click here to find out more about sponsors and sponsorship.](https://www.graphile.org/sponsor/)\n\n## Development\n\nCheckout the repository, then run the following commands:\n\n```\nyarn\nyarn test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphile%2Fjest-time-helpers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgraphile%2Fjest-time-helpers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphile%2Fjest-time-helpers/lists"}