javascript - 如何修复错误:未实现:导航(哈希更改除外)
问题描述
我正在为包含的文件实施单元测试,window.location.href
我需要检查它。
我的笑话版本是22.0.4
. 当我在节点版本 >=10 上运行测试时,一切都很好
但是当我运行它时出现此错误v8.9.3
console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Not implemented: navigation (except hash changes)
我对此一无所知。我在许多页面上进行了搜索,以找出解决方案或任何有关此问题的提示,以弄清楚这里发生了什么。
[更新] - 我深入查看了源代码,我认为这个错误来自 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)
导航.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);
};
未实现的.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);
};
我在这些文件中看到了一些奇怪的逻辑。
const scriptSource = whatwgURL.percentDecode(Buffer.from(urlString)).toString();
- 然后检查字符串并返回错误
解决方案
仅适用于我的替代版本jest
:
let assignMock = jest.fn();
delete window.location;
window.location = { assign: assignMock };
afterEach(() => {
assignMock.mockClear();
});
参考: https ://remarkablemark.org/blog/2018/11/17/mock-window-location/
推荐阅读
- laravel - 使用 laravel header() 方法发送 CSRF 令牌
- asp.net - 在 JMeter 中,在 Asp.net Web 应用程序中执行负载测试时,获取登录帖子请求的“对象已移至此处”响应
- php - PHP 从多个表中选择数据会引发错误
- ms-word - MS Word中行尾的通配符?
- php - 从多个表中选择列的总和并按 id 分组
- python - 带有嵌入式 if 语句的 For 循环以更新数据帧
- php - 调用 php 页面来创建新图像会一遍又一遍地生成相同的图像
- sql - Delphi sql:处理字段名称中的空格字符
- oracle - Can anyone tell me query to carry forward balance to next day in oracle?
- amazon-web-services - 如何在 api 网关中设置 text/plain 内容类型