shallowWrapper is empty, testing snapshot Jest/Enzyme?

2k Views Asked by At

I don't know why i get empty shallowWrapper object when i run snapshot test, i am using Jest and Enzyme:

What i get in App.test.js.snap file:

// Jest Snapshot v1, "goo.gl/fbAQLP"
exports[`renders App component 1`] = `ShallowWrapper {}`;

my package.json file:

{
  "name": "test",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@fortawesome/fontawesome-svg-core": "^1.2.30",
    "@fortawesome/free-solid-svg-icons": "^5.14.0",
    "@fortawesome/react-fontawesome": "^0.1.11",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "bootstrap": "^4.5.2",
    "install": "^0.13.0",
    "npm": "^6.14.8",
    "react": "^16.13.1",
    "react-bootstrap": "^1.3.0",
    "react-dom": "^16.13.1",
    "react-router-dom": "^5.2.0",
    "react-scripts": "^4.0.0-next.98"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "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": "^3.11.0",
    "enzyme-adapter-react-16": "^1.15.4",
    "jest": "^26.4.2"
  }
}

App.test.js file

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


it('renders App component', () => {
  expect(shallow(<App />)).toMatchSnapshot();
})

setupTests.js file:

import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ 
    adapter: new Adapter()
 });

Why i get that empty shallowWrapper object, I suspected the version of jest after some search, but even downgrading didn't solve it.

2

There are 2 best solutions below

2
Kanishk Anand On BEST ANSWER

I ran into similar problem and playing around with jest help me got through it. I suggest you can clear the jest cache and then update the snapshots using jest --clearCache && jest -u and then run your test.

Also for your snapshot, I'd recommend you to use enzyme-to-json serialiser (https://www.npmjs.com/package/enzyme-to-json) and modify your test to:

import { shallow } from 'enzyme';
import React from 'react';
import App from './App';
import { shallowToJson } from 'enzyme-to-json';

it('renders App component', () => {
  const app = shallow(<App />);
  expect(shallowToJson(app)).toMatchSnapshot();
})
0
kate09 On

The shallow( ) did returned the output. Shallow works as a wrapper instance with the rendered output. You could use shallow( ) method with debug( ) to see what prints out. If you want to see the rendered output,you could use shallow().html( ) inside the expect( ) method to test again.