diff --git a/lib/internal/test_runner/mock/mock_timers.js b/lib/internal/test_runner/mock/mock_timers.js index 66375c6688e..c479f69ed0d 100644 --- a/lib/internal/test_runner/mock/mock_timers.js +++ b/lib/internal/test_runner/mock/mock_timers.js @@ -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 diff --git a/test/parallel/test-runner-mock-timers-date.js b/test/parallel/test-runner-mock-timers-date.js index ebd1e430be8..7cee835ecca 100644 --- a/test/parallel/test-runner-mock-timers-date.js +++ b/test/parallel/test-runner-mock-timers-date.js @@ -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' }); + }); + }); });