-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
It looks like you called mount()
without a global document being loaded.
#341
Comments
It looks like I just needed to add jsdom for mount as mentioned is this issue and in the docs Something like this: import expect from 'expect'
import test from 'tape'
import React from 'react'
import { shallow, mount } from 'enzyme'
import Visit from '../../components/Visit'
import jsdom from 'jsdom'
const doc = jsdom.jsdom('<!doctype html><html><body></body></html>')
global.document = doc
global.window = doc.defaultView
test('Visit component', (assert) => {
const testVisit = () => {
console.log('just visiting...')
}
const component = shallow(<Visit />)
const wrapper = mount(<Visit visited={testVisit} />)
assert.equal(
component.find('span').text(), '', 'the visit component has no text'
)
assert.pass(
expect(component.find('span').text(), '')
)
assert.equal(
wrapper.props().visited, testVisit, 'the visit component has a visited prop'
)
assert.end()
}); |
+1 |
The same, without binding jsdom in every test file: https://github.com/rstacruz/jsdom-global#mocha |
Then : import 'jsdom-global/register'; //at the top of file , even , before importing react Without need of adding code in |
@abdennour 's fix was right for me, as other ways wanted global navigator defined/mocked, on top of also spoofing document and window with jsdom, etc. This one's easier. |
@StevenIseki @abdennour |
@stereodenis as i understand by the docs you can't use mount with react-native as it depends on "browser-like environment" using js-dom. We have to use shallow React native source : https://github.com/airbnb/enzyme/blob/master/docs/guides/react-native.md Mount API : https://github.com/airbnb/enzyme/blob/master/docs/api/mount.md |
@abdennour
|
For future readers with similar troubleshooting, if you're using enzyme to test React and need to test Full DOM Rendering, use Jest to run your tests and you'll be golden as it comes with |
(except for the dom ones that are necessary) enzymejs/enzyme#341
@chloerice I am having this error as trying to test a react native component using Enzyme and Jest. I installed all the bits as devDependencies:
and I have obv in my dependencies:
my test
Any idea what's missing? I do have these two warnings:
when installing the packages. |
@flavio-dev try removing the async keyword. And definitely fix those dependencies (highly recommend upgrading everything to react 16 if you're able to). Also |
thank you for your response. I need that I hope this is correct in terms of approach. My react version in |
@flavio-dev be sure to update all other react-related dependecies to 16^ as well (react-dom etc). Even if you plan to test the async method called in the componentDidMount, putting an async at the front of the test's callback will not do what it sounds like you mean it to (and it's not necessary). |
it does work for my current tests using
|
reported my issue with full details here: #1501 |
Nowadays, just edit your
|
If you use jest you can use |
You should put a comment at the top of your file
Probably it will work! |
Solve my problem with success! Thnx bro. |
Add this configuration property to your Jest config file
NOTE: no need to install |
Signed-off-by: Steve Ellis <email@steveell.is>
Signed-off-by: Steve Ellis <email@steveell.is>
I get the following error trying to call
mount
to test refs on my component.The text was updated successfully, but these errors were encountered: