web-dev-qa-db-ja.com

Vue-Test-Utils不明なカスタム要素:<router-link>

Jestを使用して、vue-test-utilsライブラリを利用したテストを実行しています。

VueRouterをlocalVueインスタンスに追加しましたが、実際にはルーターリンクコンポーネントが見つからないと表示されています。コードが少しファンキーに見える場合、それはTypeScriptを使用しているためですが、ES6にかなり近いはずです...主なことは、@ Prop()がプロップを渡すことと同じであることです:{..}

Vueコンポーネント:

<template>
  <div>
    <div class="temp">
      <div>
        <router-link :to="temp.url">{{temp.name}}</router-link>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import Component from 'vue-class-component'
import { Prop } from 'vue-property-decorator'
import { Temp } from './Temp'

@Component({
  name: 'temp'
})
export default class TempComponent extends Vue {
  @Prop() private temp: Temp
}
</script>

<style lang="scss" scoped>
.temp {
  padding-top: 10px;
}
</style>

温度モデル:

export class Temp {
  public static Default: Temp = new Temp(-1, '')

  public url: string

  constructor(public id: number, public name: string) {
    this.id = id
    this.name = name
    this.url = '/temp/' + id
  }
}

Jestテスト

import { createLocalVue, shallow } from '@vue/test-utils'
import TempComponent from '@/components/Temp.vue'
import { Temp } from '@/components/Temp'
import VueRouter from 'vue-router'

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

describe('Temp.vue Component', () => {
  test('renders a router-link tag with to temp.url', () => {
    const temp = Temp.Default
    temp.url = 'http://some-url.com'

    const wrapper = shallow(TempComponent, {
      propsData: { temp }
    })
    const aWrapper = wrapper.find('router-link')
    expect((aWrapper.attributes() as any).to).toBe(temp.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.

あなたが与えることができるどんな助けにも感謝します!

19
Chad Carter

router-link次のようなshallow(またはshallowMount)メソッドオプションへのスタブ:

const wrapper = shallow(TempComponent, {
     propsData: { temp },
     stubs: ['router-link']
})

または this way

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

const wrapper = shallow(TempComponent, {
     propsData: { temp },
     stubs: {
         RouterLink: RouterLinkStub
     }
})

これを実行すると、エラーは解消されます。

43
const wrapper = shallow(TempComponent, {
  propsData: { temp },
  localVue
})
0
wenfu

私のために働いた:

[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
  });
});

またはこれを試すことができます: enter image description here

0
Elmatsidis Paul