Vue单元测试:“您正在开发模式下运行Vue。”?

时间:2020-04-27 22:38:54

标签: vue.js vue-test-utils

我了解您应该在jest.setup.js代码中设置

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

我知道。实际上,这是我的jest.setup.js代码。注意console.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的单元测试文件。不是我的所有单元测试文件,只是其中的一些。我真的很困惑。

因此,我随后添加了控制台日志语句,以确保在单元测试中收到此警告,实际上已调用了jest.setup.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

当我肯定执行jest.setup时,我如何得到Vue警告?

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

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

1 个答案:

答案 0 :(得分:5)

终于解决了。如果将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

应该解决这个烦人的问题。

相关问题