-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
feat(fake-timers): basic support for Temporal
#16128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fce24a0
feat(fake-timers): basic support for `Temporal`
SimenB ba04969
changelog
SimenB ae6126a
address review
SimenB bc221e7
fix ci
SimenB fb4b089
type tests
SimenB 06d3c26
oops
SimenB 021cd99
review comments
SimenB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| import {onNodeVersions} from '@jest/test-utils'; | ||
| import runJest from '../runJest'; | ||
|
|
||
| onNodeVersions('>=26', () => { | ||
| test('useFakeTimers({now}) and setSystemTime accept Temporal instances', () => { | ||
| const result = runJest('fake-timers-temporal'); | ||
| expect(result.exitCode).toBe(0); | ||
| }); | ||
|
SimenB marked this conversation as resolved.
|
||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| const DATE = new Date('2026-01-01T00:00:00Z'); | ||
| const EPOCH_MS = DATE.getTime(); | ||
| const ISO = DATE.toISOString(); | ||
|
|
||
| describe('Temporal support in fake timers', () => { | ||
| afterEach(() => { | ||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| test('useFakeTimers({now}) accepts Temporal.Instant', () => { | ||
| jest.useFakeTimers({now: Temporal.Instant.from(ISO)}); | ||
| expect(Date.now()).toBe(EPOCH_MS); | ||
| }); | ||
|
|
||
| test('useFakeTimers({now}) accepts Temporal.ZonedDateTime', () => { | ||
| const zdt = Temporal.Instant.from(ISO).toZonedDateTimeISO('UTC'); | ||
| jest.useFakeTimers({now: zdt}); | ||
|
Comment on lines
+17
to
+24
|
||
| expect(Date.now()).toBe(EPOCH_MS); | ||
| }); | ||
|
|
||
| test('setSystemTime accepts Temporal.Instant', () => { | ||
| jest.useFakeTimers(); | ||
| jest.setSystemTime(Temporal.Instant.from(ISO)); | ||
| expect(Date.now()).toBe(EPOCH_MS); | ||
| }); | ||
|
|
||
| test('setSystemTime accepts Temporal.ZonedDateTime', () => { | ||
| jest.useFakeTimers(); | ||
| const zdt = Temporal.Instant.from(ISO).toZonedDateTimeISO('UTC'); | ||
| jest.setSystemTime(zdt); | ||
| expect(Date.now()).toBe(EPOCH_MS); | ||
| }); | ||
|
|
||
| test('advanceTimersByTime accepts Temporal.Duration', () => { | ||
| jest.useFakeTimers({now: EPOCH_MS}); | ||
| jest.advanceTimersByTime(Temporal.Duration.from({hours: 1})); | ||
| expect(Date.now()).toBe(EPOCH_MS + 3_600_000); | ||
| }); | ||
|
|
||
| test('advanceTimersByTimeAsync accepts Temporal.Duration', async () => { | ||
| jest.useFakeTimers({now: EPOCH_MS}); | ||
| await jest.advanceTimersByTimeAsync(Temporal.Duration.from({minutes: 30})); | ||
| expect(Date.now()).toBe(EPOCH_MS + 1_800_000); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "name": "fake-timers-temporal" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| export type TemporalEpochLike = {epochMilliseconds: number}; | ||
| export type TemporalDurationLike = {total(options: {unit: string}): number}; | ||
|
|
||
| export function toEpochMs( | ||
| value: number | Date | TemporalEpochLike | undefined, | ||
| ): number | undefined { | ||
| if (value == null) return undefined; | ||
| if (typeof value === 'number') return value; | ||
| // Use duck-typing rather than instanceof to handle cross-realm Date objects | ||
| // (e.g. Sinon's ClockDate, which extends Date but may fail instanceof checks | ||
| // across module boundaries in a webpack bundle). | ||
| if (typeof (value as Date).getTime === 'function') | ||
| return (value as Date).getTime(); | ||
| return (value as TemporalEpochLike).epochMilliseconds; | ||
| } | ||
|
|
||
| export function toDurationMs(value: number | TemporalDurationLike): number { | ||
| if (typeof value === 'number') return value; | ||
| return value.total({unit: 'millisecond'}); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.