Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/modules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
65 changes: 65 additions & 0 deletions docs/modules/mockdate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
description: Modern alternatives to the mockdate package for mocking time in tests
---

# Replacements for `mockdate`

`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`

[`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 --]
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 mocking 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 mocking time 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 ++]
})
```
28 changes: 24 additions & 4 deletions manifests/preferred.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -2931,6 +2934,11 @@
},
"url": {"type": "node", "id": "api/module.html#modulebuiltinmodules"}
},
"bun:test": {
"id": "bun:test",
"type": "native",
"url": "https://bun.com/docs/test"
},
"concurrently": {
"id": "concurrently",
"type": "documented",
Expand Down Expand Up @@ -3288,6 +3296,12 @@
"nodeFeatureId": {"moduleName": "node:stream"},
"url": {"type": "node", "id": "api/stream.html"}
},
"node:test": {
"id": "node:test",
"type": "native",
"nodeFeatureId": {"moduleName": "test"},
"url": {"type": "node", "id": "api/test.html"}
},
"npm-run-all2": {
"id": "npm-run-all2",
"type": "documented",
Expand Down Expand Up @@ -3542,6 +3556,12 @@
"nodeFeatureId": {"moduleName": "node:util", "exportName": "types"},
"url": {"type": "node", "id": "api/util.html#utiltypes"}
},
"vitest": {
"id": "vitest",
"type": "documented",
"url": {"type": "e18e", "id": "mockdate"},
"replacementModule": "vitest"
},
"webpack-bundle-analyzer": {
"id": "webpack-bundle-analyzer",
"type": "documented",
Expand Down