Enzyme: Cannot convert undefined or null to object.

Created on 26 Dec 2016  路  16Comments  路  Source: enzymejs/enzyme

Hi. I am using Jest with Enzyme. I have a component wrapped by few components(Provider, Intl, Relay). When i try to render the component with render function and then call .toMatchSnapshot it works perfect. But when i call mount instead of render it fails. The stack trace is:

TypeError: Cannot convert undefined or null to object

  at Object.<anonymous> (__tests__/common/Header/Header2.test.js:54:76)
  at handle (../node_modules/worker-farm/lib/child/index.js:41:8)
  at process.<anonymous> (../node_modules/worker-farm/lib/child/index.js:47:3)
  at emitTwo (../events.js:87:13)
  at process.emit (../events.js:172:7)
  at handleMessage (../internal/child_process.js:686:10)
  at Pipe.channel.onread (../internal/child_process.js:440:11)

The reason why i am calling mount that i need set props to the component.
Perhaps the problem is not in enzyme but in worker-farm lib. Anyway any help will be appreciated.

Most helpful comment

The error message is:

    TypeError: Cannot convert undefined or null to object

      at Object.<anonymous> (__tests__/container/list/AccountsList.test.js:105:89)
      at process._tickCallback (internal/process/next_tick.js:103:7)

BTW, I will file another issue to enzyme-to-json and link both of them.

All 16 comments

Hi! I have the same problem

Can you share the test code?

@ljharb

  const store = configureMockStore()({
    form: {},
    theme: {},
  });
  const Container = Relay.createContainer(HeaderContainer, {
    fragments: {
      viewer: () => Relay.QL`
        fragment on Viewer {
          ${HeaderContainer.getFragment('user')}
        }
      `,
    },
  });
  const mockViewer = {
    ...notification,
    userProfile: {
      photoFile: {},
      account: {},
      impersonator: {},
      sexDictionary: {},
    },
    notificationsQuantity: 1,
    messagesQuantity: 2,
    adminMessagesQuantity: 3,
  };

  const instance = mount(
    <Provider store={store}>
      <IntlProvider
       key={'ru'}
       locale={'ru'}
       messages={[]}>
        <RelayMockRenderer
          render={() => <Container viewer={mockViewer} />}
        />
      </IntlProvider>
    </Provider>
  );
  console.log(toJson(instance))
  expect(toJson(instance)).toMatchSnapshot();

Which line corresponds to __tests__/common/Header/Header2.test.js:54?

the last one expect(toJson(instance)).toMatchSnapshot();

That implies something wrong with the toMatchSnapshot call - is it not expected to take arguments? How is it being bootstrapped?

There is mock for files(svg, png). Everything else is just basic. As i said it works with render but doesn't with mount.

What's the implementation of IntlProvider look like? Where's worker-farm come in?

import { IntlProvider } from 'react-intl'; I don't know where worker-farm comes in. I didn't find in my project where it is being called. I guess it's called by some library.

Hi, maybe I have the same problem
and here is the stack trace

TypeError: Cannot convert undefined or null to object

      at Object.<anonymous> (src/scripts/components/__tests__/Panel.jest.js:32:134)
      at handle (node_modules/worker-farm/lib/child/index.js:41:8)
      at process.<anonymous> (node_modules/worker-farm/lib/child/index.js:47:3)
      at emitTwo (events.js:106:13)
      at process.emit (events.js:191:7)
      at process.nextTick (internal/child_process.js:752:12)
      at _combinedTickCallback (internal/process/next_tick.js:67:7)
      at process._tickCallback (internal/process/next_tick.js:98:9)

Might be a jest thing. What version of jest, and do you have any mocks set up?

Hi, same here. My jest version is 18.1.0.

The account list is not null or undefined The exception happens to the second line of the code:

        console.log(accountList);
        expect(toJSON(accountList)).toMatchSnapshot();

where does toJSON come from?

it's from https://www.npmjs.com/package/enzyme-to-json ..

BTW, ErrorPro's example also has toJSON....

The error message is:

    TypeError: Cannot convert undefined or null to object

      at Object.<anonymous> (__tests__/container/list/AccountsList.test.js:105:89)
      at process._tickCallback (internal/process/next_tick.js:103:7)

BTW, I will file another issue to enzyme-to-json and link both of them.

Looks like this is an issue with jest, not enzyme.

Was this page helpful?
0 / 5 - 0 ratings