Skip to content

Commit a645d4b

Browse files
avivkellermarco-ippolito
authored andcommitted
test_runner: error on mocking an already mocked date
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]>
1 parent ff16e13 commit a645d4b

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
@@ -331,6 +331,9 @@ class MockTimers {
331331
#createDate() {
332332
kMock ??= Symbol('MockTimers');
333333
const NativeDateConstructor = this.#nativeDateDescriptor.value;
334+
if (NativeDateConstructor.isMock) {
335+
throw new ERR_INVALID_STATE('Date is already being mocked!');
336+
}
334337
/**
335338
* Function to mock the Date constructor, treats cases as per ECMA-262
336339
* 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)