Repository: sinonjs/fake-timers
Pull request #499 (2024-08-24 17:49:47)

Support performance.measure+mark
- **Fix a mistake in release doc**
- **fix: Support performance.measure+mark**



Patch:
diff --git a/RELEASE.md b/RELEASE.md
index 1cead85..a1a7461 100644
--- a/RELEASE.md
+++ b/RELEASE.md
@@ -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
 
diff --git a/src/fake-timers-src.js b/src/fake-timers-src.js
index 3119507..b7cb1b1 100644
--- a/src/fake-timers-src.js
+++ b/src/fake-timers-src.js
@@ -217,6 +217,25 @@ function withGlobal(_global) {
     }
     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}
@@ -1794,6 +1813,11 @@ function withGlobal(_global) {
                                 : 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");
             }


Test patch:
diff --git a/test/fake-timers-test.js b/test/fake-timers-test.js
index 42ea8fb..80e73eb 100644
--- a/test/fake-timers-test.js
+++ b/test/fake-timers-test.js
@@ -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();