Vuejs2 Vue测试Utils未知自定义元素:<;路由器链路>;

Vuejs2 Vue测试Utils未知自定义元素:<;路由器链路>;,vuejs2,vue-component,jestjs,vue-router,vue-test-utils,Vuejs2,Vue Component,Jestjs,Vue Router,Vue Test Utils,我正在使用Jest来运行使用vue测试utils库的测试 尽管我已经将VueRouter添加到localVue实例中,但它说它实际上找不到路由器链接组件。如果代码看起来有点古怪,那是因为我使用的是TypeScript,但它应该读起来非常接近ES6。。。主要的是@Prop()与传入props是一样的:{..} Vue组件: $ jest --config test/unit/jest.conf.js PASS ClientApp\components\__tests__\temp.spec.t

我正在使用Jest来运行使用vue测试utils库的测试

尽管我已经将VueRouter添加到localVue实例中,但它说它实际上找不到路由器链接组件。如果代码看起来有点古怪,那是因为我使用的是TypeScript,但它应该读起来非常接近ES6。。。主要的是@Prop()与传入props是一样的:{..}

Vue组件:

$ jest --config test/unit/jest.conf.js
 PASS  ClientApp\components\__tests__\temp.spec.ts
  Temp.vue Component
    √ renders a router-link tag with to temp.url (30ms)

  console.error node_modules\vue\dist\vue.runtime.common.js:589
    [Vue warn]: Unknown custom element: <router-link> - did you register the 
component correctly? For recursive components, make sure to provide the 
"name" option.

    (found in <Root>)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        4.677s
Ran all test suites.
Done in 6.94s.

{{temp.name}
从“Vue”导入Vue
从“vue类组件”导入组件
从“vue属性装饰器”导入{Prop}
从“/Temp”导入{Temp}
@组成部分({
姓名:“临时工”
})
导出默认类TempComponent扩展Vue{
@Prop()私有温度:temp
}
临时工{
填充顶部:10px;
}
临时型号:

$ jest --config test/unit/jest.conf.js
 PASS  ClientApp\components\__tests__\temp.spec.ts
  Temp.vue Component
    √ renders a router-link tag with to temp.url (30ms)

  console.error node_modules\vue\dist\vue.runtime.common.js:589
    [Vue warn]: Unknown custom element: <router-link> - did you register the 
component correctly? For recursive components, make sure to provide the 
"name" option.

    (found in <Root>)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        4.677s
Ran all test suites.
Done in 6.94s.
导出类温度{
公共静态默认值:Temp=newtemp(-1',)
公共url:string
构造函数(公共id:编号,公共名称:字符串){
this.id=id
this.name=name
this.url='/temp/'+id
}
}
玩笑测试

从'@vue/test-utils'导入{createLocalVue,shallow}
从“@/components/Temp.vue”导入TempComponent
从“@/components/Temp”导入{Temp}
从“vue路由器”导入VueRouter
const localVue=createLocalVue()
localVue.use(VueRouter)
描述('Temp.vue Component',()=>{
test('呈现一个指向临时url的路由器链接标记',()=>{
常数温度=默认温度
temp.url=http://some-url.com'
常量包装=浅(临时组件{
propsData:{temp}
})
const aWrapper=wrapper.find('router-link')
expect((aWrapper.attributes()如有).to).toBe(临时url)
})
})
我错过了什么?测试实际上通过了,它只是抛出了警告。事实上,以下是输出:

测试输出:

$ jest --config test/unit/jest.conf.js
 PASS  ClientApp\components\__tests__\temp.spec.ts
  Temp.vue Component
    √ renders a router-link tag with to temp.url (30ms)

  console.error node_modules\vue\dist\vue.runtime.common.js:589
    [Vue warn]: Unknown custom element: <router-link> - did you register the 
component correctly? For recursive components, make sure to provide the 
"name" option.

    (found in <Root>)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        4.677s
Ran all test suites.
Done in 6.94s.
$jest--config test/unit/jest.conf.js
通过ClientApp\components\\uuuuu测试\uuuuuu\temp.spec.ts
温度vue组件
√ 呈现指向temp.url的路由器链接标记(30毫秒)
console.error节点\u modules\vue\dist\vue.runtime.common.js:589
[Vue warn]:未知自定义元素:-您是否注册了
组件是否正确?对于递归组件,请确保提供
“名称”选项。
(位于)
测试套件:1个通过,共1个
测试:1项通过,共1项
快照:共0个
时间:4.677秒
运行所有测试套件。
6.94秒完成。

感谢你能给予的任何帮助

路由器链接
存根添加到
浅层
(或
浅层计数
)方法选项,如下所示:

const wrapper=shall(临时组件{
propsData:{temp},
存根:[“路由器链接”]
})
或:

从'@vue/test-utils'导入{RouterLinkStub};
常量包装=浅(临时组件{
propsData:{temp},
存根:{
RouterLink:RouterLink存根
}
})
执行此操作后,错误应该会消失。

对我有效:

const wrapper = shallow(TempComponent, {
  propsData: { temp },
  localVue
})
[Package.json]文件

...
"vue-jest": "^3.0.5",
"vue-router": "~3.1.5",
"vue": "~2.6.11",
"@vue/test-utils": "1.0.0-beta.29",
...
[测试]文件

import App from '../../src/App';
import { mount, createLocalVue } from '@vue/test-utils';
import VueRouter from 'vue-router';

const localVue = createLocalVue();
localVue.use(VueRouter);

const router = new VueRouter({
  routes: [
    {
      name: 'dashboard',
      path: '/dashboard'
    }
  ]
});

describe('Successful test', ()=>{
  it('works', ()=>{    
    let wrapper = mount(App, {
      localVue,
      router
    });

    // Here is your assertion
  });
});
或者您可以尝试以下方法:

您需要将路由器链接注册为存根。第二种方法对我有效。
html()
函数返回了
,无论我在哪里使用
,这在1.0.0-beta.11版中不起作用。这个解决方案是什么版本的?有没有办法让测试失败而不是输出警告消息?