我理解你的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相关问答推荐

Svelte 中的简单react 性实验以意想不到的方式失败了. Vue 等效项没有

我在表格中创建了一个包含信息的新行,但在 Cypress e2e 测试中我无法获得最后一行(新行)

如何在vue中获取特定类别的产品列表

TailwindCSS 为什么前者比后者重要?

VueJS中的绑定函数含义

在keyup Vuejs 2上获取输入值

Vue 和 Bootstrap Vue - 动态使用插槽

使用 vue-cli 遇到无法推断解析器错误

未捕获的错误:[vue-composition-api] 必须在使用任何函数之前调用 Vue.use(plugin)

可以同时使用 Tailwind css 和 Bootstrap 4 吗?

包含 Vue.js v-if 和 v-for 指令的 HTML 标签在加载时flash

如何在nuxt路由中添加meta?

NUXT如何将数据从页面传递到布局

Vue.js 处理多次点击事件

Vue-i18n '无法读取 Proxy.Vue.$t 处未定义的属性 '_t'

vue-cli-service build --target lib 导入为 lib 时丢失图像路径

我应该将所有数据存储在 vuex 状态

在 vue.js 组件中react this.props.children

TypeError: Object(...) 不是 Vue 中的函数

如何访问通用 javascript 模块中的当前路由元字段