Search code examples
reactjsreduxreact-reduxjestjsreact-test-renderer

snapshot testing with react-test-renderer


I'm trying to create a test case for App component which has the following providers.

  • Store Provider
  • Translation provider
  • redux-persist
  • Router

I'm trying to render the app which certain state, to ensure the same app is rendered. The test case passes, but I get the following as snapshot.

// Jest Snapshot v1, [link to jest docs]

exports[`This should Render App 1`] = `null`;

exports[`This should Render App 2`] = `null`;

My test case is like this,

const component = renderer.create(
         <App store={store} persistor={persistor} />,
  );
  let tree = component.toJSON();
  expect(tree).toMatchSnapshot();

tree is not null and has the rendered component. But I do not see this being written in snapshot.

npm package versions

"jest": "^22.4.4",
"react-test-renderer": "^16.4.0",

Jest doc


Solution

  • I was able to fix the issue by using .toTree() method available in react-test-renderer instead of toJSON().

    toMatchSnapshot() of jest seems to accept only ReactTestRendererTree as apposed to ReactTestRendererJSON.