Enzyme: TypeError: simulateError() can only be called on class components with an instance

Created on 3 Jan 2019  路  7Comments  路  Source: enzymejs/enzyme

To repro this, run yarn test in my demo repo.

I have the following tests. The first passes, and the second crashes:

it('test works: simulates an error', () => {
  const wrapper = mount(
    <Provider store={store}>
      <ConnectedErrorBoundary>
        <InnerComponent />
      </ConnectedErrorBoundary>
    </Provider>
  );

  wrapper.find(InnerComponent).simulateError(new Error('Rendering failed.'));

  expect(enzymeToJson(wrapper.find(ErrorBoundary))).toMatchSnapshot();
});

const ReduxFrame = ({children}) => <Provider store={store}>{children}</Provider>;

it('test fails: simulates an error', () => {
  const wrapper = mount(
    <ReduxFrame>
      <ConnectedErrorBoundary>
        <InnerComponent />
      </ConnectedErrorBoundary>
    </ReduxFrame>
  );

  wrapper.find(InnerComponent).simulateError(new Error('Rendering failed.'));

  expect(enzymeToJson(wrapper.find(ErrorBoundary))).toMatchSnapshot();
});

The key difference: the passing test references Provider directly. The failing test has one level of indirection, but I don't see how that makes a functional difference. Am I doing something wrong here?

Thanks!

Your environment

Node v10.13.0

API

  • [ ] shallow
  • [x] mount
  • [ ] render

Version

| library | version
| ------------------- | -------
| enzyme | 3.8.0
| react | 16.7.0
| react-dom | 16.7.0
| react-test-renderer | 16.7.0
| adapter (below) | 1.7.1

Adapter

  • [x] enzyme-adapter-react-16
  • [ ] enzyme-adapter-react-16.3
  • [ ] enzyme-adapter-react-16.2
  • [ ] enzyme-adapter-react-16.1
  • [ ] enzyme-adapter-react-15
  • [ ] enzyme-adapter-react-15.4
  • [ ] enzyme-adapter-react-14
  • [ ] enzyme-adapter-react-13
  • [ ] enzyme-adapter-react-helper
  • [ ] others ( )
mount bug enzyme-adapter-utils patch

All 7 comments

@NickHeiner fyi when I run npm test or yarn test, it finds zero tests to run; npm test src/App.test.js reproduces the failure, however.

When I move creation of store and friends so that it's inside each test - since it's a bad practice to create stateful things outside of it or beforeEach, it still fails - and the same when I skip the first test. so that suggests it's not test pollution.

What's most confusing is that SFCs don't actually have an instance in React 16+, so the first test should be failing too.

ok, that leads me to realize that the issue is that the root node is a class in the first test, but not in the second test. This seems to be a bit of a bug in enzyme; let me dig further.

Got a fix; thanks for the excellent repro repo!

Thanks for the speedy response! And sorry for the churn about the test command not being precisely correct.

@NickHeiner fyi when I run npm test or yarn test, it finds zero tests to run; npm test src/App.test.js reproduces the failure, however.

Out of curiosity, I looked into this: when you run yarn test, Jest will find 0 tests changed since the past commit. At that point, you'd hit a for "run all tests", then Jest will run src/App.test.js.

I tried that; it didn't find any test files :-/

Hmm, that's odd. Well, whatever. :smile:

Was this page helpful?
0 / 5 - 0 ratings