Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest 24: enzyme shallow rendered component snapshot is empty #7802

Closed
karanjthakkar opened this issue Feb 5, 2019 · 13 comments
Closed

Jest 24: enzyme shallow rendered component snapshot is empty #7802

karanjthakkar opened this issue Feb 5, 2019 · 13 comments
Labels

Comments

@karanjthakkar
Copy link

🐛 Bug Report

Until Jest 23, shallow rendering any component, generated a detailed snapshot (See: https://repl.it/@karanjthakkar/EnzymeShallowWrapperJest23). Due to the changes done in #7448, this use case is now broken and returns an empty snapshot, without even the props(See: https://repl.it/@karanjthakkar/EnzymeShallowWrapperJest24).

To Reproduce

Compare results between v24 and v23 of pretty-format for a shallow component render.

Expected behavior

From the discussion in #7443, I guess that non enumerable symbolic properties were omitted from the snapshot to reduce the snapshot size and remove unimportant information. However in case of enzyme, all the properties in the shallow-ly wrapped component instance are non-enumerable props: https://github.com/airbnb/enzyme/blob/bcb6fa9e795aefeaab6db608b526a428f45c4b49/packages/enzyme/src/ShallowWrapper.js#L268 -> https://github.com/airbnb/enzyme/blob/bcb6fa9e795aefeaab6db608b526a428f45c4b49/packages/enzyme/src/Utils.js#L273 As such, I'm unsure if you would like to consider this as a bug in Jest or something that Enzyme should be fixing.

Link to repl or repo (highly encouraged)

Working with Jest 23: https://repl.it/@karanjthakkar/EnzymeShallowWrapperJest23
Broken with Jest 24: https://repl.it/@karanjthakkar/EnzymeShallowWrapperJest24

Run npx envinfo --preset jest

Paste the results here:

npx: installed 1 in 4.08s

  System:
    OS: macOS High Sierra 10.13.6
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
  Binaries:
    Node: 10.13.0 - ~/.nvm/versions/node/v10.13.0/bin/node
    npm: 6.4.1 - ~/.nvm/versions/node/v10.13.0/bin/npm
@karanjthakkar
Copy link
Author

Also found a similar issue in the wild which someone "fixed" by reverting to Jest 23: https://stackoverflow.com/questions/54419342/jest-enzyme-shallowwrapper-is-empty-when-creating-snapshot

@thymikee
Copy link
Collaborator

thymikee commented Feb 5, 2019

These are private methods of Enzyme, why would you snapshot them? If you want to snapshot actual component structure from shallow wrapper, you'll need to add a proper serializer like https://github.com/adriantoine/enzyme-to-json

@karanjthakkar
Copy link
Author

@thymikee I agree the private methods are unnecessary fluff, but I would expect atleast component name and its props to be a part of the snapshot. I'll try that custom serializer and report back if it resolves the issue. 🙂

@karanjthakkar
Copy link
Author

I've managed to resolve my issue by using the serializer you mentioned. Thanks! 💯Do you think it would be useful to mention somewhere (either in the docs/the changelog/the blogpost) that this previously "accidentally" supported functionality won't work for folks anymore? I know its marked as a breaking change but tbh the impact of that might not be easy to understand for people.

@thymikee
Copy link
Collaborator

thymikee commented Feb 5, 2019

I think we have some mentions to enzyme in the docs so feel free to add a note about serializer then :)

@thymikee thymikee closed this as completed Feb 5, 2019
@SimenB SimenB added Wontfix and removed 🐛 Bug labels Feb 5, 2019
@bengrunfeld
Copy link

I'm still having this issue, despite using enzyme-to-json.

Component:

const Footer = () => (
  <div className='footer'>
    <h2>Footer</h2>
  </div>
)

is tested with this code:

test('matches the snapshot', () => {
    const tree = mount(<Footer />)
    expect(tree).toMatchSnapshot()
})

but generates an empty ReactWrapper in the snapshot:

exports[`<Footer /> matches the snapshot 1`] = `ReactWrapper {}`;

Here is my package.json, where I've set "snapshotSerializers": ["enzyme-to-json/serializer"]

