DEV Community

Discussion on: Testing React Native Applications Part I: Jest

Collapse
 
gwmccull profile image
Garrett McCullough

Hi, great blog post. I love using the Jest's snapshot feature.

I've noticed that when I have multiple renderer.create calls in the same file, Jest will sometimes throw an error,

Invariant Violation: addComponentAsRefTo(…): Only a ReactOwner can have refs. You might be adding a ref to a component that was not created inside a component’s render method, or you have multiple copies of React loaded (details: fb.me/react-refs-must-have-owner).

If you see this also, you can try fixing it by moving your import renderer from 'react-test-renderer' into your test like this:

describe('MyComponent', () => {
  it('should render with required props', () => {
    const tree = renderer.create(
      <MyComponent {...defaultProps} />
    ).toJSON();
    expect(tree).toMatchSnapshot();
  });
});
Enter fullscreen mode Exit fullscreen mode

More info at my blog post:
mcculloughwebservices.com/2017/04/...