Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React jest test with enzyme error

Trying to test my react-native app with jest and enzyme as follows.

import 'react-native';
import React from 'react';
import renderer from 'react-test-renderer';
import { shallow } from 'enzyme';

import { SignUp } from '../../src/pages';

describe('Testing SignUp component', () => {
  it('renders as expected', () => {
    const wrapper = shallow(
        <SignUp />
     );
    expect(wrapper).toMatchSnapshot();
   });
});

on running npm test I get error Cannot find module 'enzyme/build/ShallowTraversal' from 'shallow.js'

test suite failed to run.

Please note that test done minus enzyme runs properly

test('SignUp Page renders correctly', () => {
    const tree = renderer.create(<SignUp />).toJSON();
    expect(tree).toMatchSnapshot();
});

What could be wrong?

like image 622
Isaac Sekamatte Avatar asked Dec 18 '17 15:12

Isaac Sekamatte


1 Answers

Just solved the problem by first installing enzyme-to-json; npm install --save-dev enzyme-to-json

imported toJson inside my test_file.js

import 'react-native';
import React from 'react';
import renderer from 'react-test-renderer';
import { shallow } from 'enzyme';
import toJson from 'enzyme-to-json'; //added this line

changed the test to this

describe('Testing SignUp component', () => {
  it('renders as expected', () => {
    const wrapper = shallow(
      <SignUp />
    );
   expect(toJson(wrapper)).toMatchSnapshot(); //edited this line
 });
});

changed snapShotSerializers in package.json

"preset": "react-native",
"collectCoverage": true,
"collectCoverageFrom": [
  "**/src/**.{js,jsx}"
],
"snapshotSerializers": ["enzyme-to-json/serializer"] //added this line

Now my tests are running correctly.

like image 164
Isaac Sekamatte Avatar answered Oct 20 '22 07:10

Isaac Sekamatte