-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Spy on global date has very weird behaviors #9185
Comments
have you gone thru previous issues? |
Yes, I tried going through previous issues but didn't found one that was close enough to my problem. I'll take a look at that one |
Hello 👋 In the first case,
So, when you just use the symbol, you end up in case 1. The symbol is ignored, so a new object is created and returned. It is empty because the constructor your are using is a simple function that does not mutate When you use
It depends on what your are trying to achieve, why you are using a symbol in the first place and how you want to make your assertion. As far as I am concerned my experience with mocking // Save the Date constructor for later use
const Date = global.Date;
// When calling `new Date()` in the code that is tested, it delegated to the actual `Date`
// This doesn't work at all when code calls only `Date()`
const mockDate = jest.fn((...args) => {
return new Date(...args)
});
// This is to make `mockDate` look pretty close to the actual `Date`
mockDate.name = 'Date';
mockDate.prototype = Date.prototype;
// Mocking static functions as well
mockDate.now = jest.fn(Date.now);
mockDate.parse = jest.fn(Date.parse);
mockDate.UTC = jest.fn(Date.UTC);
// `mockDate.mockReturnValue` doesn't seem to work when `Date` is called as a constructor
// This is a little hack that also has the benefit of checking that we return an actual `Date`
mockDate.mockReturnDate = (date) => {
if (!(date instanceof Date)) {
throw new TypeError('Value provided to mockDate is not a Date');
}
return mockDate.mockImplementation(() => date);
};
// Replaces `Date` by the mock
global.Date = mockDate; I have no idea if this is the recommended way of mocking |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days. |
This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Hello,
I have some test where I need to control what
new Date
returns, so I went through the way of spy on global.date. However, it is not honouring the implementation very well, and the behavior I'm getting is quite strange.If I go with this simple implementation:
What I get as output is just an empty object instead of the symbol I'm passing into it.
For some reason, changing the implementation to something like this, makes it work (in some situations)
Then, it returns
{date: symbol('whatever')}
, which I can use to check (although is a bit weird).Things start to get funny when such value is passed to another mock function, something equally simple:
The code I'm testing calls this mock function like this:
So later I have to do
expect(parse.makeDate).toHaveBeenCalledWith({ date: startDate });
.Is there any other way to have a more simplistic implementation of this?
The text was updated successfully, but these errors were encountered: