1
votes

My test does not fail when making changes to App.js.

I'm using jest, enzyme, react.js, react-create-app, and react-scripts.

As I update the App.js component, I expect that the test should fail because it does not match the snapshot of App.js. I think it has to do with my _ snapshot _/App.test.js file... How do I fix this? Any ideas?

Here is my code:

src/components/App.js:

import React, { Component } from 'react';

class App extends Component {
  render() {
    return (
      <div>
        <h2>Gift Giver</h2>
      </div>
    );
  }
}
export default App;

src/components/App.test.js:

import React from 'react';
import { shallow } from 'enzyme';
import App from './App';

const app = shallow(<App />);

it('renders correctly', () => {
  expect(app).toMatchSnapshot();
});

src/components/__ snapshots __/App.test.js.snap


exports[`renders correctly 1`] = `ShallowWrapper {}`;

package.json:

{
  "name": "giftgiver",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "enzyme": "^3.10.0",
    "react": "^16.8.6",
    "react-bootstrap": "^1.0.0-beta.9",
    "react-dom": "^16.8.6",
    "react-scripts": "3.0.1",
    "react-test-render": "^1.1.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "enzyme-adapter-react-16": "^1.14.0"
  }
}

As I update the App.js component, I expect that the test should fail because it does not match the snapshot of App.js

1

1 Answers

0
votes

You have to use .debug() when rendering snapshots.

import React from 'react';
import { shallow } from 'enzyme';
import App from './App';

const app = shallow(<App />);

it('renders correctly', () => {
  expect(app.debug()).to.matchSnapshot();
});

As an aside, generally it's better to use mount instead of shallow unless there's a very good reason not to.