{"id":13682592,"url":"https://github.com/hustcc/jest-date-mock","last_synced_at":"2025-10-24T16:03:49.673Z","repository":{"id":56279715,"uuid":"135012697","full_name":"hustcc/jest-date-mock","owner":"hustcc","description":"🌗 Mock `Date` when run unit test cases with jest. Make tests of Date easier.","archived":false,"fork":false,"pushed_at":"2024-04-21T01:52:01.000Z","size":35,"stargazers_count":265,"open_issues_count":1,"forks_count":15,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-04T14:05:05.046Z","etag":null,"topics":["date","datetime","jest","jest-date-mock","jest-mock","timestamp"],"latest_commit_sha":null,"homepage":"https://github.com/hustcc/jest-date-mock","language":"JavaScript","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/hustcc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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":null,"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":["https://paypal.me/hustcc","https://atool.vip"]}},"created_at":"2018-05-27T03:30:29.000Z","updated_at":"2025-01-17T22:40:31.000Z","dependencies_parsed_at":"2024-06-18T12:39:24.861Z","dependency_job_id":"260d3302-a533-472f-8a84-46813c586aae","html_url":"https://github.com/hustcc/jest-date-mock","commit_stats":{"total_commits":36,"total_committers":10,"mean_commits":3.6,"dds":0.2777777777777778,"last_synced_commit":"ae6a562a3c0ee3aefd8cd937aa6ab8d6141b9277"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hustcc%2Fjest-date-mock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hustcc%2Fjest-date-mock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hustcc%2Fjest-date-mock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hustcc%2Fjest-date-mock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hustcc","download_url":"https://codeload.github.com/hustcc/jest-date-mock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253033623,"owners_count":21843732,"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":["date","datetime","jest","jest-date-mock","jest-mock","timestamp"],"created_at":"2024-08-02T13:01:49.253Z","updated_at":"2025-10-24T16:03:49.605Z","avatar_url":"https://github.com/hustcc.png","language":"JavaScript","funding_links":["https://paypal.me/hustcc","https://atool.vip"],"categories":["JavaScript","Packages","Commit hooks"],"sub_categories":["Mocks","Server-rendered React"],"readme":"# jest-date-mock\n\n\u003e Mock `Date` when run unit test cases with jest. Make tests of `Date` easier.\n\u003e - [jest-random-mock](https://github.com/hustcc/jest-random-mock) Mock `Math.random` in jest, with deterministic random generator.\n\u003e - [jest-canvas-mock](https://github.com/hustcc/jest-canvas-mock) Mock `canvas` when run unit test cases with jest.\n\n[![Build Status](https://github.com/hustcc/jest-date-mock/workflows/build/badge.svg)](https://github.com/hustcc/jest-date-mock/actions)\n[![Coverage Status](https://coveralls.io/repos/github/hustcc/jest-date-mock/badge.svg?branch=master)](https://coveralls.io/github/hustcc/jest-date-mock)\n[![npm](https://img.shields.io/npm/v/jest-date-mock.svg)](https://www.npmjs.com/package/jest-date-mock)\n[![npm](https://img.shields.io/npm/dm/jest-date-mock.svg)](https://www.npmjs.com/package/jest-date-mock)\n\n\n## Install\n\nThis should only be installed as a development dependency (`devDependencies`) as it is only designed for testing.\n\n```bash\nnpm i --save-dev jest-date-mock\n```\n\n\n## Setup\n\nIn your `package.json` under the `jest`, create a `setupFiles` array and add `jest-date-mock` to the array.\n\n```js\n{\n  \"jest\": {\n    \"setupFiles\": [\"jest-date-mock\"]\n  }\n}\n```\n\nIf you already have a `setupFiles` attribute you can also append `jest-date-mock` to the array.\n\n```js\n{\n  \"jest\": {\n    \"setupFiles\": [\"./__setups__/other.js\", \"jest-date-mock\"]\n  }\n}\n```\n\nMore about in [configuration section](https://facebook.github.io/jest/docs/en/configuration.html#content).\n\n\n## Setup file\n\nAlternatively you can create a new setup file which then requires this module or\nadd the `require` statement to an existing setup file.\n\n`__setups__/date.js`\n\n```js\nimport 'jest-date-mock';\n// or\nrequire('jest-date-mock');\n```\n\nAdd that file to your `setupFiles` array:\n\n```js\n\"jest\": {\n  \"setupFiles\": [\n    \"./__setups__/date.js\"\n  ]\n}\n```\n\n\n## Usage\n\n\u003e Use the only `3 api` for test cases.\n\n - `advanceBy(ms)`: advance date timestamp by `ms`.\n - `advanceTo([timestamp])`: reset date to `timestamp`, default to `0`.\n - `clear()`: shut down the mock system.\n\n```js\nimport { advanceBy, advanceTo, clear } from 'jest-date-mock';\n\ntest('usage', () =\u003e {\n  advanceTo(new Date(2018, 5, 27, 0, 0, 0)); // reset to date time.\n\n  const now = Date.now();\n\n  advanceBy(3000); // advance time 3 seconds\n  expect(+new Date() - now).toBe(3000);\n\n  advanceBy(-1000); // advance time -1 second\n  expect(+new Date() - now).toBe(2000);\n\n  clear();\n  Date.now(); // will got current timestamp\n});\n```\n\nMore sample code [here](__tests__).\n\n\nAlso, add an API `Date.current()` to get the actual current timestamp.\n\n```js\nimport { advanceBy, advanceTo, clear } from 'jest-date-mock';\n\nadvanceTo(0); // reset to timestamp = 0\n\nDate.now(); // will got 0\n\nDate.current(); // will got the actual timestamp.\n```\n\n\n## License\n\nMIT@[hustcc](https://github.com/hustcc).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhustcc%2Fjest-date-mock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhustcc%2Fjest-date-mock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhustcc%2Fjest-date-mock/lists"}