gpt4 book ai didi

vue.js - [Vue 警告] : Unknown custom element: - When running jest unit tests

转载 作者:搜寻专家 更新时间:2023-10-30 22:11:44 24 4
gpt4 key购买 nike

问题

我正在使用 nuxt 1.4 和路由,使用 Jest 进行单元测试。我的应用程序没有抛出错误并且似乎运行良好。然而,在运行我的单元测试时 npm run unit (开 Jest )它在终端中抛出错误:[Vue warn]: Unknown custom element: <nuxt-link> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

预期

我希望它不会抛出此错误,因为我的应用程序正在运行。

文件

package.json :

{
"name": "vue-starter",
"version": "1.0.0",
"description": "Nuxt.js project",
"private": true,
"scripts": {
"dev": "nuxt",
"build": "nuxt build",
"start": "nuxt start",
"generate": "nuxt generate",
"lint": "eslint --ext .js,.vue --ignore-path .gitignore .",
"precommit": "npm run lint",
"test": "npm run lint && npm run unit",
"unit": "jest",
"unit:report": "jest --coverage"
},
"dependencies": {
"babel-jest": "^22.4.1",
"jest-serializer-vue": "^1.0.0",
"node-sass": "^4.7.2",
"npm": "^5.7.1",
"nuxt": "^1.0.0",
"sass-loader": "^6.0.7",
"vue-jest": "^2.1.1"
},
"devDependencies": {
"@vue/test-utils": "^1.0.0-beta.12",
"babel-eslint": "^8.2.1",
"eslint": "^4.15.0",
"eslint-friendly-formatter": "^3.0.0",
"eslint-loader": "^1.7.1",
"eslint-plugin-vue": "^4.0.0",
"jest": "^22.4.2"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
],
"jest": {
"moduleFileExtensions": [
"js",
"vue"
],
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/babel-jest",
".*\\.(vue)$": "<rootDir>/node_modules/vue-jest"
},
"snapshotSerializers": [
"<rootDir>/node_modules/jest-serializer-vue"
]
}
}

我测试的组件:

<template>
<div>
<nuxt-link class="name" :to="{ path: `entity/${item.id}`, params: { id: item.id }}">{{item.name}}</nuxt-link>
<button class="connect" @click="connect">{{ btnText }}</button>
</div>
</template>

<script>
// import nuxtLink from '../.nuxt/components/nuxt-link';

const connectionStatusMap = [
'Connect',
'Connected',
'Pending',
'Cancel',
];

export default {
/*components: {
'nuxt-link': nuxtLink,
},*/
props: {
item: {
type: Object
}
},
...
}
</script>

我的测试脚本:

import TestItem from '../components/TestItem';
import { shallow, mount, createLocalVue } from '@vue/test-utils';
import Vuex from 'vuex';
import VueRouter from 'vue-router';

const localVue = createLocalVue()

localVue.use(Vuex)
localVue.use(VueRouter)

...
it(`should show the entity`, () => {
const wrapper = mount(TestItem, {
propsData: { item },
localVue,
store,
// stubs: ['nuxt-link'],
})
expect(wrapper.find('.name').text()).toBe(item.name);
});

it(`should show allow me to connect if I'm not yet connected`, () => {
const wrapper = shallow(TestItem, {
propsData: { item },
localVue,
store,
stubs: ['nuxt-link'],
})
expect(wrapper.find('.connect').text()).toBe('Connect');
});
...

我试过了

我尝试创建一个 localVue 并按照建议对组件进行 stub in this github comment我也试过shallow/mount但这似乎也不起作用。

最佳答案

这就是我如何摆脱恼人的警告:

包含 RouterLinkStub,例如:

import { shallowMount, createLocalVue, RouterLinkStub } from '@vue/test-utils';

将 NuxtLink stub 映射到 RouterLinkStub

const wrapper = shallowMount(TestItem, {
...
stubs: {
NuxtLink: RouterLinkStub
}
})

如果您正在检查 nuxt-link 文本或其他内容,请更改:

const link = wrapper.find('nuxt-link');

const link = wrapper.find(RouterLinkStub);

https://onigra.github.io/blog/2018/03/19/vue-test-utils-router-link-stub/ 上发现了这个黄金

幸好你不需要懂日语就可以阅读代码...

关于vue.js - [Vue 警告] : Unknown custom element: <nuxt-link> - When running jest unit tests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49665571/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com