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

Never rejects if Date.now is mocked in the test #12

Closed
valignatev opened this issue Nov 14, 2018 · 5 comments
Closed

Never rejects if Date.now is mocked in the test #12

valignatev opened this issue Nov 14, 2018 · 5 comments

Comments

@valignatev
Copy link
Contributor

Hi there, thanks for a nice and convenient package!

This is mostly the same case as #4, but Date.now is mocked instead of setTimeout. And if the Date.now is mocked, this condition: https://github.com/TheBrainFamily/wait-for-expect/blob/master/src/index.ts#L21 will be either always true, or always false. So tests with mocked Date.now will either fail without retrying, or fail with temeout error.

The solution for this is always to use the real Date.now the same way we're doing with setTimeout. I'll provide a PR for this where we can continue the discussion.

@valignatev
Copy link
Contributor Author

Bonus: example of such mock in the real world: https://github.com/facebook/jest/blob/6d3da9be06350bfa7414b609807ebd0cbdf0c416/examples/snapshot/__tests__/clock.react.test.js#L11

valignatev added a commit to valignatev/wait-for-expect that referenced this issue Nov 15, 2018
lgandecki pushed a commit that referenced this issue Nov 16, 2018
* (#12) use real Date.now to avoid using potentially mocked one

* (#12) make tsc happy with Date on Window interface and Date jest mock
@lgandecki
Copy link
Member

Merged and released, thank you so much :-)

@chetmurphy
Copy link

I am using typescript with react-hooks-testing-library that uses this library. To get tests to run, I had to mock Date in my tests

declare global {
  interface Window { Date: any; }
}
window.Date = Date;

And comment out the code in wait-for-expect starting at line 16:

var _ref = typeof window !== "undefined" ? window : global,
   setTimeout = _ref.setTimeout,
   now = _ref.Date.now;

Is this a bug?

@lgandecki
Copy link
Member

Could you please make a small reproduction that shows the problem?

@chetmurphy
Copy link

chetmurphy commented Mar 25, 2019 via email

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

3 participants