Skip to content

Commit fe557f6

Browse files
authored
Support performance.measure+mark (#499)
* Fix a mistake in release doc * fix: Support performance.measure+mark fixes #493
1 parent 2433719 commit fe557f6

3 files changed

Lines changed: 46 additions & 7 deletions

File tree

RELEASE.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@ You'll need a working installation of [git-extras](https://github.com/tj/git-ext
88
$ npm version x.y.z # or npm version [patch|minor|major]
99
```
1010

11-
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.
12-
13-
## Publish to NPM
14-
15-
```
16-
$ npm publish
17-
```
11+
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.
1812

1913
## Create a GitHub release
2014

src/fake-timers-src.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,25 @@ function withGlobal(_global) {
217217
}
218218
isPresent.Date = true;
219219

220+
/**
221+
* The PerformanceEntry object encapsulates a single performance metric
222+
* that is part of the browser's performance timeline.
223+
*
224+
* This is an object returned by the `mark` and `measure` methods on the Performance prototype
225+
*/
226+
class FakePerformanceEntry {
227+
constructor(name, entryType, startTime, duration) {
228+
this.name = name;
229+
this.entryType = entryType;
230+
this.startTime = startTime;
231+
this.duration = duration;
232+
}
233+
234+
toJSON() {
235+
return JSON.stringify({ ...this });
236+
}
237+
}
238+
220239
/**
221240
* @param {number} num
222241
* @returns {boolean}
@@ -1794,6 +1813,11 @@ function withGlobal(_global) {
17941813
: NOOP;
17951814
}
17961815
});
1816+
// ensure `mark` returns a value that is valid
1817+
clock.performance.mark = (name) =>
1818+
new FakePerformanceEntry(name, "mark", 0, 0);
1819+
clock.performance.measure = (name) =>
1820+
new FakePerformanceEntry(name, "measure", 0, 100);
17971821
} else if ((config.toFake || []).includes("performance")) {
17981822
return handleMissingTimer("performance");
17991823
}

test/fake-timers-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3632,6 +3632,27 @@ describe("FakeTimers", function () {
36323632
this.clock.uninstall();
36333633
});
36343634

3635+
it("should create fake versions of `mark` and `measure` that return PerformanceEntry objects", function () {
3636+
if (typeof Performance === "undefined") {
3637+
return this.skip();
3638+
}
3639+
3640+
function testEntry(performanceEntry) {
3641+
assert.keys(performanceEntry, [
3642+
"startTime",
3643+
"duration",
3644+
"name",
3645+
"entryType",
3646+
]);
3647+
3648+
assert(typeof performanceEntry.toJSON() === "string");
3649+
}
3650+
3651+
this.clock = FakeTimers.install();
3652+
testEntry(performance.mark("foo"));
3653+
testEntry(performance.measure("bar", "s", "t"));
3654+
});
3655+
36353656
it("should replace the getEntries, getEntriesByX methods with noops that return []", function () {
36363657
if (typeof Performance === "undefined") {
36373658
return this.skip();

0 commit comments

Comments
 (0)