Skip to content
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

Support performance.measure+mark #499

Merged
merged 2 commits into from
Aug 24, 2024
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
8 changes: 1 addition & 7 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ You'll need a working installation of [git-extras](https://github.com/tj/git-ext
$ npm version x.y.z # or npm version [patch|minor|major]
```

Runs the tests, builds a changelog and the authors file, updates package.json, creates a new tag and pushes the tag and its commits to the sinon repo.

## Publish to NPM

```
$ npm publish
```
Runs the tests, builds a changelog and the authors file, updates package.json, creates a new tag and pushes the tag and its commits to the sinon repo, as well as publishing.

## Create a GitHub release

Expand Down
24 changes: 24 additions & 0 deletions src/fake-timers-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* Queues a function to be called during a browser's idle periods
* @callback RequestIdleCallback
* @param {function(IdleDeadline)} callback

Check warning on line 27 in src/fake-timers-src.js

View workflow job for this annotation

GitHub Actions / lint

Syntax error in type: function(IdleDeadline)
* @param {{timeout: number}} options - an options object
* @returns {number} the id
*/
Expand All @@ -51,13 +51,13 @@

/**
* @typedef RequestAnimationFrame
* @property {function(number):void} requestAnimationFrame

Check warning on line 54 in src/fake-timers-src.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @Property "requestAnimationFrame" description
* @returns {number} - the id
*/

/**
* @typedef Performance
* @property {function(): number} now

Check warning on line 60 in src/fake-timers-src.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @Property "now" description
*/

/* eslint-disable jsdoc/require-property-description */
Expand Down Expand Up @@ -217,6 +217,25 @@
}
isPresent.Date = true;

/**
* The PerformanceEntry object encapsulates a single performance metric
* that is part of the browser's performance timeline.
*
* This is an object returned by the `mark` and `measure` methods on the Performance prototype
*/
class FakePerformanceEntry {
constructor(name, entryType, startTime, duration) {
this.name = name;
this.entryType = entryType;
this.startTime = startTime;
this.duration = duration;
}

toJSON() {
return JSON.stringify({ ...this });
}
}

/**
* @param {number} num
* @returns {boolean}
Expand Down Expand Up @@ -329,7 +348,7 @@
return timer && timer.callAt >= from && timer.callAt <= to;
}

/**

Check warning on line 351 in src/fake-timers-src.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @returns declaration
* @param {Clock} clock
* @param {Timer} job
*/
Expand Down Expand Up @@ -409,7 +428,7 @@
* @param {number} minute
* @param {number} second
* @param {number} ms
* @returns void

Check warning on line 431 in src/fake-timers-src.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @returns type
*/
// eslint-disable-next-line no-unused-vars
constructor(year, month, date, hour, minute, second, ms) {
Expand Down Expand Up @@ -441,7 +460,7 @@
/**
* A normal Class constructor cannot be called without `new`, but Date can, so we need
* to wrap it in a Proxy in order to ensure this functionality of Date is kept intact
* @type {ClockDate}

Check warning on line 463 in src/fake-timers-src.js

View workflow job for this annotation

GitHub Actions / lint

The type 'ClockDate' is undefined
*/
const ClockDateProxy = new Proxy(ClockDate, {
// handler for [[Call]] invocations (i.e. not using `new`)
Expand Down Expand Up @@ -637,7 +656,7 @@
}

/* eslint consistent-return: "off" */
/**

Check warning on line 659 in src/fake-timers-src.js

View workflow job for this annotation

GitHub Actions / lint

JSDoc @returns declaration present but return expression not available in function
* Timer comparitor
* @param {Timer} a
* @param {Timer} b
Expand Down Expand Up @@ -768,7 +787,7 @@
}
}

/**

Check warning on line 790 in src/fake-timers-src.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @returns declaration
* Gets clear handler name for a given timer type
* @param {string} ttype
*/
Expand All @@ -779,7 +798,7 @@
return `clear${ttype}`;
}

/**

Check warning on line 801 in src/fake-timers-src.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @returns declaration
* Gets schedule handler name for a given timer type
* @param {string} ttype
*/
Expand All @@ -790,7 +809,7 @@
return `set${ttype}`;
}

/**

Check warning on line 812 in src/fake-timers-src.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @returns declaration
* Creates an anonymous function to warn only once
*/
function createWarnOnce() {
Expand Down Expand Up @@ -1794,6 +1813,11 @@
: NOOP;
}
});
// ensure `mark` returns a value that is valid
clock.performance.mark = (name) =>
new FakePerformanceEntry(name, "mark", 0, 0);
clock.performance.measure = (name) =>
new FakePerformanceEntry(name, "measure", 0, 100);
} else if ((config.toFake || []).includes("performance")) {
return handleMissingTimer("performance");
}
Expand Down
21 changes: 21 additions & 0 deletions test/fake-timers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3632,6 +3632,27 @@ describe("FakeTimers", function () {
this.clock.uninstall();
});

it("should create fake versions of `mark` and `measure` that return PerformanceEntry objects", function () {
if (typeof Performance === "undefined") {
return this.skip();
}

function testEntry(performanceEntry) {
assert.keys(performanceEntry, [
"startTime",
"duration",
"name",
"entryType",
]);

assert(typeof performanceEntry.toJSON() === "string");
}

this.clock = FakeTimers.install();
testEntry(performance.mark("foo"));
testEntry(performance.measure("bar", "s", "t"));
});

it("should replace the getEntries, getEntriesByX methods with noops that return []", function () {
if (typeof Performance === "undefined") {
return this.skip();
Expand Down
Loading