{
  "name": "bengrunfeld.com",
  "version": "1.0.0",
  "description": "Personal blog website",
  "main": "index.js",
  "scripts": {
    "test": "jest",
    "updateSnapshot": "jest -u",
    "coverage": "jest --coverage",
    "buildDev": "rm -rf dist && webpack --mode development --config webpack.server.config.js && webpack --mode development --config webpack.dev.config.js",
    "buildProd": "rm -rf dist && webpack --mode production --config webpack.server.config.js && webpack --mode production --config webpack.prod.config.js",
    "start": "node ./dist/server.js"
  },
  "repository": "https://github.com/bengrunfeld/bengrunfeld",
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bootstrap": "^4.3.1",
    "express": "^4.16.4",
    "jquery": "^3.3.1",
    "popper.js": "^1.14.7",
    "prop-types": "^15.7.2",
    "react": "next",
    "react-dom": "next",
    "react-router-dom": "^5.0.0",
    "styled-components": "^4.1.3"
  },
  "engines": {
    "node": ">=4.3.2"
  },
  "jest": {
    "setupFiles": [
      "./src/test/setup.js"
    ],
    "snapshotSerializers": [
      "enzyme-to-json/serializer"
    ],
    "moduleFileExtensions": [
      "js",
      "jsx"
    ],
    "moduleDirectories": [
      "node_modules",
      "bower_components",
      "shared"
    ],
    "moduleNameMapper": {
      "\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js",
      "\\.(gif|ttf|eot|svg)$": "<rootDir>/__mocks__/fileMock.js",
      "^react(.*)$": "<rootDir>/vendor/react-master$1",
      "^config$": "<rootDir>/configs/app-config.js"
    }
  },
  "devDependencies": {
    "@babel/core": "^7.3.4",
    "@babel/plugin-syntax-dynamic-import": "^7.2.0",
    "@babel/preset-env": "^7.3.4",
    "@babel/preset-react": "^7.0.0",
    "@babel/preset-typescript": "^7.3.3",
    "babel-core": "^7.0.0-bridge.0",
    "babel-eslint": "^10.0.1",
    "babel-jest": "^24.5.0",
    "babel-loader": "^8.0.5",
    "babel-plugin-styled-components": "^1.10.0",
    "css-loader": "^2.1.1",
    "enzyme": "^3.9.0",
    "enzyme-adapter-react-16": "^1.11.2",
    "enzyme-to-json": "^3.3.5",
    "eslint": "^5.15.3",
    "eslint-loader": "^2.1.2",
    "eslint-plugin-jest": "^22.4.1",
    "eslint-plugin-react": "^7.12.4",
    "file-loader": "^3.0.1",
    "html-loader": "^0.5.5",
    "html-webpack-plugin": "^3.2.0",
    "jest": "^24.5.0",
    "jest-styled-components": "^6.3.1",
    "mini-css-extract-plugin": "^0.5.0",
    "optimize-css-assets-webpack-plugin": "^5.0.1",
    "react-test-renderer": "^16.8.4",
    "style-loader": "^0.23.1",
    "uglifyjs-webpack-plugin": "^2.1.2",
    "url-loader": "^1.1.2",
    "webpack": "^4.29.6",
    "webpack-cli": "^3.3.0",
    "webpack-dev-middleware": "^3.6.1",
    "webpack-hot-middleware": "^2.24.3",
    "webpack-node-externals": "^1.7.2"
  }
}

@pc-skhilko
Copy link

test('matches the snapshot', () => {
    const component = shallow(<Footer />);
    expect(component.debug()).toMatchSnapshot();
})

seems to be producing similar results to react-test-renderer

const tree = renderer.create(<Footer />).toJSON();
expect(tree).toMatchSnapshot();

Any concerns about using shallow().debug() for snapshot testing rather than introducing a new dependency on enzyme-to-json?

@flavluc
Copy link

flavluc commented Apr 30, 2019

any updates? shallow().debug() seems to do the work, but is it ok to use it?

cfcosta pushed a commit to cfcosta/kiali-ui that referenced this issue Jun 24, 2019
This is something that is not working after the upgrade to
`react-scripts`, and also has the benefit of generating more correct and
less brittle snapshots.

The other option is adding [a proper serializer][1] like
[enzyme-to-json][2], but this option also makes things easier to
understand without adding another dependency.

[1]: jestjs/jest#7802
[2]: https://github.com/adriantoine/enzyme-to-json
@ElmirMahmudov
Copy link

For snapshot testing do you guys recommend react-test-renderer's .toJSON() and/or .toTree() methods?

