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

TypeError: TestEnvironment is not a constructor in combination with Enzyme #546

Closed
ehtb opened this issue Sep 2, 2016 · 6 comments · Fixed by jestjs/jest#1580
Closed

Comments

@ehtb
Copy link

ehtb commented Sep 2, 2016

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.

@gaearon
Copy link
Contributor

gaearon commented Sep 2, 2016

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

@gaearon
Copy link
Contributor

gaearon commented Sep 2, 2016

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

@ehtb
Copy link
Author

ehtb commented Sep 2, 2016

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);
});

@gaearon
Copy link
Contributor

gaearon commented Sep 2, 2016

@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] }

@gaearon
Copy link
Contributor

gaearon commented Sep 2, 2016

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.

@gaearon
Copy link
Contributor

gaearon commented Sep 2, 2016

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.

@lock lock bot locked and limited conversation to collaborators Jan 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants