Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
jest-plugin-clock
Advanced tools
Jest plugin to mock dates, times, and datetimes.
Install jest-plugin-clock
using yarn
:
yarn add --dev jest-plugin-clock
When testing dates, you often want to have deterministic outcomes even when using new Date()
and Date.now()
. This allows you to set the clock time such that your test can compute the deterministic outcome each time.
If you want, you can import clock
from jest-plugin-clock
at the top of every test:
import clock from 'jest-plugin-clock';
If you want to install clock
as a global, you can modify the jest
section of your package.json
to include:
"jest": {
"setupFiles": [
"jest-plugin-clock/setup"
]
}
Here's an example test that tests clock
itself:
describe('clock', () => {
context('with string date', () => {
clock.set('2017-08-16T15:20:40.450');
it('returns the correct year', () => {
expect(new Date().getFullYear()).toEqual(2017);
});
it('returns the correct month', () => {
// getMonth is 0-indexed.
expect(new Date().getMonth()).toEqual(7);
});
it('returns the correct day (date)', () => {
expect(new Date().getDate()).toEqual(16);
});
it('returns the correct hour', () => {
expect(new Date().getHours()).toEqual(15);
});
it('returns the correct minute', () => {
expect(new Date().getMinutes()).toEqual(20);
});
it('returns the correct seconds', () => {
expect(new Date().getSeconds()).toEqual(40);
});
it('returns the correct milliseconds', () => {
expect(new Date().getMilliseconds()).toEqual(450);
});
});
context('with date object', () => {
clock.set(new Date('2017-08-16T15:20:40.450'));
it('returns the correct year', () => {
expect(new Date().getFullYear()).toEqual(2017);
});
it('returns the correct month', () => {
// getMonth is 0-indexed.
expect(new Date().getMonth()).toEqual(7);
});
it('returns the correct day (date)', () => {
expect(new Date().getDate()).toEqual(16);
});
it('returns the correct hour', () => {
expect(new Date().getHours()).toEqual(15);
});
it('returns the correct minute', () => {
expect(new Date().getMinutes()).toEqual(20);
});
it('returns the correct seconds', () => {
expect(new Date().getSeconds()).toEqual(40);
});
it('returns the correct milliseconds', () => {
expect(new Date().getMilliseconds()).toEqual(450);
});
});
});
FAQs
Jest plugin to mock dates, times, and datetimes.
The npm package jest-plugin-clock receives a total of 11,826 weekly downloads. As such, jest-plugin-clock popularity was classified as popular.
We found that jest-plugin-clock demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.