-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
base: main
Are you sure you want to change the base?
Conversation
Review requested:
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #55858 +/- ##
=======================================
Coverage 88.41% 88.41%
=======================================
Files 654 654
Lines 187824 187827 +3
Branches 36134 36137 +3
=======================================
+ Hits 166060 166064 +4
+ Misses 15005 15003 -2
- Partials 6759 6760 +1
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is throwing necessary/helpful? If it's already mocked, why not just silently abort? (don't re-mock, but don't throw)
@@ -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!'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit
throw new ERR_INVALID_STATE('Date is already being mocked!'); | |
throw new ERR_INVALID_STATE('Date is already mocked.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm following the same format from the other throw in the file (which also includes the !
)
IMO throwing is helpful because in this case, we aren't the ones mocking. If the user attempts to perform any other mocking utilities (such as restoring the mock), it won't work. |
Fixes #55849
Previously, the properties from the global
Date
, including the not-writableisMock
and[kMock]
values, would be copied if the globalDate
was already mocked. This became an issue because the mock setup would attempt overwrite these properties.Now,
ERR_INVALID_STATE
is thrown, asDate is already being mocked!
.Example
Before
After