Skip to content

Commit ff8d907

Browse files
rriskifatso83
andauthored
fix: return fake version for performance.timeOrigin (#515)
* fix: return fake version for `performance.timeOrigin` Fixes #514 * Simplify implementation * Update comment --------- Co-authored-by: Carl-Erik Kopseng <[email protected]>
1 parent 7861c93 commit ff8d907

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

src/fake-timers-src.js

+3-15
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ if (typeof require === "function" && typeof module === "object") {
2323

2424
/**
2525
* Queues a function to be called during a browser's idle periods
26-
*
2726
* @callback RequestIdleCallback
2827
* @param {function(IdleDeadline)} callback
2928
* @param {{timeout: number}} options - an options object
@@ -106,7 +105,6 @@ if (typeof require === "function" && typeof module === "object") {
106105

107106
/**
108107
* Configuration object for the `install` method.
109-
*
110108
* @typedef {object} Config
111109
* @property {number|Date} [now] a number (in milliseconds) or a Date object (default epoch)
112110
* @property {string[]} [toFake] names of the methods that should be faked.
@@ -120,7 +118,6 @@ if (typeof require === "function" && typeof module === "object") {
120118
/* eslint-disable jsdoc/require-property-description */
121119
/**
122120
* The internal structure to describe a scheduled fake timer
123-
*
124121
* @typedef {object} Timer
125122
* @property {Function} func
126123
* @property {*[]} args
@@ -134,7 +131,6 @@ if (typeof require === "function" && typeof module === "object") {
134131

135132
/**
136133
* A Node timer
137-
*
138134
* @typedef {object} NodeImmediate
139135
* @property {function(): boolean} hasRef
140136
* @property {function(): NodeImmediate} ref
@@ -146,7 +142,6 @@ if (typeof require === "function" && typeof module === "object") {
146142

147143
/**
148144
* Mocks available features in the specified global namespace.
149-
*
150145
* @param {*} _global Namespace to mock (e.g. `window`)
151146
* @returns {FakeTimers}
152147
*/
@@ -276,7 +271,6 @@ function withGlobal(_global) {
276271
* Parse strings like "01:10:00" (meaning 1 hour, 10 minutes, 0 seconds) into
277272
* number of milliseconds. This is used to support human-readable strings passed
278273
* to clock.tick()
279-
*
280274
* @param {string} str
281275
* @returns {number}
282276
*/
@@ -312,7 +306,6 @@ function withGlobal(_global) {
312306

313307
/**
314308
* Get the decimal part of the millisecond value as nanoseconds
315-
*
316309
* @param {number} msFloat the number of milliseconds
317310
* @returns {number} an integer number of nanoseconds in the range [0,1e6)
318311
*
@@ -329,7 +322,6 @@ function withGlobal(_global) {
329322

330323
/**
331324
* Used to grok the `now` parameter to createClock.
332-
*
333325
* @param {Date|number} epoch the system time
334326
* @returns {number}
335327
*/
@@ -483,7 +475,6 @@ function withGlobal(_global) {
483475
/**
484476
* A normal Class constructor cannot be called without `new`, but Date can, so we need
485477
* to wrap it in a Proxy in order to ensure this functionality of Date is kept intact
486-
*
487478
* @type {ClockDate}
488479
*/
489480
const ClockDateProxy = new Proxy(ClockDate, {
@@ -510,7 +501,6 @@ function withGlobal(_global) {
510501
* Most of the properties are the original native ones,
511502
* but we need to take control of those that have a
512503
* dependency on the current clock.
513-
*
514504
* @returns {object} the partly fake Intl implementation
515505
*/
516506
function createIntl() {
@@ -683,7 +673,6 @@ function withGlobal(_global) {
683673
/* eslint consistent-return: "off" */
684674
/**
685675
* Timer comparitor
686-
*
687676
* @param {Timer} a
688677
* @param {Timer} b
689678
* @returns {number}
@@ -815,7 +804,6 @@ function withGlobal(_global) {
815804

816805
/**
817806
* Gets clear handler name for a given timer type
818-
*
819807
* @param {string} ttype
820808
*/
821809
function getClearHandler(ttype) {
@@ -827,7 +815,6 @@ function withGlobal(_global) {
827815

828816
/**
829817
* Gets schedule handler name for a given timer type
830-
*
831818
* @param {string} ttype
832819
*/
833820
function getScheduleHandler(ttype) {
@@ -1183,13 +1170,11 @@ function withGlobal(_global) {
11831170

11841171
/**
11851172
* A high resolution timestamp in milliseconds.
1186-
*
11871173
* @typedef {number} DOMHighResTimeStamp
11881174
*/
11891175

11901176
/**
11911177
* performance.now()
1192-
*
11931178
* @returns {DOMHighResTimeStamp}
11941179
*/
11951180
function fakePerformanceNow() {
@@ -1848,6 +1833,9 @@ function withGlobal(_global) {
18481833
new FakePerformanceEntry(name, "mark", 0, 0);
18491834
clock.performance.measure = (name) =>
18501835
new FakePerformanceEntry(name, "measure", 0, 100);
1836+
// `timeOrigin` should return the time of when the Window session started
1837+
// (or the Worker was installed)
1838+
clock.performance.timeOrigin = getEpoch(config.now);
18511839
} else if ((config.toFake || []).includes("performance")) {
18521840
return handleMissingTimer("performance");
18531841
}

test/fake-timers-test.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -3684,6 +3684,16 @@ describe("FakeTimers", function () {
36843684
testEntry(performance.measure("bar", "s", "t"));
36853685
});
36863686

3687+
it("should create fake version of `timeOrigin` that returns the installed time", function () {
3688+
if (typeof Performance === "undefined") {
3689+
return this.skip();
3690+
}
3691+
3692+
this.clock = FakeTimers.install({ now: new Date(1234) });
3693+
assert.isNumber(performance.timeOrigin);
3694+
assert.equals(performance.timeOrigin, 1234);
3695+
});
3696+
36873697
it("should replace the getEntries, getEntriesByX methods with noops that return []", function () {
36883698
if (typeof Performance === "undefined") {
36893699
return this.skip();
@@ -4788,7 +4798,6 @@ describe("FakeTimers", function () {
47884798

47894799
/**
47904800
* Returns elements that are present in both lists.
4791-
*
47924801
* @function
47934802
* @template E
47944803
* @param {E[]} [list1]
@@ -4801,7 +4810,6 @@ describe("FakeTimers", function () {
48014810

48024811
/**
48034812
* Get property names and original values from timers module.
4804-
*
48054813
* @function
48064814
* @param {string[]} [toFake]
48074815
* @returns {{propertyName: string, originalValue: any}[]}
@@ -5948,7 +5956,6 @@ describe("Node Timer: ref(), unref(),hasRef()", function () {
59485956
describe("Intl API", function () {
59495957
/**
59505958
* Tester function to check if the globally hijacked Intl object is plugging into the faked Clock
5951-
*
59525959
* @param {string} ianaTimeZone - IANA time zone name
59535960
* @param {number} timestamp - UNIX timestamp
59545961
* @returns {boolean}

0 commit comments

Comments
 (0)