【发布时间】:2020-04-27 22:38:54
【问题描述】:
我知道在您的 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();
【问题讨论】:
-
我有同样的问题,固定了一个导致这种行为的组件。到目前为止,我发现它就在我的商店里。我在商店主文件中导入 Vue 并调用
Vue.use(Vuex)。商店在应用程序的各个地方使用,不仅在组件中,如果我在商店文件中添加Vue.config.productionTip = false,它可以工作,控制台日志消失。但这不是我的解决方案,在 jest init 之后调用了 store,这看起来很奇怪,所以我会继续挖掘。 -
哦,天哪。看起来
jest.mock()搞砸了。 -
我在使用
setTimeout等待所有承诺在开玩笑的单元测试中解决时得到了这个(这就是 flush-promises npm 模块的工作原理)。 -
对所提供答案的反馈如何?
标签: vue.js vue-test-utils