Enzyme: Uncaught TypeError: inherits is not a function

Created on 13 Nov 2017  路  3Comments  路  Source: enzymejs/enzyme

I'm tasked with reworking the unit tests of our rather large code base, and I'd love to use Enzyme for this, but I'm having problems running it. Here are the details:

After adding the dependencies (yarn add enzyme enzyme-adapter-react-16), configuring the adapter like in the docs, and running our tests I get the following error (instead of the tests' output):

Uncaught TypeError: inherits is not a function at webpack:///~/cheerio/~/parse5/lib/extensions/position_tracking/preprocessor_mixin.js

So enzyme depends on cheerio, which in turn depends on parse5. In parse5 class inheritance is established by calling require('util').inherit(), but this function is undefined as the error states.

I've tried many things to make this work, but so far the only thing that actually works is manually adding the inherit function to every file in node_modules/cheerio/node_modules/parse5/lib where it is needed.

Other workarounds tried:

  • add util as a project wide dependency (i.e. yarn add util)
  • yarn add util with additional resolve.alias in webpack config
  • installing util in node_modules/cheerio/node_modules/parse5
  • delete node_modules, clear npm cache, re-install everything

But anything I do, as soon as I configure the Adapter I get the TypeError mentioned.

I'm not even sure if this is considered an enzyme issue, but everything apart from enzyme works on our end.

Other stuff I'm using that might be relevant: Webpack 2.2.1, Karma, HeadlessChrome, Windows 10, node 6.11.0, ES6 with babel.

invalid

Most helpful comment

I'm having the same issue. After investigation, I found there's a webpack alias (util: "src/...") in my project.

fix the incorrect alias may help to resolve this issue.

All 3 comments

cheerio browserifies fine ( https://wzrd.in/standalone/cheerio@^1.0.0-0 ) so my suspicion is that your webpack settings might be causing issues.

As for util, that's a node core module, so you definitely don't want it installed from npm.

I'm having the same issue. After investigation, I found there's a webpack alias (util: "src/...") in my project.

fix the incorrect alias may help to resolve this issue.

Closing; happy to reopen if needed.

Was this page helpful?
0 / 5 - 0 ratings