Skip to content

Commit 034505e

Browse files
Aviv KellerRafaelGSS
Aviv Keller
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 49d5522 commit 034505e

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
@@ -328,6 +328,9 @@ class MockTimers {
328328
#createDate() {
329329
kMock ??= Symbol('MockTimers');
330330
const NativeDateConstructor = this.#nativeDateDescriptor.value;
331+
if (NativeDateConstructor.isMock) {
332+
throw new ERR_INVALID_STATE('Date is already being mocked!');
333+
}
331334
/**
332335
* Function to mock the Date constructor, treats cases as per ECMA-262
333336
* 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)