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

It looks like you called mount() without a global document being loaded. #341

Closed
svnm opened this issue Apr 22, 2016 · 20 comments
Closed

It looks like you called mount() without a global document being loaded. #341

svnm opened this issue Apr 22, 2016 · 20 comments

Comments

@svnm
Copy link

svnm commented Apr 22, 2016

I get the following error trying to call mount to test refs on my component.

import expect from 'expect'
import test from 'tape'
import React from 'react'
import { shallow, mount } from 'enzyme'
import Visit from '../../components/Visit'

test('Visit component', (assert) => {

  const component = shallow(<Visit visited={test} />)
  const wrapper = mount(<Visit />)

  assert.equal(
    component.find('span').text(), '', 'the visit component has no text'
  )

  assert.pass(
    expect(wrapper.ref('visit').prop('visited')).to.equal(false)
  )
  assert.end()
});
@svnm
Copy link
Author

svnm commented Apr 22, 2016

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()
});

@svnm svnm closed this as completed Apr 22, 2016
@muneermuhammed
Copy link

+1

@wilfredjonathanjames
Copy link

The same, without binding jsdom in every test file: https://github.com/rstacruz/jsdom-global#mocha

@abdennour
Copy link

abdennour commented Nov 26, 2016

npm install --save-dev --save-exact jsdom jsdom-global

Then :

import 'jsdom-global/register'; //at the top of file , even  , before importing react

Without need of adding code in beforeEach and afterEach.

@JoeCostanzo
Copy link

@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.

@stereodenis
Copy link

@StevenIseki @abdennour
what about react-native? I got this issue
when I try the solutions above — I got console.error about unpermitted properties

@wilomgfx
Copy link

@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

@makemek
Copy link

makemek commented Jun 9, 2017

@abdennour
You can also add -r jsdom-global/register as a mocha flag so that you don't have to import it every time in the code.
For example, in package.json

"test": "mocha --compilers js:babel-core/register -r jsdom-global/register"

@chloerice
Copy link

chloerice commented Nov 22, 2017

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 jsdom baked in and properly set up.

alex028502 pushed a commit to alex028502/sunset-clock that referenced this issue Nov 30, 2017
(except for the dom ones that are necessary)

enzymejs/enzyme#341
@flavio-dev
Copy link

@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:

 "enzyme": "^3.3.0",
 "enzyme-adapter-react-16": "^1.1.1",
 "jest": "22.1.4",
 "react-addons-test-utils": "^15.6.2",
 "react-dom": "^16.2.0",
 "react-native-mock": "^0.3.1",
 "react-test-renderer": "16.2.0"

and I have obv in my dependencies:

 "react": "^16.2.0",
 "react-native": "0.52.0",

my test

import React from 'react';
import { mount } from 'enzyme'

...

  it('renders correctly a list using Enzyme', async () => {
    const wrapper = mount(
      <UserView
         blabla={...}
      />
    );
});

Any idea what's missing?

I do have these two warnings:

warning " > [email protected]" has incorrect peer dependency "react-dom@^15.4.2".
warning "react-native-mock > [email protected]" has incorrect peer dependency "react@^15.6.2".

when installing the packages.

@chloerice
Copy link

@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 react-addons-test-utils is deprecated as of react 15.5.0.

@flavio-dev
Copy link

flavio-dev commented Jan 30, 2018

thank you for your response. I need that async keyword as I am attempting to test a fully rendered component that has a fetch at componentDidMount() lifecycle (setting a state that updates the view). Hence the mount() over shallow(), and the async as there will be an await late in the test.

I hope this is correct in terms of approach.

My react version in dependencies is 16.2.0. I didn't know about react-addons-test-utils being deprecated so thank you for this. Will try to fix the warnings somehow and see.

@chloerice
Copy link

@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).

@flavio-dev
Copy link

it does work for my current tests using renderer tho:

import * as dependency from '../fetch';

...

pp = Promise.resolve(JSON.stringify({users: [...]}))
dependency.default = jest.fn(() => pp)

it('renders correctly the users', async () => {
    const tree = renderer.create(
      <UsersView
        fetchUsersUrl={'test'}
        ...
      />
    );

    await pp;

    expect(dependency.default).toBeCalledWith('test');
    expect(tree.toJSON()).toMatchSnapshot();
  });

@flavio-dev
Copy link

reported my issue with full details here: #1501

@fabb
Copy link

fabb commented Jan 18, 2019

Nowadays, just edit your jest.config.js:

testEnvironment: 'jsdom',

@panmona
Copy link

panmona commented Apr 3, 2019

If you use jest you can use --env=jsdom

@jonasantonelli
Copy link

jonasantonelli commented Jun 10, 2019

You should put a comment at the top of your file

/**
 * @jest-environment jsdom
 */

Probably it will work!

@LucasNeevs
Copy link

npm install --save-dev --save-exact jsdom jsdom-global

Then :

import 'jsdom-global/register'; //at the top of file , even  , before importing react

Without need of adding code in beforeEach and afterEach.

Solve my problem with success! Thnx bro.

@shonmorgun
Copy link

Add this configuration property to your Jest config file jest.config.js

testEnvironment: 'jsdom'

NOTE: no need to install jsdom and json-global since Jest comes with jsdom

se3000 pushed a commit to smartcontractkit/chainlink that referenced this issue Oct 6, 2021
HenryNguyen5 pushed a commit to smartcontractkit/operator-ui that referenced this issue Sep 2, 2022
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