Implement Symbol.hasInstance for MockDate#616
Conversation
🦋 Changeset detectedLatest commit: 3c3bf25 The changes in this PR will be included in the next version bump. This PR includes changesets to release 13 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
@tajo would you like me to add a Changeset to this PR? |
|
Note that the original MockDate.prototype = RealDate.prototype;With modern JS this leads to the following TypeError: I believe this to be the reason why the prototype line has been omitted from ladles mock date, and it is also what causes the issue in date-fns That is why I'm asking to adjust the implementation of |
|
@runjak makes sense. Can you please add some unit test and changeset? Thanks! |
6d33f81 to
cdcc615
Compare
By adding Symbol.hasInstance to MockDate we allow for instanceof to work with mocked Dates. When the global Date object is replaced with a mocked one unmocked Dates are also instances of MockedDate and vice versa.
cdcc615 to
3c3bf25
Compare
|
@tajo thanks for the reply 🙏 |
|
Hey, I hope I'm not bothering you with this and you're doing fine stress-wise. Just wanted to note that from my perspective all should be fine here and if you like you could merge my changes. |
Hey 👋
so I'm enjoying use of both: ladle and date-fns. And this leads me to a situation where I'm using date-fns
isValidandisDateto check some data. In particular we've got a component that does date based calculations based on aMockDate.My understanding is that date-fns depends on the global Date object in code like this:
So when calculating new dates we get instances of
RealDate, and check whether aRealDateisinstanceofaMockDate, becauseMockDaterightly replaces the global date when mocking with ladle.For inheritance this is usually the wrong way: a parent is not an instance of the child class. My suggestions would be to treat it as that though in case of
MockDatewith the following snippet:I've tested it by test-wise appending (but not comitting) this code to
packages/ladle/lib/app/src/mock-date.ts:It leads to output like this: