Skip to content

Commit

Permalink
test_runner: error on mocking an already mocked date
Browse files Browse the repository at this point in the history
Fixes #55849

PR-URL: #55858
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
RedYetiDev authored and RafaelGSS committed Nov 18, 2024
1 parent 49d5522 commit 034505e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/internal/test_runner/mock/mock_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ class MockTimers {
#createDate() {
kMock ??= Symbol('MockTimers');
const NativeDateConstructor = this.#nativeDateDescriptor.value;
if (NativeDateConstructor.isMock) {
throw new ERR_INVALID_STATE('Date is already being mocked!');
}
/**
* Function to mock the Date constructor, treats cases as per ECMA-262
* and returns a Date object with a mocked implementation
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-runner-mock-timers-date.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,11 @@ describe('Mock Timers Date Test Suite', () => {
assert.strictEqual(fn.mock.callCount(), 0);
clearTimeout(id);
});

it((t) => {
t.mock.timers.enable();
t.test('should throw when a already-mocked Date is mocked', (t2) => {
assert.throws(() => t2.mock.timers.enable(), { code: 'ERR_INVALID_STATE' });
});
});
});

0 comments on commit 034505e

Please sign in to comment.