enzymejs / enzyme

JavaScript Testing utilities for React
https://enzymejs.github.io/enzyme/
MIT License
19.95k stars 2.01k forks source link

Installing latest version of enzyme with cheerio@1.0.0-rc.6 dependency raises TypeError: parse5.parseFragment is not a function when calling render #2516

Closed el-ethan closed 3 years ago

el-ethan commented 3 years ago

Current behavior

When I call the render() function on a basic React component, it gives me the following errror:

TypeError: parse5.parseFragment is not a function

Here is an example test that fails with minimal code:

import { render } from 'enzyme';

function Foo() {
    return <div></div>;
}

describe('test', () => {
    it('shouldnt break', () => {
        render(<Foo />);
    });
});

which raises the following error:

 FAIL  src/helpers/GaGlobalSiteTag/test.js
  test
    ✕ shouldnt break (18ms)

  ● test › shouldnt break

    TypeError: parse5.parseFragment is not a function

       8 | describe('test', () => {
       9 |  it('shouldnt break', () => {
    > 10 |      render(<Foo />);
         |      ^
      11 |  });
      12 | });
      13 |

      at Object.<anonymous>.exports.parse (node_modules/enzyme/node_modules/cheerio/lib/parsers/parse5.js:19:14)
      at Object.<anonymous>.exports.parse (node_modules/enzyme/node_modules/cheerio/lib/parsers/parse5.js:18:14)
      at parse (node_modules/enzyme/node_modules/cheerio/lib/parse.js:21:9)
      at Function.load (node_modules/enzyme/node_modules/cheerio/lib/load.js:34:14)
      at loadCheerioRoot (node_modules/enzyme/src/Utils.js:366:18)
      at render (node_modules/enzyme/src/render.js:22:10)
      at Object.<anonymous> (src/helpers/GaGlobalSiteTag/test.js:10:3)

I believe the issue lies in cheerio@1.0.0-rc.6 because, in addition to the stacktrace pointing to cheerio, I have found that with cheerio@1.0.0-rc.5, and the same test code, my test passes without error. I discovered this because I have a branch with a package-lock.json that locks in cheerio@1.0.0-rc.5. If I remove node_modules and install with that package lock, then I do not get the error (even with enzyme@3.11.0). However, if I do npm install enzyme@latest, my version of enzyme does not change (since I had already been using the latest), but my cheerio gets updated to cheerio@1.0.0-rc.6, and I get the error.

Expected behavior

It should render the component without error.

Your environment

API

Version

library version
enzyme 3.11.0
react 16.14.0
react-dom react-dom@16.14.0
react-test-renderer react-test-renderer@16.14.0
adapter (below) enzyme-adapter-react-16

Adapter

Please let me know if I can provide any more info, or if you think I should report this issue elsewhere. Thank you!

ljharb commented 3 years ago

enzyme is pinned now to v1.0.0-rc.3 of cheerio; altho it hasn't been released yet. I'd suggest adding that as an explicit dev dep, using =.

el-ethan commented 3 years ago

@ljharb thank you so much for the quick response! I added "cheerio": "1.0.0-rc.3" to my devDependencies as suggested, and this resolved the issue. Thank you!

ljharb commented 3 years ago

This was fixed in https://github.com/enzymejs/enzyme/commit/cafdb2b86ed8865527cdd6ae31c42593d3728ceb and will be released in the next enzyme version.