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 <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Chemi Atlow <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
  • Loading branch information
RedYetiDev authored and ruyadorno committed Nov 27, 2024
1 parent 6858f7a commit 363e7d5
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 @@ -335,6 +335,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 363e7d5

Please sign in to comment.