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

Cannot convert undefined or null to object. #750

Closed
ErrorPro opened this issue Dec 26, 2016 · 16 comments
Closed

Cannot convert undefined or null to object. #750

ErrorPro opened this issue Dec 26, 2016 · 16 comments

Comments

@ErrorPro
Copy link

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.

@andrychernysh
Copy link

Hi! I have the same problem

@ljharb
Copy link
Member

ljharb commented Dec 26, 2016

Can you share the test code?

@ErrorPro
Copy link
Author

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

@ljharb
Copy link
Member

ljharb commented Dec 27, 2016

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

@ErrorPro
Copy link
Author

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

@ljharb
Copy link
Member

ljharb commented Dec 27, 2016

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

@ErrorPro
Copy link
Author

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

@ljharb
Copy link
Member

ljharb commented Dec 27, 2016

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

@ErrorPro
Copy link
Author

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.

@cettoana
Copy link

cettoana commented Jan 4, 2017

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)

@ljharb
Copy link
Member

ljharb commented Jan 4, 2017

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

@john-hu
Copy link

john-hu commented Feb 13, 2017

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

@ljharb
Copy link
Member

ljharb commented Feb 13, 2017

where does toJSON come from?

@john-hu
Copy link

john-hu commented Feb 13, 2017

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

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

@john-hu
Copy link

john-hu commented Feb 13, 2017

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.

@ljharb
Copy link
Member

ljharb commented Sep 26, 2017

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

@ljharb ljharb closed this as completed Sep 26, 2017
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

5 participants