-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
ref(core): Streamline and test browserPerformanceTimeOrigin
#18708
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
+163
−15
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0417dd6
ref(core): Streamline and test `browserPerformanceTimeOrigin`
Lms24 e32955f
add test file
Lms24 3dca951
fix repeated cache misses when caching `undefined`
Lms24 3b6d45e
Apply suggestions from code review
Lms24 faec1fb
Update packages/core/test/lib/utils/time.test.ts
Lms24 12da3f0
format
Lms24 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| async function getFreshPerformanceTimeOrigin() { | ||
| // Adding the query param with the date, forces a fresh import each time this is called | ||
| // otherwise, the dynamic import would be cached and thus fall back to the cached value. | ||
| const timeModule = await import(`../../../src/utils/time?update=${Date.now()}`); | ||
| return timeModule.browserPerformanceTimeOrigin(); | ||
| } | ||
|
|
||
| const RELIABLE_THRESHOLD_MS = 3_600_000; | ||
|
|
||
| describe('browserPerformanceTimeOrigin', () => { | ||
| it('returns `performance.timeOrigin` if it is available and reliable', async () => { | ||
| const timeOrigin = await getFreshPerformanceTimeOrigin(); | ||
| expect(timeOrigin).toBeDefined(); | ||
| expect(timeOrigin).toBeGreaterThan(0); | ||
| expect(timeOrigin).toBeLessThan(Date.now()); | ||
| expect(timeOrigin).toBe(performance.timeOrigin); | ||
| }); | ||
|
|
||
| it('returns `undefined` if `performance.now` is not available', async () => { | ||
| vi.stubGlobal('performance', undefined); | ||
|
|
||
| const timeOrigin = await getFreshPerformanceTimeOrigin(); | ||
| expect(timeOrigin).toBeUndefined(); | ||
|
|
||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| it('returns `Date.now()` if `performance.timeOrigin` is not reliable', async () => { | ||
| const currentTimeMs = 1767778040866; | ||
|
|
||
| const unreliableTime = currentTimeMs - RELIABLE_THRESHOLD_MS - 2_000; | ||
|
|
||
| const timeSincePageloadMs = 1_234.789; | ||
|
|
||
| vi.useFakeTimers(); | ||
| vi.setSystemTime(new Date(currentTimeMs)); | ||
|
|
||
| vi.stubGlobal('performance', { | ||
| timeOrigin: unreliableTime, | ||
| timing: { | ||
| navigationStart: unreliableTime, | ||
| }, | ||
| now: () => timeSincePageloadMs, | ||
| }); | ||
|
|
||
| const timeOrigin = await getFreshPerformanceTimeOrigin(); | ||
| expect(timeOrigin).toBe(1767778040866); | ||
|
|
||
| vi.useRealTimers(); | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| it('returns `performance.timing.navigationStart` if `performance.timeOrigin` is not available', async () => { | ||
| const currentTimeMs = 1767778040870; | ||
|
|
||
| const navigationStartMs = currentTimeMs - 2_000; | ||
|
|
||
| const timeSincePageloadMs = 1_234.789; | ||
|
|
||
| vi.useFakeTimers(); | ||
| vi.setSystemTime(new Date(currentTimeMs)); | ||
|
|
||
| vi.stubGlobal('performance', { | ||
| timeOrigin: undefined, | ||
| timing: { | ||
| navigationStart: navigationStartMs, | ||
| }, | ||
| now: () => timeSincePageloadMs, | ||
| }); | ||
|
|
||
| const timeOrigin = await getFreshPerformanceTimeOrigin(); | ||
| expect(timeOrigin).toBe(navigationStartMs); | ||
|
|
||
| vi.useRealTimers(); | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| it('returns `performance.timing.navigationStart` if `performance.timeOrigin` is less reliable', async () => { | ||
| const currentTimeMs = 1767778040874; | ||
|
|
||
| const navigationStartMs = currentTimeMs - 2_000; | ||
|
|
||
| const timeSincePageloadMs = 1_234.789; | ||
|
|
||
| vi.useFakeTimers(); | ||
| vi.setSystemTime(new Date(currentTimeMs)); | ||
|
|
||
| vi.stubGlobal('performance', { | ||
| timeOrigin: navigationStartMs - 1, | ||
| timing: { | ||
| navigationStart: navigationStartMs, | ||
| }, | ||
| now: () => timeSincePageloadMs, | ||
| }); | ||
|
|
||
| const timeOrigin = await getFreshPerformanceTimeOrigin(); | ||
| expect(timeOrigin).toBe(navigationStartMs); | ||
|
|
||
| vi.useRealTimers(); | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| describe('caching', () => { | ||
| it('caches `undefined` result', async () => { | ||
| vi.stubGlobal('performance', undefined); | ||
|
|
||
| const timeModule = await import(`../../../src/utils/time?update=${Date.now()}`); | ||
|
|
||
| const result1 = timeModule.browserPerformanceTimeOrigin(); | ||
|
|
||
| expect(result1).toBeUndefined(); | ||
|
|
||
| vi.stubGlobal('performance', { | ||
| timeOrigin: 1000, | ||
| now: () => 100, | ||
| }); | ||
|
|
||
| const result2 = timeModule.browserPerformanceTimeOrigin(); | ||
| expect(result2).toBeUndefined(); // Should still be undefined due to caching | ||
|
|
||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| it('caches `number` result', async () => { | ||
| const timeModule = await import(`../../../src/utils/time?update=${Date.now()}`); | ||
| const result = timeModule.browserPerformanceTimeOrigin(); | ||
| const timeOrigin = performance.timeOrigin; | ||
| expect(result).toBe(timeOrigin); | ||
|
|
||
| vi.stubGlobal('performance', { | ||
| now: undefined, | ||
| }); | ||
|
|
||
| const result2 = timeModule.browserPerformanceTimeOrigin(); | ||
| expect(result2).toBe(timeOrigin); | ||
|
|
||
| vi.unstubAllGlobals(); | ||
| }); | ||
| }); | ||
| }); |
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.