ryan-collingham added a commit to ryan-collingham/testplan that referenced this issue Sep 11, 2019
Fixes security vulnerabilities reported by npm audit and gives us latest
react-scripts features and updates. Update Snapshot tests to work with latest
Jest 24 required by react-scripts 3 by using the enzyme-to-json package (see
jestjs/jest#7802).
ryan-collingham added a commit to ryan-collingham/testplan that referenced this issue Sep 11, 2019
Fixes security vulnerabilities reported by npm audit and gives us latest
react-scripts features and updates. Update Snapshot tests to work with latest
Jest 24 required by react-scripts 3 by using the enzyme-to-json package (see
jestjs/jest#7802).
ryan-collingham added a commit to morganstanley/testplan that referenced this issue Sep 12, 2019
* Update to react-scripts 3

Fixes security vulnerabilities reported by npm audit and gives us latest
react-scripts features and updates. Update Snapshot tests to work with latest
Jest 24 required by react-scripts 3 by using the enzyme-to-json package (see
jestjs/jest#7802).

* Update eslint config

* Fix lint errors
@Askat08
Copy link

Askat08 commented Jan 11, 2020

Thank you

@Askat08
Copy link

Askat08 commented Apr 13, 2020

I'm still having this issue, despite using enzyme-to-json.

Component:

const Footer = () => (
  <div className='footer'>
    <h2>Footer</h2>
  </div>
)

is tested with this code:

test('matches the snapshot', () => {
    const tree = mount(<Footer />)
    expect(tree).toMatchSnapshot()
})

but generates an empty ReactWrapper in the snapshot:

exports[`<Footer /> matches the snapshot 1`] = `ReactWrapper {}`;

Here is my package.json, where I've set "snapshotSerializers": ["enzyme-to-json/serializer"]

{
  "name": "bengrunfeld.com",
  "version": "1.0.0",
  "description": "Personal blog website",
  "main": "index.js",
  "scripts": {
    "test": "jest",
    "updateSnapshot": "jest -u",
    "coverage": "jest --coverage",
    "buildDev": "rm -rf dist && webpack --mode development --config webpack.server.config.js && webpack --mode development --config webpack.dev.config.js",
    "buildProd": "rm -rf dist && webpack --mode production --config webpack.server.config.js && webpack --mode production --config webpack.prod.config.js",
    "start": "node ./dist/server.js"
  },
  "repository": "https://github.com/bengrunfeld/bengrunfeld",
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bootstrap": "^4.3.1",
    "express": "^4.16.4",
    "jquery": "^3.3.1",
    "popper.js": "^1.14.7",
    "prop-types": "^15.7.2",
    "react": "next",
    "react-dom": "next",
    "react-router-dom": "^5.0.0",
    "styled-components": "^4.1.3"
  },
  "engines": {
    "node": ">=4.3.2"
  },
  "jest": {
    "setupFiles": [
      "./src/test/setup.js"
    ],
    "snapshotSerializers": [
      "enzyme-to-json/serializer"
    ],
    "moduleFileExtensions": [
      "js",
      "jsx"
    ],
    "moduleDirectories": [
      "node_modules",
      "bower_components",
      "shared"
    ],
    "moduleNameMapper": {
      "\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js",
      "\\.(gif|ttf|eot|svg)$": "<rootDir>/__mocks__/fileMock.js",
      "^react(.*)$": "<rootDir>/vendor/react-master$1",
      "^config$": "<rootDir>/configs/app-config.js"
    }
  },
  "devDependencies": {
    "@babel/core": "^7.3.4",
    "@babel/plugin-syntax-dynamic-import": "^7.2.0",
    "@babel/preset-env": "^7.3.4",
    "@babel/preset-react": "^7.0.0",
    "@babel/preset-typescript": "^7.3.3",
    "babel-core": "^7.0.0-bridge.0",
    "babel-eslint": "^10.0.1",
    "babel-jest": "^24.5.0",
    "babel-loader": "^8.0.5",
    "babel-plugin-styled-components": "^1.10.0",
    "css-loader": "^2.1.1",
    "enzyme": "^3.9.0",
    "enzyme-adapter-react-16": "^1.11.2",
    "enzyme-to-json": "^3.3.5",
    "eslint": "^5.15.3",
    "eslint-loader": "^2.1.2",
    "eslint-plugin-jest": "^22.4.1",
    "eslint-plugin-react": "^7.12.4",
    "file-loader": "^3.0.1",
    "html-loader": "^0.5.5",
    "html-webpack-plugin": "^3.2.0",
    "jest": "^24.5.0",
    "jest-styled-components": "^6.3.1",
    "mini-css-extract-plugin": "^0.5.0",
    "optimize-css-assets-webpack-plugin": "^5.0.1",
    "react-test-renderer": "^16.8.4",
    "style-loader": "^0.23.1",
    "uglifyjs-webpack-plugin": "^2.1.2",
    "url-loader": "^1.1.2",
    "webpack": "^4.29.6",
    "webpack-cli": "^3.3.0",
    "webpack-dev-middleware": "^3.6.1",
    "webpack-hot-middleware": "^2.24.3",
    "webpack-node-externals": "^1.7.2"
  }
}

try to import import { shallowToJson } from "enzyme-to-json"; and wrap your tree with shallowToJson() like so expect(shallowToJson(tree)).toMatchSnapshot()

@kanishk-anand-oyo
Copy link

I am getting empty ShallowWrapper and ReactWrapper, even after using enzyme-to-json as serializer in my jest.config.js.
Here's my setup :

"preact" : 10.3.3,
"jest": 26.0.1,
"jest-enzyme": 7.1.2
"enzyme-to-json": 3.5.0
"enzyme-adapter-preact-pure" : 2.2.0

Part of jest.config.js :

moduleNameMapper: {
    '^react$': '<rootDir>/node_modules/preact/compat',
    '^react-dom$': '<rootDir>/node_modules/preact/compat',
    '^.+\\.(css|less|scss)$': 'identity-obj-proxy'
},
snapshotSerializers: ['enzyme-to-json/serializer']

And here's a sample example of the snapshot test :

import { shallowToJson } from 'enzyme-to-json';
import { shallow } from 'enzyme';
import Loader from './index';

describe('Loader Component', () => {
  it('should render correctly', () => {
    const component = shallow(<Loader />);
    console.log(component); // returns ShallowWrapper{}
    expect(shallowToJson(component)).toMatchSnapshot();
  });
});

Any leads will be helpful!

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

9 participants