0

我正在尝试使用 Mocha + Chai 对包含来自 HereMaps 的 Map 的 VueJS 组件进行单元测试。我按照 VueJS 文档中的官方教程进行操作,进展顺利。

但是,当我想对我的Map.vue组件(包含 HereMap)进行单元测试时,该组件似乎没有正确安装,并且无法克服终端错误。我认为这可能是 HereMap 需要的脚本的问题,但我不确定。

我的单元测试 mapTest.spec.ts如下:

import { shallowMount } from '@vue/test-utils'
import Map from '@/components/Map.vue'
import { expect } from 'chai'

describe('Map', () => {
    const wrapper = shallowMount(Map);
    it('mounts', () => {
        wrapper.setProps({latitude: 0, longitude: 0});
        expect(true).equal(true);
  })
})

当我评论该行时const wrapper = shallowMount(Map),错误消失并且测试运行顺利。

有谁知道如何正确地对 HereMap 进行单元测试?

非常感谢您的帮助。


我从 Mocha 得到的错误如下:

 RUNTIME EXCEPTION  Exception occurred while loading your tests

TypeError: self.eval is not a function
    at Window.<anonymous> (/home/amaury/Documents/web-projects/projet-restau/frontend/dist/js/webpack:/src/vendor/here-maps/3.1/mapsjs-core.js:34:1)
    at Object../src/vendor/here-maps/3.1/mapsjs-core.js (/home/amaury/Documents/web-projects/projet-restau/frontend/dist/js/webpack:/src/vendor/here-maps/3.1/mapsjs-core.js:404:1)
    at __webpack_require__ (/home/amaury/Documents/web-projects/projet-restau/frontend/dist/js/webpack:/webpack/bootstrap:19:1)
    at Object../node_modules/cache-loader/dist/cjs.js?!./node_modules/babel-loader/lib/index.js!./node_modules/ts-loader/index.js?!./node_modules/vuetify-loader/lib/loader.js?!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/components/Map.vue?vue&type=script&lang=ts& (/home/amaury/Documents/web-projects/projet-restau/frontend/dist/js/webpack:/src/components/Map.vue:10:1)
    at __webpack_require__ (/home/amaury/Documents/web-projects/projet-restau/frontend/dist/js/webpack:/webpack/bootstrap:19:1)
    at Module../src/components/Map.vue?vue&type=script&lang=ts& (/home/amaury/Documents/web-projects/projet-restau/frontend/dist/js/webpack:/src/components/Map.vue?0e8b:1:1)
    at __webpack_require__ (/home/amaury/Documents/web-projects/projet-restau/frontend/dist/js/webpack:/webpack/bootstrap:19:1)
    at Module../src/components/Map.vue (/home/amaury/Documents/web-projects/projet-restau/frontend/dist/js/webpack:/src/components/Map.vue?737c:1:1)
    at __webpack_require__ (/home/amaury/Documents/web-projects/projet-restau/frontend/dist/js/webpack:/webpack/bootstrap:19:1)
    at Object../tests/unit/map/mapTest.spec.ts (/home/amaury/Documents/web-projects/projet-restau/frontend/dist/js/webpack:/tests/unit/map/mapTest.spec.ts:2:1)
    at __webpack_require__ (/home/amaury/Documents/web-projects/projet-restau/frontend/dist/js/webpack:/webpack/bootstrap:19:1)
    at run (/home/amaury/Documents/web-projects/projet-restau/frontend/dist/js/webpack:/node_modules/@vue/cli-plugin-unit-mocha/node_modules/mochapack/lib/entry.js:3:1)
    at Array.forEach (<anonymous>)
    at Object../node_modules/@vue/cli-plugin-unit-mocha/node_modules/mochapack/lib/entry.js (/home/amaury/Documents/web-projects/projet-restau/frontend/dist/js/webpack:/node_modules/@vue/cli-plugin-unit-mocha/node_modules/mochapack/lib/entry.js:10:1)
    at __webpack_require__ (/home/amaury/Documents/web-projects/projet-restau/frontend/dist/js/webpack:/webpack/bootstrap:19:1)
    at /home/amaury/Documents/web-projects/projet-restau/frontend/dist/js/webpack:/webpack/bootstrap:83:1
    at Object.<anonymous> (/home/amaury/Documents/web-projects/projet-restau/frontend/dist/js/main.js:87:10)
    at Module._compile (internal/modules/cjs/loader.js:1123:30)
    at Object._module2.default._extensions.<computed> [as .js] (/home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/@vue/cli-plugin-unit-mocha/node_modules/mochapack/src/util/registerRequireHook.js:99:12)
    at Module.load (internal/modules/cjs/loader.js:972:32)
    at Function.Module._load (internal/modules/cjs/loader.js:872:14)
    at Module.require (internal/modules/cjs/loader.js:1012:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at /home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/@vue/cli-plugin-unit-mocha/node_modules/mocha/lib/mocha.js:334:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/@vue/cli-plugin-unit-mocha/node_modules/mocha/lib/mocha.js:331:14)
    at Mocha.run (/home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/@vue/cli-plugin-unit-mocha/node_modules/mocha/lib/mocha.js:809:10)
    at cb (/home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/@vue/cli-plugin-unit-mocha/node_modules/mochapack/src/runner/TestRunner.js:96:21)
    at /home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/@vue/cli-plugin-unit-mocha/node_modules/mochapack/src/webpack/compiler/registerReadyCallback.js:12:7
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:61:1)
    at AsyncSeriesHook.lazyCompileHook (/home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/tapable/lib/Hook.js:154:20)
    at /home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/webpack/lib/Compiler.js:304:22
    at Compiler.emitRecords (/home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/webpack/lib/Compiler.js:499:39)
    at /home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/webpack/lib/Compiler.js:298:10
    at /home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/webpack/lib/Compiler.js:485:14
    at eval (eval at create (/home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:14:1)
    at /home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/copy-webpack-plugin/dist/index.js:115:7
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:10:1)
    at AsyncSeriesHook.lazyCompileHook (/home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/tapable/lib/Hook.js:154:20)
    at /home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/webpack/lib/Compiler.js:482:27
    at /home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/neo-async/async.js:2818:7
    at done (/home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/neo-async/async.js:3522:9)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:4:1)
    at /home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/webpack/lib/Compiler.js:464:33
    at MemoryFileSystem.writeFile (/home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/memory-fs/lib/MemoryFileSystem.js:328:9)
    at writeOut (/home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/webpack/lib/Compiler.js:462:30)
    at Immediate._onImmediate (/home/amaury/Documents/web-projects/projet-restau/frontend/node_modules/memory-fs/lib/MemoryFileSystem.js:288:4)
    at processImmediate (internal/timers.js:456:21)
    at process.topLevelDomainCallback (domain.js:137:15)
4

0 回答 0