LoginSignup
1
1

More than 5 years have passed since last update.

VeeValidateを使用したコンポーネントのユニットテストをする方法

Posted at

はじめに

VeeValidateVue Test Utils でテストができるようにしましたので備忘録として投稿します。

VeeValidateVue Test Utils でテストする方法

VeeValidateVue Test Utils でテストするとき下記のように xxx.spec.jp を作ります。

import Vuex from 'vuex'
import VeeValidate, { Validator } from 'vee-validate'
import flushPromises from 'flush-promises'
import Form from '@/components/Form.vue'

const localVue = createLocalVue()
localVue.use(Vuex)
localVue.use(VeeValidate)

describe('Form', () => {
  it('The name field is required', async () => {
    const wrapper = shallowMount(EntryForm, {
      localVue
    })

    wrapper.find('[name="name"]').setValue('')
    wrapper.vm.$validator.validateAll()
    await flushPromises()
    expect(wrapper.vm.errors.first('name')).to.equal('The name field is required.')
  })
})

おわりに

VeeValidateはフォームに入力してからエラーメッセージが表示されるまでタイムラグがあります。
初めは非同期でやっていたためにエラーメッセージが表示される前にテストが終わってエラーになってしまいました。
async , await flushPromises() を追記することで解決しました。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1