ReferenceError:窗口未定义。我开玩笑地将npm test运行到单元测试时遇到了此错误。错误来自以下代码导出功能。有人遇到这种错误并解决了吗?
import { createStore, applyMiddleware, compose, combineReducers } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from '../modules/rootReducer';
export function injectAsyncReducers(asyncReducers) {
const injectReducers = Object.keys(asyncReducers).reduce((all, item) => {
if (store.asyncReducers[item]) {
delete all[item];
}
return all;
}, asyncReducers);
store.asyncReducers = Object.assign({}, store.asyncReducers, injectReducers);
replaceReducers(rootReducer);
}
最佳答案
当您没有为jest使用正确的testEnviremoent配置时,通常会出现此错误,在这种情况下,它应该是jsdom(您可以在这里查看:https://github.com/tmpvar/jsdom)。您可以在package.json文件中进行配置,如下所示:"jest": {"testEnvironment": "node"}
如果您使用的是create-react-app,则测试脚本应如下所示:"test": "react-scripts test --env=jsdom"
或者,您可以在此处查看testEviroment配置的更多选项:https://facebook.github.io/jest/docs/en/configuration.html#testenvironment-string