Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: error on mocking an already mocked date #55858

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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!');
RedYetiDev marked this conversation as resolved.
Show resolved Hide resolved
}
/**
* 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