Skip to content

Commit 363e7d5

Browse files
avivkellerruyadorno
authored andcommitted
test_runner: error on mocking an already mocked date
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>
1 parent 6858f7a commit 363e7d5

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

lib/internal/test_runner/mock/mock_timers.js

+3
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ class MockTimers {
335335
#createDate() {
336336
kMock ??= Symbol('MockTimers');
337337
const NativeDateConstructor = this.#nativeDateDescriptor.value;
338+
if (NativeDateConstructor.isMock) {
339+
throw new ERR_INVALID_STATE('Date is already being mocked!');
340+
}
338341
/**
339342
* Function to mock the Date constructor, treats cases as per ECMA-262
340343
* and returns a Date object with a mocked implementation

test/parallel/test-runner-mock-timers-date.js

+7
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,11 @@ describe('Mock Timers Date Test Suite', () => {
117117
assert.strictEqual(fn.mock.callCount(), 0);
118118
clearTimeout(id);
119119
});
120+
121+
it((t) => {
122+
t.mock.timers.enable();
123+
t.test('should throw when a already-mocked Date is mocked', (t2) => {
124+
assert.throws(() => t2.mock.timers.enable(), { code: 'ERR_INVALID_STATE' });
125+
});
126+
});
120127
});

0 commit comments

Comments
 (0)