问题描述
我正在为包含window.location.href
的文件实施单元测试,我需要检查它.
I am implementing unit test for a file that contain window.location.href
and I need to check it.
我开玩笑的版本是22.0.4
.当我在节点版本> = 10
My jest version is 22.0.4
. Everything is fine when I run my test on node version >=10
但是在v8.9.3
console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Not implemented: navigation (except hash changes)
我对此一无所知.我在许多页面上进行了搜索,以找出解决方案或有关此问题的任何提示,以了解此处发生的情况.
I have no idea about it. I have searched on many page to find out the solution or any hint about this to figure out what happened here.
[UPDATE]-我对源代码进行了深入研究,我认为此错误来自jsdom.
[UPDATE] - I took a look deep to source code and I think this error is from jsdom.
at module.exports (webapp/node_modules/jsdom/lib/jsdom/browser/not-implemented.js:9:17)
at navigateFetch (webapp/node_modules/jsdom/lib/jsdom/living/window/navigation.js:74:3)
navigation.js文件
exports.evaluateJavaScriptURL = (window, urlRecord) => {
const urlString = whatwgURL.serializeURL(urlRecord);
const scriptSource = whatwgURL.percentDecode(Buffer.from(urlString)).toString();
if (window._runScripts === "dangerously") {
try {
return window.eval(scriptSource);
} catch (e) {
reportException(window, e, urlString);
}
}
return undefined;
};
exports.navigate = (window, newURL, flags) => {
// This is NOT a spec-compliant implementation of navigation in any way. It implements a few selective steps that
// are nice for jsdom users, regarding hash changes and JavaScript URLs. Full navigation support is being worked on
// and will likely require some additional hooks to be implemented.
const document = idlUtils.implForWrapper(window._document);
const currentURL = document._URL;
if (!flags.reloadTriggered && urlEquals(currentURL, newURL, { excludeFragments: true })) {
if (newURL.fragment !== currentURL.fragment) {
navigateToFragment(window, newURL, flags);
}
return;
}
// NOT IMPLEMENTED: Prompt to unload the active document of browsingContext.
// NOT IMPLEMENTED: form submission algorithm
// const navigationType = 'other';
// NOT IMPLEMENTED: if resource is a response...
if (newURL.scheme === "javascript") {
window.setTimeout(() => {
const result = exports.evaluateJavaScriptURL(window, newURL);
if (typeof result === "string") {
notImplemented("string results from 'javascript:' URLs", window);
}
}, 0);
return;
}
navigateFetch(window);
};
not-implemented.js
module.exports = function (nameForErrorMessage, window) {
if (!window) {
// Do nothing for window-less documents.
return;
}
const error = new Error(`Not implemented: ${nameForErrorMessage}`);
error.type = "not implemented";
window._virtualConsole.emit("jsdomError", error);
};
我在这些文件中看到了一些奇怪的逻辑.
I see some weird logics in these file.
-
const scriptSource = whatwgURL.percentDecode(Buffer.from(urlString)).toString();
- 然后检查字符串并返回错误
推荐答案
我在一项单元测试中遇到了类似的问题.这是我为解决此问题所做的事情.
I faced a similar issue in one of my unit tests. Here's what I did to resolve it.
- 用
window.location.assign(url)
替换window.location.href
或window.location.replace(url)
-
JSDOM将仍然抱怨
window.location.assign
未实现.
- Replace
window.location.href
withwindow.location.assign(url)
ORwindow.location.replace(url)
JSDOM will still complain about
window.location.assign
not implemented.
Error: Not implemented: navigation (except hash changes)
Error: Not implemented: navigation (except hash changes)
然后,在其中一个包含window.assign(url)
或window.replace(url)
的上述组件/函数的单元测试中,定义以下内容
Then, in one of your unit tests for the above component / function containing window.assign(url)
or window.replace(url)
define the following
-
sinon.stub(window.location, 'assign');
-
sinon.stub(window.location, 'replace');
- 确保您导入sinon
import sinon from 'sinon';
sinon.stub(window.location, 'assign');
sinon.stub(window.location, 'replace');
- Make sure you import sinon
import sinon from 'sinon';
希望这可以像为我那样为您解决问题.
Hopefully, this should fix the issue for you as it did for me.
JSDOM抱怨Error: Not implemented: navigation (except hash changes)
的原因是因为JSDOM没有实现window.alert
,window.location.assign
等方法.
The reason JSDOM complains about the Error: Not implemented: navigation (except hash changes)
is because JSDOM does not implement methods like window.alert
, window.location.assign
, etc.
参考:
- http://xxd3vin.github.io/2018/03/13/error-not-implemented-navigation-except-hash-changes.html
- https://www.npmjs.com/package/jsdom#virtual-consoles
- http://xxd3vin.github.io/2018/03/13/error-not-implemented-navigation-except-hash-changes.html
- https://www.npmjs.com/package/jsdom#virtual-consoles
这篇关于如何解决错误:未实现:导航(哈希更改除外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!