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.
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:
create-react-app issue546npm install --save-dev enzyme react-addons-test-utilsnpm testThis 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.
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.jsand commenting out lines 80 to 90.