我的create react应用程序中有以下Jest测试。我设置了一个环境文件,该文件在运行应用程序时可以正常运行:
REACT_APP_API_PATH="http://localhost:5000/"
REACT_APP_LOG=ON
我将此文件复制到同一目录中的.env.test,但是当我运行“ npm test”时,出现以下错误。我也尝试将文件复制到.env
console.error node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Uncaught [Error: Configuration file has not been set up.]
at reportException (/home/jc/projects/grip/grip-client/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/helpers/runtime-script-errors.js:66:24)
at invokeEventListeners (/home/jc/projects/grip/grip-client/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:209:9)
at HTMLUnknownElementImpl._dispatch (/home/jc/projects/grip/grip-client/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:119:9)
at HTMLUnknownElementImpl.dispatchEvent (/home/jc/projects/grip/grip-client/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:82:17)
at HTMLUnknownElementImpl.dispatchEvent (/home/jc/projects/grip/grip-client/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/nodes/HTMLElement-impl.js:30:27)
at HTMLUnknownElement.dispatchEvent (/home/jc/projects/grip/grip-client/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:157:21)
at Object.invokeGuardedCallbackDev (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:199:16)
at invokeGuardedCallback (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:256:31)
at replayUnitOfWork (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:17107:5)
at renderRoot (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:17979:13) Error: Configuration file has not been set up.
at new Error (/home/jc/projects/grip/grip-client/src/containers/App.js:126:19)
at constructClassInstance (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:12484:18)
at updateClassComponent (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:14255:5)
at beginWork (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:15082:16)
at performUnitOfWork (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:17820:12)
at workLoop (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:17860:24)
at HTMLUnknownElement.callCallback (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:149:14)
at invokeEventListeners (/home/jc/projects/grip/grip-client/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:193:27)
at HTMLUnknownElementImpl._dispatch (/home/jc/projects/grip/grip-client/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:119:9)
at HTMLUnknownElementImpl.dispatchEvent (/home/jc/projects/grip/grip-client/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:82:17)
at HTMLUnknownElementImpl.dispatchEvent (/home/jc/projects/grip/grip-client/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/nodes/HTMLElement-impl.js:30:27)
at HTMLUnknownElement.dispatchEvent (/home/jc/projects/grip/grip-client/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:157:21)
at Object.invokeGuardedCallbackDev (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:199:16)
at invokeGuardedCallback (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:256:31)
at replayUnitOfWork (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:17107:5)
at renderRoot (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:17979:13)
at performWorkOnRoot (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:18837:7)
at performWork (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:18749:7)
at performSyncWork (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:18723:3)
at requestWork (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:18592:5)
at scheduleWork (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:18401:5)
at scheduleRootUpdate (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:19069:3)
at updateContainerAtExpirationTime (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:19097:10)
at updateContainer (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:19154:10)
at ReactRoot.Object.<anonymous>.ReactRoot.render (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:19416:3)
at /home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:19556:14
at unbatchedUpdates (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:18952:10)
at legacyRenderSubtreeIntoContainer (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:19552:5)
at Object.render (/home/jc/projects/grip/grip-client/node_modules/react-dom/cjs/react-dom.development.js:19613:12)
at Object.render (/home/jc/projects/grip/grip-client/src/containers/__tests__/App.test.js:33:14)
at Object.asyncJestTest (/home/jc/projects/grip/grip-client/node_modules/jest-jasmine2/build/jasmine_async.js:108:37)
at resolve (/home/jc/projects/grip/grip-client/node_modules/jest-jasmine2/build/queue_runner.js:56:12)
at new Promise (<anonymous>)
at mapper (/home/jc/projects/grip/grip-client/node_modules/jest-jasmine2/build/queue_runner.j FAIL src/containers/__tests__/App.test.js (18.215s)
App
✕ renders without crashing - mount (489ms)
● App › renders without crashing - mount
Configuration file has not been set up.
124 |
125 | if (!process.env.REACT_APP_API_PATH) {
> 126 | const err = Error('Configuration file has not been set up.');
| ^
127 | console.log(err);
128 | throw err;
129 | }
at new Error (src/containers/App.js:126:19)
at constructClassInstance (node_modules/react-dom/cjs/react-dom.development.js:12484:18)
at updateClassComponent (node_modules/react-dom/cjs/react-dom.development.js:14255:5)
at beginWork (node_modules/react-dom/cjs/react-dom.development.js:15082:16)
at performUnitOfWork (node_modules/react-dom/cjs/react-dom.development.js:17820:12)
at workLoop (node_modules/react-dom/cjs/react-dom.development.js:17860:24)
at renderRoot (node_modules/react-dom/cjs/react-dom.development.js:17946:7)
at performWorkOnRoot (node_modules/react-dom/cjs/react-dom.development.js:18837:7)
at performWork (node_modules/react-dom/cjs/react-dom.development.js:18749:7)
at performSyncWork (node_modules/react-dom/cjs/react-dom.development.js:18723:3)
at requestWork (node_modules/react-dom/cjs/react-dom.development.js:18592:5)
at scheduleWork (node_modules/react-dom/cjs/react-dom.development.js:18401:5)
at scheduleRootUpdate (node_modules/react-dom/cjs/react-dom.development.js:19069:3)
at updateContainerAtExpirationTime (node_modules/react-dom/cjs/react-dom.development.js:19097:10)
at updateContainer (node_modules/react-dom/cjs/react-dom.development.js:19154:10)
at ReactRoot.Object.<anonymous>.ReactRoot.render (node_modules/react-dom/cjs/react-dom.development.js:19416:3)
at node_modules/react-dom/cjs/react-dom.development.js:19556:14
at unbatchedUpdates (node_modules/react-dom/cjs/react-dom.development.js:18952:10)
at legacyRenderSubtreeIntoContainer (node_modules/react-dom/cjs/react-dom.development.js:19552:5)
at Object.render (node_modules/react-dom/cjs/react-dom.development.js:19613:12)
at Object.render (src/containers/__tests__/App.test.js:33:14)
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { shallow, mount } from 'enzyme';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import App from '../App';
import configureStore from '../../store/configureStore';
describe('App', () => {
it('renders without crashing - mount', () => {
// const App = require('../App').default;
const div = document.createElement('div');
ReactDOM.render(
<Router>
<Provider store={configureStore()}>
<Route path="/">
<App />
</Route>
</Provider>
</Router>,
div
);
});
});
我的App.js:126
if (!process.env.REACT_APP_API_PATH) {
const err = Error('Configuration file has not been set up.');
console.log(err);
throw err;
}