mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00: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:
parent
b648d37ad7
commit
775a10039a
@ -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
|
||||
|
@ -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…
Reference in New Issue
Block a user