简体   繁体   English

使用 vue-test-utils 时,“名称为‘某物’的路由不存在”vue-router console.warn

[英]'Route with name 'something' does not exist' vue-router console.warn when using vue-test-utils

I'm making test codes for vue.js component which uses vue-router using vue-test-utils.我正在为使用 vue-test-utils 使用 vue-router 的 vue.js 组件制作测试代码。

Here are codes related to this.以下是与此相关的代码。

const Routes = [{
  ...
 }, ...
 {
  ...,
  path: '/transfers',
  name: 'transfers',
  redirect: '/transfers/all',
  ...
 },...]

Routes.ts路由文件

export default class Transfers extends Vue {
  ...
  @Watch('$route', { immediate: true })
  async setDataResource (value: any) {
    const routeParams = value.params
    const paramKey: string = Object.keys(routeParams)[0]
    ...
  }
}

Transfers.vue Transfers.vue

import { shallowMount, createLocalVue } from '@vue/test-utils'
import VueRouter from 'vue-router'
import Routes from '../../router/Routes'

const localVue = createLocalVue()
localVue.use(VueRouter)
...
const router = new VueRouter({ Routes })

...
describe('Elements', () => {
  const div = document.createElement('div')
  div.id = 'root'
  document.body.appendChild(div)

  router.push({ name: 'transfers', params: { processing: 'all' } })

  const wrapper = shallowMount(Transfers, {
    localVue,
    store,
    router,
    attachTo: '#root'
  })
  ...
})

transfers.test.js transfers.test.js

What I wanna do is to test watch on $route with params.我想做的是使用参数在 $route 上测试手表。

It works well with 'params: { processing: 'all' }' but它适用于 'params: { processing: 'all' }' 但是

console.warn
      [vue-router] Route with name 'transfers' does not exist

appears.出现。

I can use router.push({ name: 'transfers' }) in .vue, so I think the name is registered to routes normally.我可以在 .vue 中使用 router.push({ name: 'transfers' }) ,所以我认为该名称已正常注册到路由。 But only for testing, 'Route with name 'transfers'' seems to not found.但仅用于测试,似乎找不到“名称为 'transfers'”的路由。

The Routes.ts file originally didn't use name options for routes, but I added it since I can push params to router only using name option. Routes.ts 文件最初没有为路由使用名称选项,但我添加了它,因为我只能使用名称选项将参数推送到路由器。 I mean, name options are not necessary for our project!我的意思是,我们的项目不需要名称选项!

So my question is,所以我的问题是,

  • Is there any way to push params to router in test code, without using name option in route?有没有办法在测试代码中将参数推送到路由器,而无需在路由中使用 name 选项?
  • If I should use name option, why does this warning only appears when testing?如果我应该使用 name 选项,为什么这个警告只在测试时出现?

I did a lot of testing in the last project I was working on.我在我从事的最后一个项目中做了很多测试。 I always mocked my $route and $router (see snippet).我总是嘲笑我的 $route 和 $router (见代码段)。 I've created a Codepen (includes the whole file) of a page I was testing.我创建了一个我正在测试的页面的 Codepen(包括整个文件)。 It shows my basic setup and some ways I was testing the routing object.它显示了我的基本设置以及我测试路由对象的一些方法。 ie IE

let mocks = {
  $route: {
    path: '/some-path',
    query: {
      amount: null,
      loanType: null
    }
  },
  $router: [],
  $validator: {
    validateAll: jest.fn()
  },
  $toast: {
    show: jest.fn(),
    error: jest.fn()
  },
  $withProcessing: jest.fn()
}

It's a little hard to exactly diagnose your prob without jumping into the code but I hope you get something out of the below that helps you out.在不跳入代码的情况下准确诊断您的问题有点困难,但我希望您从下面的内容中得到一些帮助。

https://codepen.io/RuNpiXelruN/pen/XWRKmoE https://codepen.io/RunNpiXelruN/pen/XWRKmoE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM