【发布时间】:2020-12-25 07:59:06
【问题描述】:
是否可以在没有 Vue CLI/Webpack/Babel 设置的情况下测试使用纯 javascript 开发的 Vue 组件?
到目前为止,我在网上找到的关于 subjet 的文档非常少:
- https://github.com/vuejs/vue-test-utils/issues/1192
- https://vue-test-utils.vuejs.org/installation/#running-vue-test-utils-without-a-build-step
- How to test plain Vue components (not single file components)
我安装了 Vue,Vue 测试工具,JSDOM
我的 package.json :
{
"name": "GuichetUnique.CMS",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"jest": "^26.4.2",
"vue": "^2.6.12",
"vue-template-compiler": "^2.6.12"
},
"devDependencies": {
"@vue/test-utils": "^1.0.5",
"jsdom": "16.4.0",
"jsdom-global": "3.0.2"
},
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Jest 使用简单的 js 函数运行良好,但我没有设法测试 Vue 组件。
我的测试内容
require('jsdom-global')();
var testUtils = require('@vue/test-utils'), Vue = require('vue');
testUtils.mount({ template: '<div>test</div>' });
测试结果
TypeError: Illegal invocation at removeEventListener (node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:157:15)
请帮忙!
【问题讨论】:
-
希望对您有所帮助。
Deprecation Notice: When stubbing components, supplying a string (ComponentToStub: '<div class="stubbed" />) is no longer supported.Link -
谢谢迪利普。我将进一步探索 API
-
TypeError: Illegal invocation at removeEventListener此错误代码表明您正在尝试删除不存在的事件侦听器。请仔细检查您的代码。 -
请问您为什么不想使用 VUE-cli?
-
感谢您的提问 Eliezer。基本上,不选择使用 Vue-Cli 背后的想法是我们所有的组件都已经用纯 JavaScript 编写,没有任何构建步骤。我们希望添加单元测试来放置代码,而不会干扰 6 名开发人员团队的开发工作流程。现在也许使用 Vue-Cli 的开销是值得的,但我们想先权衡我们的选择。
标签: testing visual-studio-2017 jestjs components vue-test-utils