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." with Enzyme and React Native #1501

Closed
2 of 4 tasks
flavio-dev opened this issue Jan 30, 2018 · 5 comments
Labels
feature request Issues asking for stuff that would be semver-minor question

Comments

@flavio-dev
Copy link

flavio-dev commented Jan 30, 2018

Current behavior

Testing a mount() returns this error.

So I started testing using react-test-renderer (still do for snapshots).
Then wanted to test a fully rendered component post componentDidMount() (as there is a fetch in there that populates the view) and see how many item rendered. I therefore wanted to use mount().

I installed Enzyme, then got the adapter for my version of React, then got react-dom as it seems like it's needed as dependency, then got react-native-mockas it's recommended.
Then realised that react-native-mock wasn't working with my version of react-native, so I used the @jonny/react-native-mock.
I also installed then uninstalled react-addons-test-utils as it is now deprecated.

In my setupTests file which runs before the tests, I do:

import { configure } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import '@jonny/react-native-mock/mock'

configure({ adapter: new Adapter() })

Expected behavior

Would love to have that react native component mounted =)

Full example

Component:

import React, { Component } from 'react'
import {
  Text,
  View
} from 'react-native'

export default class TestTest extends Component {
  render() {
    return (
      <View>
        <Text>some text</Text>
      </View>
    )
  }
}

and tests:

  it('test mount using Enzyme', () => {
    const wrapper = mount(
      <TestTest />
    )
  });

  it('test shallow using Enzyme', () => {
    const wrapper = shallow(
      <TestTest />
    )

    expect(wrapper.find('Text')).toHaveLength(1);
  });

The mount returns: It looks like you called mount() without a global document being loaded.
The shallow works fine.

Your environment

"react": "16.2.0"
"react-native": "0.52.0"

"@jonny/react-native-mock": "^0.4.1",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"jest": "22.1.4",
"react-dom": "^16.2.0",
"react-test-renderer": "16.2.0"

API

  • shallow
  • mount
  • render

Version

library version
Enzyme ^3.3.0
React 16.2.0
react-native 0.52.0
jonny/react-native-mock ^0.4.1
jest 22.1.4
enzyme-adapter-react-16 1.1.1
react-dom ^16.2.0
react-test-renderer 16.2.0

Adapter

  • enzyme-adapter-react-16
@ljharb
Copy link
Member

ljharb commented Jan 31, 2018

You shouldn't be using react-test-renderer at all; i believe there's a package that lets you use .debug() from enzyme for snapshots.

Separately, until #1436 is finished, React Native will have a lot of quirks with enzyme.

@flavio-dev
Copy link
Author

actually according to https://medium.com/react-native-training/learning-to-test-react-native-with-jest-part-1-f782c4e30101 it's possible to use jest-serializer-enzyme it seems. But things is like you said: it's not working perfectly right now. But I can't be the only one not being able to mount() using Jest/Enzyme no? Any temp solutions?

@ljharb ljharb added question feature request Issues asking for stuff that would be semver-minor labels Jun 26, 2018
@ljharb
Copy link
Member

ljharb commented Jul 6, 2018

You'd need a react native adapter to use enzyme with React Native.

Closing this in favor of #1436.

@ljharb ljharb closed this as completed Jul 6, 2018
@quantuminformation
Copy link

I only get this error when trying to debug with webstorm

@GhayoorUlHaq
Copy link

Getting same in mac terminal and webstorm terminal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues asking for stuff that would be semver-minor question
Projects
None yet
Development

No branches or pull requests

4 participants