Skip to content
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

global.Date not mocked when window.Date exists #69

Open
jfstephe opened this issue Feb 29, 2024 · 3 comments
Open

global.Date not mocked when window.Date exists #69

jfstephe opened this issue Feb 29, 2024 · 3 comments

Comments

@jfstephe
Copy link

Hi,

First off, thanks for this. It helped us be able to test some code with date_fns that would have been a nightmare otherwise.

The problem:

If you have UI tests running in node then you have a window object and a global. The code at:

timezone-mock/index.js

Lines 265 to 272 in 780f8c2

function register(new_timezone, glob) {
if (!glob) {
if (typeof window !== 'undefined') {
glob = window;
} else {
glob = global;
}
}

...assumes one or the other but not both.

As a workaround I'm doing:

timezoneMock.register(timezone);
global.Date = window.Date;

/// ...tests

timezoneMock.unregister();
global.Date = window.Date;
@Jimbly
Copy link
Owner

Jimbly commented Feb 29, 2024

In your setup, do both window and global have a Date member, or only global?

If not both, probably an easy fix to check typeof window !== 'undefined' && window.Date, otherwise, if both, probably changing the flow to something like this would work (you could try hacking it up locally and report back and/or send a PR if it does):

function registerInternal(glob) {
  if (glob.Date !== MockDate) {
    _Date = glob.Date;
    exports._Date = glob.Date;
  }
  glob.Date = MockDate;
}

function register(new_timezone, glob) {
  timezone = new_timezone || 'US/Pacific';
  if (glob) {
    registerInternal(glob);
  } else {
    if (typeof window !== 'undefined') {
      registerInternal(window);
    }
    if (typeof global !== 'undefined') {
      registerInternal(global);
    }
  }
  if (!orig_object_toString) {
    orig_object_toString = Object.prototype.toString;
    Object.prototype.toString = mockDateObjectToString;
  }
}

@jfstephe
Copy link
Author

jfstephe commented Mar 1, 2024

Hi, thanks for the quick response!

Yes we have both Global (from node) and Window (from the UI test lib).

Strangely I've re-run this without my workaround and it's working, but this definitely was an issue and I think it makes sense that an issue is there. It could be a race condition somewhere in my code, not sure.

I did manage to run the code you suggested above and that continued to work. The unregister would need some tweaking too I think.

I'm not in a position to easily do a PR I'm afraid due to company restrictions. This isn't hurting me ATM and there's a workaround so maybe for vNext? At least it's recorded for others :-)

@Jimbly
Copy link
Owner

Jimbly commented Mar 1, 2024

Yes we have both Global (from node) and Window (from the UI test lib).

I was wondering if the fake window your UI test lib is adding has a Date member (wouldn't be too surprising if it doesn't, since it's not needed for mocking testing UI stuff, but it might also put it there just for completeness...)? Could use a slightly different fix if it does or doesn't, though I guess register could be made to simply replace whichever Date it finds on either =).

Anyway, will leave this here, if someone else runs into a similar issue in the future they can try the fix and hopefully send a PR if it works =).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants