jest: Node environment doesn't allow testing of libraries like Nightmare
I’m trying to write tests for a small scraper I built with Nightmare
. Because it uses Electron it’s not possible to test it with Jest. I think the issue has to do with the variables that are exposed globally in the environment. The tests do work with Mocha but it would be awesome if I could just simply run the tests in my local environment (as opposed to the Jest sandbox).
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 18 (7 by maintainers)
jest.useRealTimers()
We will make this a default soon as well.
On Sat, Aug 20, 2016 at 1:28 AM +0200, “Miguel Oller” <notifications@github.commailto:notifications@github.com> wrote:
So after trying to write my tests again today I came across another bug with using Nightmare. This time I took the time to figure out exactly what it was.
It looks like Jest mocks the global setTimeout which Nightmare uses internally to wait for elements to appear in the DOM or simply to wait a set time. I’ve updated the repo I linked to before to include this new bug.
Is there a way to disable the mocking of timers in Jest?
You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/facebook/jest/issues/1448#issuecomment-241159472, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AAA0KIMK29IKQk8yR9JUPj53cUcCVWW9ks5qhjv9gaJpZM4Jm5Hf.
We’ll make this the default and have an opt-out to the current behavior. It’s currently internal.
Jest resets the module registry before every test for isolation but people don’t enjoy using this feature in open source and prefer to use imports or top level requires.
Example:
but this requires people to write their requires inside of
it
calls instead of at the top level and they won’t be able to use imports. People do that though, so we’ll make it the default.It just happens that the stream library has a “lazy require”, ie it requires something inside of a function call. If you have a reference of a module from the first module registry and you call something on it and that requires something every time, when the module registry gets reset, it loses it’s reference and re-requires it. That way a module, during its lifetime, might use different instances of different modules and that gets messy and it is too magical, so we’ll kill it.
Sorry about the trouble. If you stick
"persistModuleRegistryBetweenSpecs": true
into your config and callexample().end().then(…)
in your test it will work.You can also return the promise from
it
without usingdone
at all.We’ll make the module registry persistent in the next major version of Jest, as people have trouble with it.