![](/img/trans.png)
[英]how to unit test components in vue.js that rely on external dependencies?
[英]How to bypass test coverage for external/dependency vue components?
我们有两个 vue3 项目。 项目 1 有一些组件使用项目 2 中的组件作为导入的依赖项。
在项目 1 中, MyComponent.vue
:
<template>
<Project2Modal> Some html here </Project2Modal>
</template>
<script>
import { defineComponent } from 'vue';
import Project2Modal from 'project2/components/Project2Modal.vue';
export default defineComponent({
components: {
Project2Modal,
},
props: {
title: {
type: String,
required: true,
},
...
});
</script>
我们遇到的问题是,当我们尝试测试(使用jest
和vue-test-utils
)项目 1 中的那些组件时,测试覆盖率还包括项目 2 依赖组件的覆盖率(如“未覆盖”行所示)根本不是MyComponent.vue
文件的一部分)。 这使得项目 1 的整体测试覆盖率低于应有的水平(当我们从模板中删除project2Modal
组件时,它变为 100%)。
项目 1 test --coverage
------------------------------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
------------------------------------------|---------|----------|---------|---------|-------------------
All files | 96.96 | 90.04 | 96.07 | 97.05 |
src | 100 | 100 | 100 | 100 |
globals.js | 100 | 100 | 100 | 100 |
src/components | 73.33 | 25 | 20 | 73.33 |
MyComponent.vue | 73.33 | 25 | 20 | 73.33 | 118-136
我们如何告诉项目 1 的 jest 绕过项目 2 组件的测试覆盖率? (因为那些已经在项目 2 中进行了测试)。
在jest.config.js
,我们已经尝试禁止从项目 2 中获取覆盖,但这没有任何作用:
collectCoverageFrom: [
'!<rootDir>/node-modules/project2',
],
我们还没有找到与此问题相关的或已经提出的问题,因此我们将不胜感激!
你在使用伊斯坦布尔吗? 看起来你可以通过几种方式实现你想要的,或者你可以直接在 package.json 中修改你的 Jest 设置。
当我在测试覆盖率时忽略导入的文件时,我使用了这篇文章,它让我得到了排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.