Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 363e7d5

Browse filesBrowse files
avivkellerruyadorno
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 6858f7a commit 363e7d5
Copy full SHA for 363e7d5

2 files changed

+10Lines changed: 10 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/test_runner/mock/mock_timers.js‎

Copy file name to clipboardExpand all lines: lib/internal/test_runner/mock/mock_timers.js
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ class MockTimers {
335335
#createDate() {
336336
kMock ??= Symbol('MockTimers');
337337
const NativeDateConstructor = this.#nativeDateDescriptor.value;
338+
if (NativeDateConstructor.isMock) {
339+
throw new ERR_INVALID_STATE('Date is already being mocked!');
340+
}
338341
/**
339342
* Function to mock the Date constructor, treats cases as per ECMA-262
340343
* and returns a Date object with a mocked implementation
Collapse file

‎test/parallel/test-runner-mock-timers-date.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-runner-mock-timers-date.js
+7Lines changed: 7 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.