0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 13:09:21 +01:00

test_runner: error on mocking an already mocked date

Fixes #55849

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Aviv Keller 2024-11-17 05:25:12 -05:00 committed by GitHub
parent b648d37ad7
commit 775a10039a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 0 deletions

View File

@ -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

View File

@ -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' });
});
});
});