enzyme: Cannot find module 'enzyme/build/ShallowTraversal' from 'shallow.js'

Hi,

Our test suite is now failing with the following error (Greenkeeper PR to upgrade from 2.9.1 to 3.0.0):

FAIL tests/unit/core/TestInstallAddon.js

  ● Test suite failed to run

    Cannot find module 'enzyme/build/ShallowTraversal' from 'shallow.js'

      
      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:192:17)
      at Object.<anonymous> (node_modules/enzyme-to-json/build/shallow.js:25:25)

Is it something you are aware of? I thought that was because we did not have any adapter configured (cf. migration guide) but configuring one gave the same error locally, it seems. I did not find anything related to this error message so I am not sure what to do yet.

Thanks.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 21 (5 by maintainers)

Most upvoted comments

Yeah I got an error when tryng to use jest-serializer-enzyme, but if I use enzyme-to-json/serializer instead:

"snapshotSerializers": [
      "enzyme-to-json/serializer"
    ],

it works

Other future googlers who tried going to the latest enzyme-to-json and were still having the issue. If you are using jest-serializer-enzyme like we were, then you’ll want to follow the advice in this thread and remove that dependency and just use the serializer that comes with enzyme-to-json.

Encountered this issue when upgrading an older project to Webpack 4.

For future Googlers, upgrading to the latest enzyme-to-json fixed my issue:

yarn add --dev enzyme-to-json

It does not work. I have the official React 16 and Enzyme 3 and I have no idea how and where to define Enzyme adapter. In the beginning of every unit test? In package.json? Is there ANY working example how to write a test using Enzyme, Jest and React 16?

import { App } from './index'
import Enzyme, { shallow } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import 'jest-enzyme'
import React from 'react'
Enzyme.configure({adapter: new Adapter()}) // ????

describe('<App/>', () => {
  it('renders without crashing', () => {
    shallow(<App/>)
  })
})

And again…

Cannot find module 'enzyme/build/ShallowTraversal' from 'shallow.js'

In my package.json

  "devDependencies": {
    "enzyme": "^3.0.0",
    "enzyme-adapter-react-16": "^1.0.0",
    "jest-enzyme": "^3.8.3",
    "raf": "^3.3.2",
    "react-test-renderer": "^16.0.0"
  }

Quick thoughts: I am pretty sure your test runner (eg. Jest) has an option to run a script before everything else (pre-hook or something like that). I’ll put the adapter code in it instead of Importing it in all test files.

@actraiser enzyme v3.0 is now released, so no need for the beta.