抑制单元测试中的 Vue 警告 [英] Suppress Vue warnings in unit tests

查看:36
本文介绍了抑制单元测试中的 Vue 警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to suppress my warnings in my tests following the config listed here: https://vue-test-utils.vuejs.org/api/config.html#silent, which is as follows:

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

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

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

However, I am still seeing the warning in the test results:

  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>

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

It's worth noting that I do not see this error in normal usage of the app. This only pops up in tests (otherwise I would attempt to fix the actual suggested issue).

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

What am I doing wrong here and why can I not suppress these warning? Or am I misunderstanding what silent is supposed to do?

推荐答案

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

According to the VueJS docs - https://vue-test-utils.vuejs.org/api/config.html#silent

沉默

类型:布尔型

默认值:真

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

It suppresses warnings triggered by Vue while mutating component's observables (e.g. props). When set to false, all warnings are visible in the console. This is a configurable way which relies on Vue.config.silent.

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

which relies on Vue.config.silent, so all you need is to import vue package and set it's config.silent to false

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

我在我的 Github 中放了一个工作示例,它只是官方示例的一个分支,但在测试过程中不显示警告.

I put a working example here in my Github, it's just a fork of official example but it doesn't show warnings during the tests.

https://github.com/al1b/vue-test-utils-入门

欲了解更多信息:

如果你查看源代码:

  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}`)
    }
  }

这篇关于抑制单元测试中的 Vue 警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