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

TypeError: Cannot read property 'now' of undefined in 1.1.0 #17

Closed chetmurphy closed 5 years ago

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

Moving the discussion from #12 Repro at https://github.com/chetmurphy/wait-for-expect-test - you do some weird things there, the whole setup of enzyme is not needed when you use *-testing-library. To sum up, I removed setupFiles and testEnvironment from your package.json and it worked.

Screen Shot 2019-03-25 at 12 57 48 PM Screen Shot 2019-03-25 at 12 57 44 PM

I don't think this is a bug in the library, when you start mocking your globals, windows, etc, things might start breaking.

Also, your mocking of date in Block.test.tsx could not work - since the code is breaking on the import, before you even get to your "mock"

chetmurphy commented 5 years ago

Confirmed and thanks.


From: Łukasz Gandecki notifications@github.com Sent: Monday, March 25, 2019 2:00:18 AM To: TheBrainFamily/wait-for-expect Cc: chetmurphy; Author Subject: Re: [TheBrainFamily/wait-for-expect] TypeError: Cannot read property 'now' of undefined in 1.1.0 (#17)

Moving the discussion from #12https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FTheBrainFamily%2Fwait-for-expect%2Fissues%2F12&data=02%7C01%7C%7Cc7f4af74c0ea4bc9824008d6b1004e45%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636891012198143958&sdata=WCFWhH9dEGhBYuBckEaCbGuX%2FkzLp%2FtcF9grUJjpo%2BI%3D&reserved=0 Repro at https://github.com/chetmurphy/wait-for-expect-testhttps://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fchetmurphy%2Fwait-for-expect-test&data=02%7C01%7C%7Cc7f4af74c0ea4bc9824008d6b1004e45%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636891012198153969&sdata=16BTGNuxJkI86vEV%2BPV4m4L9g74a%2BLVgX9IO4mf%2BcMU%3D&reserved=0 - you do some weird things there, the hole setup of enzyme is not needed when you use *-testing-library. To sum up, I removed setupFiles and testEnvironment from your package.json and it worked.

[Screen Shot 2019-03-25 at 12 57 44 PM]https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fuser-images.githubusercontent.com%2F4002543%2F54906839-9d0bcf00-4efd-11e9-8401-5e81766fec14.png&data=02%7C01%7C%7Cc7f4af74c0ea4bc9824008d6b1004e45%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636891012198163967&sdata=hfFqYNczi2b%2Frnwz3xMZelgpztwmfsCHPVxWK59KHWE%3D&reserved=0 [Screen Shot 2019-03-25 at 12 34 21 PM]https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fuser-images.githubusercontent.com%2F4002543%2F54906841-9d0bcf00-4efd-11e9-8e08-31cd43e508df.png&data=02%7C01%7C%7Cc7f4af74c0ea4bc9824008d6b1004e45%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636891012198173978&sdata=sX6rRuRcuMOClU5wE7Qp9EyMi1%2BssNs0v9iJMWZS%2Fv0%3D&reserved=0

I don't think this is a bug in the library, when you start mocking your globals, windows, etc, things might start breaking.

Also, your mocking of date in Block.test.tsx could not work - since the code is breaking on the import, before you even get to your "mock"

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FTheBrainFamily%2Fwait-for-expect%2Fissues%2F17%23issuecomment-476107816&data=02%7C01%7C%7Cc7f4af74c0ea4bc9824008d6b1004e45%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636891012198183983&sdata=AloNAGzPA%2F%2F8Nog6nVnNHKPIdy1i%2BpB%2Bve%2BL8TwduoQ%3D&reserved=0, or mute the threadhttps://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAJrbVhJGKBwfpZwp7ShZmPzaD0lho5ayks5vaJAigaJpZM4cFq-m&data=02%7C01%7C%7Cc7f4af74c0ea4bc9824008d6b1004e45%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636891012198193994&sdata=jAnCuCOY%2BUXIoMepFgJehG%2FSjlFqJngk%2BQr2sO59AK4%3D&reserved=0.

eric-burel commented 5 years ago

Hi, for lost googlers that struggled like me, I needed to initialize JSDOM this way server side so window object is fully defined:

import jsdom from "jsdom-global";

const initComponentTest = () => {
    // init jsdom for SSR
    jsdom("", {
        runScripts: "outside-only"
    });

For the details this manual initialization of JSDOM is needed to test components with Enzyme server side in Vulcan (a Meteor + Mocha + React setup with SSR).

The runScripts option will load window.Date, window.Map and so on, that are not included in JSDOM as a default as stated in their doc.

As explained in #15 maybe it would nice to catch this error early on in wait-for-expect, because it's quite hard to debug and can easily happen with a bad setup/too much mocking.