Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
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' });
});
});
});
Loading