我试图使用jest和酶测试一个React组件。但是每当我试图安装组件时,我都会得到以下错误。
Error: Uncaught [SecurityError: replaceState cannot update history to a URL which differs in components other than in path, query, or fragment.]
at reportException (node_modules/jsdom/lib/jsdom/living/helpers/runtime-script-errors.js:66:24)
at invokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:209:9)
at HTMLUnknownElementImpl._dispatch (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:119:9)
at HTMLUnknownElementImpl.dispatchEvent (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:82:17)
at HTMLUnknownElementImpl.dispatchEvent (node_modules/jsdom/lib/jsdom/living/nodes/HTMLElement-impl.js:30:27)
at HTMLUnknownElement.dispatchEvent (node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:157:21)
at Object.invokeGuardedCallbackDev (node_modules/react-dom/cjs/react-dom.development.js:199:16)
at invokeGuardedCallback (node_modules/react-dom/cjs/react-dom.development.js:256:31)
at commitRoot (node_modules/react-dom/cjs/react-dom.development.js:17458:7)
at completeRoot (node_modules/react-dom/cjs/react-dom.development.js:18912:3) SecurityError: replaceState cannot update history to a URL which differs in components other than in path, query, or fragment.
at HistoryImpl._sharedPushAndReplaceState (node_modules/jsdom/lib/jsdom/living/window/History-impl.js:83:15)
at HistoryImpl.replaceState (node_modules/jsdom/lib/jsdom/living/window/History-impl.js:57:10)
at History.replaceState (node_modules/jsdom/lib/jsdom/living/generated/History.js:129:21)
at node_modules/history/createBrowserHistory.js:211:23
at Object.confirmTransitionTo (node_modules/history/createTransitionManager.js:44:7)
at Object.replace (node_modules/history/createBrowserHistory.js:202:23)
at Redirect.perform (node_modules/react-router/Redirect.js:102:15)
at Redirect.componentDidMount (node_modules/react-router/Redirect.js:61:32)
at commitLifeCycles (node_modules/react-dom/cjs/react-dom.development.js:15961:22)
at commitAllLifeCycles (node_modules/react-dom/cjs/react-dom.development.js:17262:7)
console.error node_modules/react-dom/cjs/react-dom.development.js:15749
The above error occurred in the <Redirect> component:
in Redirect (created by ProfileContainer)
in ProfileContainer
in Router (created by BrowserRouter)
in BrowserRouter (created by WrapperComponent)
in WrapperComponent
这是我的React组件
第一次
这是我的测试文件
describe("<ProfileContainer />", () => {
it("renders without crashing", () => {
const wrapper = mount(
<BrowserRouter>
<ProfileContainer {...mockProps} />
</BrowserRouter>
);
});
});
组态设定
jest: 23.6.0
enzyme: 3.7.0
enzyme-adapter-react-16: 1.7.0
任何建议都是有用的。
2条答案
按热度按时间j91ykkif1#
我不知道这是否有帮助,但我在使用
BrowserRouter
的Jest测试中遇到了这个错误,切换到MemoryRouter
使它消失了。uxhixvfz2#
这个错误是描述性的,你试图使用history. replaceState的url与当前url的来源或主机不同。
这是发生在这里
您可能需要更改jest中的testURL,使其与
getOrgName()
的源地址和主机地址匹配,因为jest中的默认url是http://localhost
这里有更多关于如何配置jest和覆盖testURL的details。