From 0c0ecd894827978eada5f0d50f938192406c5194 Mon Sep 17 00:00:00 2001 From: joaopedrodcf Date: Mon, 6 Apr 2026 19:19:00 +0100 Subject: [PATCH 1/6] Replace mockdate --- docs/modules/README.md | 1 + docs/modules/mockdate.md | 65 ++++++++++++++++++++++++++++++++++++++++ manifests/preferred.json | 29 +++++++++++++++--- 3 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 docs/modules/mockdate.md diff --git a/docs/modules/README.md b/docs/modules/README.md index 5c8dda3..3365a92 100644 --- a/docs/modules/README.md +++ b/docs/modules/README.md @@ -75,6 +75,7 @@ ESLint plugin. - [`lodash`, `underscore` and related](./lodash-underscore.md) - [`materialize-css`](./materialize-css.md) - [`md5`](./md5.md) +- [`mockdate`](./mockdate.md) - [`mkdirp`](./mkdirp.md) - [`moment.js`](./moment.md) - [`npm-run-all`](./npm-run-all.md) diff --git a/docs/modules/mockdate.md b/docs/modules/mockdate.md new file mode 100644 index 0000000..ece0bfe --- /dev/null +++ b/docs/modules/mockdate.md @@ -0,0 +1,65 @@ +--- +description: Modern alternatives to the mockdate package for freezing time in tests +--- + +# Replacements for `mockdate` + +`mockdate` is mainly used in tests, and modern test runners already include built-in APIs for freezing time without pulling in an extra dependency. + +## `vitest` + +[`vitest`](https://vitest.dev/guide/mocking.html#mock-the-current-date) provides `vi.useFakeTimers()` and `vi.setSystemTime()` for freezing the current date during tests. + +```ts +import MockDate from 'mockdate' // [!code --] +import { vi, test, expect } from 'vitest' // [!code ++] + +test('freeze date', () => { + MockDate.set('2026-01-01') // [!code --] + vi.useFakeTimers() // [!code ++] + vi.setSystemTime(new Date('2026-01-01')) // [!code ++] + + expect(new Date().toISOString()).toBe('2026-01-01T00:00:00.000Z') + + MockDate.reset() // [!code --] + vi.useRealTimers() // [!code ++] +}) +``` + +## `node:test` + +[`node:test`](https://nodejs.org/en/learn/test-runner/mocking#time) supports freezing time via `mock.timers` since node 20.4.0 and later. + +```ts +import MockDate from 'mockdate' // [!code --] +import { test } from 'node:test' // [!code ++] +import assert from 'node:assert/strict' // [!code ++] + +test('freeze date', (t) => { + MockDate.set('2026-01-01') // [!code --] + t.mock.timers.enable({ apis: ['Date'], now: new Date('2026-01-01') }) // [!code ++] + + assert.equal(new Date().toISOString(), '2026-01-01T00:00:00.000Z') + + MockDate.reset() // [!code --] +}) +``` + +## `bun:test` + +[`bun:test`](https://bun.com/docs/guides/test/mock-clock) provides `mock.timers.enable()` for freezing the clock in tests. + +```ts +import MockDate from 'mockdate' // [!code --] +import { test, expect, mock } from 'bun:test' // [!code ++] + +test('freeze date', () => { + MockDate.set('2026-01-01') // [!code --] + mock.timers.enable({ now: new Date('2026-01-01') }) // [!code ++] + + expect(new Date().toISOString()).toBe('2026-01-01T00:00:00.000Z') + + MockDate.reset() // [!code --] + mock.timers.reset() // [!code ++] +}) +``` diff --git a/manifests/preferred.json b/manifests/preferred.json index c06ac71..960e0c8 100644 --- a/manifests/preferred.json +++ b/manifests/preferred.json @@ -2394,6 +2394,12 @@ "replacements": ["fs.mkdir"], "url": {"type": "e18e", "id": "mkdirp"} }, + "mockdate": { + "type": "module", + "moduleName": "mockdate", + "replacements": ["node:test", "vitest", "bun:test"], + "url": {"type": "e18e", "id": "mockdate"} + }, "moment": { "type": "module", "moduleName": "moment", @@ -2831,10 +2837,7 @@ "featureId": "async-clipboard", "compatKey": "api.Clipboard" }, - "url": { - "type": "mdn", - "id": "Web/API/Clipboard_API" - } + "url": {"type": "mdn", "id": "Web/API/Clipboard_API"} }, "Date": { "id": "Date", @@ -2931,6 +2934,12 @@ }, "url": {"type": "node", "id": "api/module.html#modulebuiltinmodules"} }, + "bun:test": { + "id": "bun:test", + "type": "documented", + "url": {"type": "e18e", "id": "mockdate"}, + "replacementModule": "bun:test" + }, "concurrently": { "id": "concurrently", "type": "documented", @@ -3288,6 +3297,12 @@ "nodeFeatureId": {"moduleName": "node:stream"}, "url": {"type": "node", "id": "api/stream.html"} }, + "node:test": { + "id": "node:test", + "type": "native", + "nodeFeatureId": {"moduleName": "node:test"}, + "url": {"type": "node", "id": "api/test.html"} + }, "npm-run-all2": { "id": "npm-run-all2", "type": "documented", @@ -3548,6 +3563,12 @@ "url": {"type": "e18e", "id": "source-map-explorer"}, "replacementModule": "webpack-bundle-analyzer" }, + "vitest": { + "id": "vitest", + "type": "documented", + "url": {"type": "e18e", "id": "mockdate"}, + "replacementModule": "vitest" + }, "webpack.output.clean": { "id": "webpack.output.clean", "type": "documented", From eda6e79277a198a627f7ca68c41c2b98e52f5e0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ferreira?= Date: Tue, 7 Apr 2026 21:40:33 +0100 Subject: [PATCH 2/6] Update docs/modules/mockdate.md Co-authored-by: James Garbutt <43081j@users.noreply.github.com> --- docs/modules/mockdate.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/modules/mockdate.md b/docs/modules/mockdate.md index ece0bfe..4e4bd5e 100644 --- a/docs/modules/mockdate.md +++ b/docs/modules/mockdate.md @@ -4,7 +4,7 @@ description: Modern alternatives to the mockdate package for freezing time in te # Replacements for `mockdate` -`mockdate` is mainly used in tests, and modern test runners already include built-in APIs for freezing time without pulling in an extra dependency. +`mockdate` is mainly used in tests, and modern test runners already include built-in APIs for mocking time without pulling in an extra dependency. ## `vitest` From 180356fcc329100c0a974cbaae32645b2de0782e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ferreira?= Date: Tue, 7 Apr 2026 21:40:42 +0100 Subject: [PATCH 3/6] Update docs/modules/mockdate.md Co-authored-by: James Garbutt <43081j@users.noreply.github.com> --- docs/modules/mockdate.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/modules/mockdate.md b/docs/modules/mockdate.md index 4e4bd5e..9fefbbf 100644 --- a/docs/modules/mockdate.md +++ b/docs/modules/mockdate.md @@ -8,7 +8,7 @@ description: Modern alternatives to the mockdate package for freezing time in te ## `vitest` -[`vitest`](https://vitest.dev/guide/mocking.html#mock-the-current-date) provides `vi.useFakeTimers()` and `vi.setSystemTime()` for freezing the current date during tests. +[`vitest`](https://vitest.dev/guide/mocking.html#mock-the-current-date) provides `vi.useFakeTimers()` and `vi.setSystemTime()` for mocking the current date during tests. ```ts import MockDate from 'mockdate' // [!code --] From 6d76df5bb8c3a09bf163527ab794dd8fe2ad8030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ferreira?= Date: Tue, 7 Apr 2026 21:41:00 +0100 Subject: [PATCH 4/6] Update docs/modules/mockdate.md Co-authored-by: James Garbutt <43081j@users.noreply.github.com> --- docs/modules/mockdate.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/modules/mockdate.md b/docs/modules/mockdate.md index 9fefbbf..7b91834 100644 --- a/docs/modules/mockdate.md +++ b/docs/modules/mockdate.md @@ -28,7 +28,7 @@ test('freeze date', () => { ## `node:test` -[`node:test`](https://nodejs.org/en/learn/test-runner/mocking#time) supports freezing time via `mock.timers` since node 20.4.0 and later. +[`node:test`](https://nodejs.org/en/learn/test-runner/mocking#time) supports mocking time via `mock.timers` since node 20.4.0 and later. ```ts import MockDate from 'mockdate' // [!code --] From cf4753d8bf6ec7f344b441b8b5991a7148a132e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ferreira?= Date: Tue, 7 Apr 2026 21:41:17 +0100 Subject: [PATCH 5/6] Update docs/modules/mockdate.md Co-authored-by: James Garbutt <43081j@users.noreply.github.com> --- docs/modules/mockdate.md | 2 +- manifests/preferred.json | 19 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/docs/modules/mockdate.md b/docs/modules/mockdate.md index 7b91834..3a067d4 100644 --- a/docs/modules/mockdate.md +++ b/docs/modules/mockdate.md @@ -47,7 +47,7 @@ test('freeze date', (t) => { ## `bun:test` -[`bun:test`](https://bun.com/docs/guides/test/mock-clock) provides `mock.timers.enable()` for freezing the clock in tests. +[`bun:test`](https://bun.com/docs/guides/test/mock-clock) provides `mock.timers.enable()` for mocking time in tests. ```ts import MockDate from 'mockdate' // [!code --] diff --git a/manifests/preferred.json b/manifests/preferred.json index 960e0c8..e7f6234 100644 --- a/manifests/preferred.json +++ b/manifests/preferred.json @@ -2936,9 +2936,8 @@ }, "bun:test": { "id": "bun:test", - "type": "documented", - "url": {"type": "e18e", "id": "mockdate"}, - "replacementModule": "bun:test" + "type": "native", + "url": "https://bun.com/docs/test" }, "concurrently": { "id": "concurrently", @@ -3298,7 +3297,7 @@ "url": {"type": "node", "id": "api/stream.html"} }, "node:test": { - "id": "node:test", + "id": "node:test", "type": "native", "nodeFeatureId": {"moduleName": "node:test"}, "url": {"type": "node", "id": "api/test.html"} @@ -3557,18 +3556,18 @@ "nodeFeatureId": {"moduleName": "node:util", "exportName": "types"}, "url": {"type": "node", "id": "api/util.html#utiltypes"} }, - "webpack-bundle-analyzer": { - "id": "webpack-bundle-analyzer", - "type": "documented", - "url": {"type": "e18e", "id": "source-map-explorer"}, - "replacementModule": "webpack-bundle-analyzer" - }, "vitest": { "id": "vitest", "type": "documented", "url": {"type": "e18e", "id": "mockdate"}, "replacementModule": "vitest" }, + "webpack-bundle-analyzer": { + "id": "webpack-bundle-analyzer", + "type": "documented", + "url": {"type": "e18e", "id": "source-map-explorer"}, + "replacementModule": "webpack-bundle-analyzer" + }, "webpack.output.clean": { "id": "webpack.output.clean", "type": "documented", From 808c60ab64f5d97cd4ec28495a0d6c483448e9c5 Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Wed, 8 Apr 2026 17:24:23 +0100 Subject: [PATCH 6/6] Apply suggestions from code review Co-authored-by: James Garbutt <43081j@users.noreply.github.com> --- docs/modules/mockdate.md | 2 +- manifests/preferred.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/modules/mockdate.md b/docs/modules/mockdate.md index 3a067d4..405fdae 100644 --- a/docs/modules/mockdate.md +++ b/docs/modules/mockdate.md @@ -1,5 +1,5 @@ --- -description: Modern alternatives to the mockdate package for freezing time in tests +description: Modern alternatives to the mockdate package for mocking time in tests --- # Replacements for `mockdate` diff --git a/manifests/preferred.json b/manifests/preferred.json index e7f6234..e317b34 100644 --- a/manifests/preferred.json +++ b/manifests/preferred.json @@ -3299,7 +3299,7 @@ "node:test": { "id": "node:test", "type": "native", - "nodeFeatureId": {"moduleName": "node:test"}, + "nodeFeatureId": {"moduleName": "test"}, "url": {"type": "node", "id": "api/test.html"} }, "npm-run-all2": {