TheBrainFamily / wait-for-expect

Wait for expectation to be true, useful for integration and end to end testing. Integral part of react-testing-library.
MIT License
293 stars 31 forks source link

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

Closed valignatev closed 5 years ago

valignatev commented 5 years ago

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 commented 5 years ago

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

lgandecki commented 5 years ago

Merged and released, thank you so much :-)

chetmurphy commented 5 years ago

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 commented 5 years ago

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

chetmurphy commented 5 years ago

Repro at https://github.com/chetmurphy/wait-for-expect-test

After installing with yarn run yarn test – will fail

● Test suite failed to run

TypeError: Cannot read property 'now' of undefined

  at Object.<anonymous> (node_modules/wait-for-expect/lib/index.js:16:21)
  at Object.<anonymous> (node_modules/dom-testing-library/dist/wait.js:10:45)
  at Object.<anonymous> (node_modules/dom-testing-library/dist/index.js:83:13)

Test Suites: 1 failed, 1 total

Comment out wait for expect section at wait-for-expect/lib/index.js:16

Rerun yarn test - should succeed.

--Chet


From: Łukasz Gandecki notifications@github.com Sent: Sunday, March 24, 2019 10:01:14 PM To: TheBrainFamily/wait-for-expect Cc: chetmurphy; Comment Subject: Re: [TheBrainFamily/wait-for-expect] Never rejects if Date.now is mocked in the test (#12)

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

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FTheBrainFamily%2Fwait-for-expect%2Fissues%2F12%23issuecomment-476055600&data=02%7C01%7C%7C0407f97894f8434a3d9208d6b0dee8a8%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636890868757911332&sdata=dLO7WiRjKTi52KjX5YFg9PPcB%2Fud5ODJTjyoxImiHIM%3D&reserved=0, or mute the threadhttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAJrbVk8_jyPVuept_h6qb6q1_62UhqFbks5vaFgagaJpZM4YdgZ4&data=02%7C01%7C%7C0407f97894f8434a3d9208d6b0dee8a8%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636890868757921344&sdata=p%2By%2FPXqR4GRqohGgv7ME8iPK35TIrgLzkI%2ByLmZIKiI%3D&reserved=0.