question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when using enzyme mount function

See original GitHub issue

Hello Guys,

I’m using the enzyme mount function for a test and when i run this test i get this error message: It looks like you called mount() without a global document being loaded

My test

import React from 'react';
import { shallow, mount } from 'enzyme';
import { Layout } from './index';
import ErrorSnackbar from './errorSnackbar';

  it('should render ErrorSnackbar component when error prop is defined', () => {
    const errorStub = { message: chance.sentence() };
    const clearErrorMock = jest.fn();
    const wrapper = mount(
      <Layout loading={chance.bool()} clearError={clearErrorMock} pageTitle={chance.word()} error={errorStub} />
    );

    expect(wrapper.find(ErrorSnackbar).exists()).toBe(true);
    expect(wrapper.find(ErrorSnackbar).props()).error.toBe(errorStub);
    expect(wrapper.find(ErrorSnackbar).props()).clearError.toBe(clearErrorMock);
  })

package.json dev dependencies

"devDependencies": {
    "chance": "^1.0.13",
    "enzyme": "^3.3.0",
    "enzyme-adapter-react-16": "^1.1.1",
    "enzyme-to-json": "^3.3.0",
    "react-test-renderer": "^16.2.0",
    "redux-mock-store": "^1.4.0"
  },

setupTests.js

import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

9reactions
leonardovillelacommented, Jan 21, 2018

If anyone wish to configure WebStorm to run Jest tests with CRA correct, i add the jest option --env=jsdom, in that way the tests runs fine.

WebStorm Jest Config

8reactions
gaearoncommented, Jan 21, 2018

The original idea was that jsdom is not really required and we want to nudge people to not depend on it in their tests (eg by using snapshot tests instead). Then they could remove the flag and enjoy faster test boot time.

Writing tests without depending on jsdom takes a bit of training to use correctly so we didn’t include those tests by default with the generated project.

I think we overestimated people’s curiosity. I thought people would try removing it, or at least read the relevant section in the user guide, but in practice everyone just leaves this flag on. It seems like most people don’t complain about test startup time either.

So maybe we should just flip the default to use jsdom.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mount does not work in enzyme with next js - Stack Overflow
I am writing a web app on next js(with typescript and styled-component). I started writing tests ...
Read more >
Introduction · Enzyme - GitHub Pages
If you're using React 16.8+ and .mount() , Enzyme will wrap apis including .simulate() , .setProps() , .setContext() , .invoke ...
Read more >
simulateError(error) · Enzyme - GitHub Pages
Simulate a component throwing an error as part of its rendering lifecycle. This is particularly useful in combination with React 16 error boundaries...
Read more >
Change Log · Enzyme - GitHub Pages
... shallow / mount : simulate : added functional component example to simulate doc (#2248) ... mount : setContext : use proper wrapper...
Read more >
Using enzyme to Test Components in React Native
Loading an emulated DOM with JSDOM ... To use enzyme's mount until a React Native adapter exists, an emulated DOM must be loaded....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found