这里为JavaScript测试Bug映射API

HERE Maps API for JavaScript Testing Bug

本文关键字:映射 API Bug 测试 JavaScript 这里      更新时间:2023-09-26

Update:我仍然得到与下面相同的问题,但现在JSDom将以下错误发布到控制台:

Error: Uncaught [SyntaxError: ]
    at reportException (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/living/helpers/runtime-script-errors.js:58:24)
    at processJavaScript (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/living/nodes/HTMLScriptElement-impl.js:130:7)
    at HTMLScriptElementImpl._eval (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/living/nodes/HTMLScriptElement-impl.js:65:7)
    at e (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/browser/resource-loader.js:30:22)
    at Object.check (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/living/nodes/Document-impl.js:97:11)
    at /Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/living/nodes/Document-impl.js:116:12
    at wrappedEnqueued (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/browser/resource-loader.js:234:16)
    at Request.request [as _callback] (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/browser/resource-loader.js:184:9)
    at Request.self.callback (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/request/request.js:187:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:191:7)
    at Request.<anonymous> (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/request/request.js:1044:10)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
    at Gunzip.<anonymous> (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/request/request.js:965:12)
    at emitNone (events.js:91:20)
    at Gunzip.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:934:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9) SyntaxError
    at XMLHttpRequest.open (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:451:15)
    at http://js.api.here.com/v3/3.0/mapsjs-core.js:51:86
    at http://js.api.here.com/v3/3.0/mapsjs-core.js:51:383
    at http://js.api.here.com/v3/3.0/mapsjs-core.js:290:26
    at Object.exports.runInContext (vm.js:44:17)
    at processJavaScript (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/living/nodes/HTMLScriptElement-impl.js:128:10)
    at HTMLScriptElementImpl._eval (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/living/nodes/HTMLScriptElement-impl.js:65:7)
    at e (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/browser/resource-loader.js:30:22)
    at Object.check (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/living/nodes/Document-impl.js:97:11)
    at /Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/living/nodes/Document-impl.js:116:12
    at wrappedEnqueued (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/browser/resource-loader.js:234:16)
    at Request.request [as _callback] (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/browser/resource-loader.js:184:9)
    at Request.self.callback (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/request/request.js:187:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:191:7)
    at Request.<anonymous> (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/request/request.js:1044:10)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
    at Gunzip.<anonymous> (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/request/request.js:965:12)
    at emitNone (events.js:91:20)
    at Gunzip.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:934:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

我试图让HERE地图JavaScript API在使用Mocha编写的测试中正确工作。

it("should generate a map when the component gets rendered", function generateMapTest(done) {
    this.timeout(0);
    const scriptMap = getScriptMap();
    const scriptNames = Object.keys(scriptMap);
    const finalScriptToLoad = last(scriptNames);
    getScript(finalScriptToLoad).onLoad((err: any, res?: any) => {
        global.H = window.H;
        const platform = new H.service.Platform({
            app_id: 'app_id',
            app_code: 'app_code',
        });
        console.log(window.H);
        done(err);
    });
});

为了解释上面的内容—scriptMap变量用于存储一个对象,该对象包含要加载的每个HERE Maps脚本的url。每个脚本都是通过使用JavaScript将标签附加到DOM(使用jsdom)来顺序加载的。当每个脚本加载时,调用回调数组中的每个回调,传递通常的错误和响应对象。使用如下所示的onLoad方法将回调函数添加到该数组。

当从HERE服务器加载完最后的脚本后,我将H对象附加到Mocha中的全局变量,并尝试创建一个Platform服务。这会导致以下错误:
Unhandled rejection TypeError: Cannot read property 'mapsjs-core' of undefined
    at Hi (http://js.api.here.com/v3/3.0/mapsjs-core.js:287:626)
    at Ji (http://js.api.here.com/v3/3.0/mapsjs-core.js:288:350)
    at V.Es (eval at <anonymous> (http://js.api.here.com/v3/3.0/mapsjs-core.js:47:404), <anonymous>:13:188)
    at new V (eval at <anonymous> (http://js.api.here.com/v3/3.0/mapsjs-core.js:47:404), <anonymous>:12:257)
    at Object.getPlatform (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/src/utils/get-platform.js:9:16)
    at /Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/src/HEREMap.js:34:50
    at /Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/src/utils/cache.js:52:104
    at arrayEach (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/lodash/lodash.js:522:11)
    at Function.forEach (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/lodash/lodash.js:9011:14)
    at /Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/src/utils/cache.js:52:30

我正在努力调试所提供的代码的缩小版本,所以如果我能有一些帮助跟踪这个bug的来源,我会很感激。可能值得说明的是,所有这些都是在构建HERE Maps React组件的上下文中,源代码可以在这里找到:https://github.com/josh-es/react-here-maps.

~ ^ ~

我设法将问题隔离到XMLHttpRequest API的JSDom实现。用'xmlhttprequest' npm包替换它修复了这个问题。运行命令

安装
npm install --save-dev xmlhttprequest

然后修复上面的问题:

import { XMLHttpRequest } from "xmlhttprequest";
global.window.XMLHttpRequest = XMLHttpRequest;