我理解你的jest.设置.js代码,你应该设置

Vue.config.productionTip = false;
Vue.config.devtools = false;

我也是.事实上,这是我的Jest .设置.js代码.注意控制台.日志(log)("yo-ho");

// test/setup.js

import Vue from 'vue';
import Vuetify from 'vuetify';
import { config } from '@vue/test-utils';
import VueCompositionApi from '@vue/composition-api'; // <-- Make the import

Vue.use(Vuetify);
Vue.use(VueCompositionApi);
Vue.config.productionTip = false;
Vue.config.devtools = false;
console.log('yo ho');
// https://vue-test-utils.vuejs.org/
// and this came from: https://github.com/kazupon/vue-i18n/issues/323
// it mocks out the $t function to return the key so you can test that the right key is being used
config.mocks = {
  $t: (key) => 'i18n:' + key
};

因此,考虑到这一点,我预计永远不会收到这些警告.但我在大约1/3的单元测试文件上做了.不是我所有的单元测试文件,只是其中的一些.我真的很困惑.

然后我添加了控制台日志(log)语句,以确保在单元测试中我得到了这个警告,即Jest .设置.js实际上正在被呼叫.这是我的一个单元测试的输出:

PASS src/components/announcement-banner.test.ts (8.255s)

  ● Console

    console.log tests/unit/jest.setup.js:12
      yo ho
    console.info node_modules/Vue/dist/vue.runtime.common.dev.js:8403
      Download the Vue Devtools extension for a better development experience:
      https://github.com/vuejs/vue-devtools
    console.info node_modules/Vue/dist/vue.runtime.common.dev.js:8412
      You are running Vue in development mode.
      Make sure to turn on production mode when deploying for production.
      See more tips at https://vuejs.org/guide/deployment.html

我到底是怎么得到Vue警告的,而我肯定是在Jest .设置?

为了消除这些警告,我必须转到特定的测试文件,并在createLocalVue()调用之前直接添加配置行.

Vue.config.productionTip = false;
Vue.config.devtools = false;
const localVue = createLocalVue();

推荐答案

终于解决了.如果在给定文件中导入Vue,那么看起来jest.mock('module')正在导入干净的Vue(到模拟中,幕后).我通过为Vue创建全局模拟解决了这个问题.

在项目的根目录(其中node_modules个目录)中,创建__mocks__/vue/index.ts(如果不使用TypeScript,则为.js)文件,其中包含:

import Vue from 'vue'

Vue.config.productionTip = false
Vue.config.devtools = false

export default Vue

应该能解决这个恼人的问题.

Vue.js相关问答推荐

将Vite Vuejs应用部署到Apache服务器

vue3 + pinia:如何从?store 中获取一个值

是否可以将 nuxt3 与类星体框架一起使用

组合 API | Vue 路由参数没有被监视

Vue 脚本标签中更漂亮的 destruct 功能

在重复内容区域中添加

如何使用带有自定义标签和类的`transition-group`

使用数组元素的计算(computed)属性

在 jest document.querySelector 中测试 vue 组件期间始终返回 null

Vue cli 3项目,图像路径中的动态src不起作用

ECMA6 中 nameFunction() {} 和 nameFunction () => {} 的区别

如何使用 vue-resource 添加额外的标头来发布请求?

带有子菜单的 Vuetify 导航抽屉

Vue.js 无法切换字体真棒图标

包含 Vue 组件的 HTML 字符串的 Nuxt 渲染函数

简单的点击功能不触发

执行调度程序刷新期间未处理的错误,这可能是一个 Vue 内部错误

如何从 keep-alive 中销毁缓存的 Vue 组件?

VS Code - 在 .vue 文件中启用 autoClosingTags 选项

如何订阅store 模块