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

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

Closed
chetmurphy opened this issue Mar 24, 2019 · 3 comments
Closed

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

chetmurphy opened this issue Mar 24, 2019 · 3 comments

Comments

@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

lgandecki commented Mar 25, 2019

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
Copy link
Author

chetmurphy commented Mar 25, 2019 via email

@eric-burel
Copy link

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.

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