Create-react-app: TypeError: TestEnvironment is not a constructor in combination with Enzyme

Created on 2 Sep 2016  Â·  6Comments  Â·  Source: facebook/create-react-app

With Enzyme installed, running tests:

"test": "react-scripts test --env=jsdom"

Throws the error:

TypeError: TestEnvironment is not a constructor
at runTest (node_modules/react-scripts/node_modules/jest/node_modules/jest-cli/build/runTest.js:30:15)

The tests run correctly when removing --env=jsdom.

bug underlying tools

Most helpful comment

We’re going to cut a patch fixing this today but for now, you can work around it by opening node_modules/react-scripts/node_modules/jest-config/build/normalize.js and commenting out lines 80 to 90.

All 6 comments

Can you give the exact test code that is failing please?

Interesting. At first I couldn’t reproduce this but after stopping and starting the watcher again I’m getting this even with an empty test file.

Console output:

FAIL  src/App.test.js
  â—Ź Test suite failed to run

    TypeError: TestEnvironment is not a function

      at runTest (node_modules/react-scripts/node_modules/jest/node_modules/jest-cli/build/runTest.js:30:15)

Test Summary
 › Ran all tests.
 › 1 test suite failed, 0 tests passed (0 total in 1 test suite, run time 0.585s)

cc @cpojer

Sure, you can easily recreate the error by creating a new (0.3.0) project:

  1. create-react-app issue546
  2. npm install --save-dev enzyme react-addons-test-utils
  3. npm test

This throws:

Test suite failed to run
TypeError: TestEnvironment is not a constructor

at runTest (node_modules/react-scripts/node_modules/jest/node_modules/jest-cli/build/runTest.js:30:15)

Even running an empty test is throwing this error, but for completeness:

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

it('renders welcome message', () => {
  const wrapper = shallow(<App />);
  const welcome = <h2>Welcome to React</h2>;
  expect(wrapper.contains(welcome)).toEqual(true);
});

@cpojer

Apparently TestEnvironment was resolved to jsdom itself instead of jest-environment-jsdom:

object
{ createVirtualConsole: [Function],
  getVirtualConsole: [Function],
  createCookieJar: [Function],
  nodeLocation: [Function],
  reconfigureWindow: [Function],
  debugMode: false,
  availableDocumentFeatures: [Getter/Setter],
  defaultDocumentFeatures: [Getter/Setter],
  applyDocumentFeatures: [Getter/Setter],
  jsdom: { [Function] jQueryify: [Function], env: [Function] },
  jQueryify: [Function],
  env: [Function],
  serializeDocument: [Function] }

I fixed this locally by commenting these lines out. In case of Create React App it is incorrect to resolve relative to rootDir because that’s not where Jest is. It should try require.resolve() first—at least for our use case.

We’re going to cut a patch fixing this today but for now, you can work around it by opening node_modules/react-scripts/node_modules/jest-config/build/normalize.js and commenting out lines 80 to 90.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alleroux picture alleroux  Â·  3Comments

alleroux picture alleroux  Â·  3Comments

fson picture fson  Â·  3Comments

Evan-GK picture Evan-GK  Â·  3Comments

onelson picture onelson  Â·  3Comments