【问题标题】:Suppress Vue warnings in unit tests在单元测试中抑制 Vue 警告
【发布时间】:2019-05-06 08:43:52
【问题描述】:

我正在尝试按照此处列出的配置抑制我的测试中的警告:https://vue-test-utils.vuejs.org/api/config.html#silent,如下所示:

import { config } from '@vue/test-utils';

// this should actually be the default but the default is not working
config.silent = true;

但是,我仍然在测试结果中看到警告:

  TheQueue
✓ should show the queue bar if there are items queued
✓ should show the correct count of queued items in queued bar
[Vue warn]: Avoid mutating a prop directly since the value will be 
overwritten whenever the parent component re-renders. Instead, use a 
data or computed property based on the prop's value. Prop being 
mutated: "mdTemplateData"

found in

---> <MdTab>
       <MdContent>
         <MdTabs>
           <MdDrawer>
             <TheQueue> at src/components/the-queue/TheQueue.vue
               <Root>

值得注意的是,我在应用程序的正常使用中没有看到此错误。这只会在测试中弹出(否则我会尝试修复实际建议的问题)。

我在这里做错了什么,为什么我不能取消这些警告?还是我误解了silent 应该做什么?

【问题讨论】:

  • 我也在尝试在单元测试中静默 Vue 警告,但据我阅读 vue-test-utils source code 了解,此 silent 配置属性仅在您使用 setPropsmethod 时触发你的测试。
  • 这方面有什么进展吗?我也有同样的问题
  • 我知道你在 6 个月前问过,但我现在才看到你的问题,希望对你有帮助

标签: vue.js vue-test-utils


【解决方案1】:

根据 VueJS 文档 - https://vue-test-utils.vuejs.org/api/config.html#silent

静音

类型:布尔值

默认值:真

它在改变组件时抑制由 Vue 触发的警告 可观察的(例如道具)。设置为 false 时,所有警告均可见 在控制台中。这是一种可配置的方式,它依赖于 Vue.config.silent.

它依赖于 Vue.config.silent,所以你只需要导入 vue 包并将它的 config.silent 设置为 false

import Vue from `vue`
Vue.config.silent = true;

我在我的 Github 中放了一个工作示例,它只是官方示例的一个分支,但它在测试期间没有显示警告。

https://github.com/al1b/vue-test-utils-getting-started

更多信息:

如果您检查source code

  warn = (msg, vm) => {
    const trace = vm ? generateComponentTrace(vm) : ''

    if (config.warnHandler) {
      config.warnHandler.call(null, msg, vm, trace)
    } else if (hasConsole && (!config.silent)) {
      console.error(`[Vue warn]: ${msg}${trace}`)
    }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-18
    • 2021-09-26
    • 1970-01-01
    • 2011-03-31
    • 2019-10-22
    • 2014-12-19
    • 2021-12-27
    • 1970-01-01
    相关资源
    最近更新 更多