【问题标题】:Tests pass when ran one by one but test suite throws an error测试通过一个一个运行,但测试套件抛出一个错误
【发布时间】:2019-03-22 12:52:31
【问题描述】:

我正在尝试测试表单组件的行为。我有以下测试套件:

import {createLocalVue, mount} from '@vue/test-utils'
import formDiscovery from '../pages/Discovery'
import Vuex from 'vuex'
import Buefy from 'buefy'
import VeeValidate, {Validator} from 'vee-validate'
import * as moduleContactInfos from '../store/contactInfos'
import * as moduleCompanyInfos from '../store/companyInfos'
import fr from 'vee-validate/dist/locale/fr'

let wrapper, store
beforeEach(() => {
  const localVue = createLocalVue()
  localVue.use(Vuex)
  localVue.use(VeeValidate)
  Validator.localize('fr', fr)
  localVue.use(Buefy)
  store = new Vuex.Store({
    modules: {
      contactInfos: moduleContactInfos,
      companyInfos: moduleCompanyInfos
    }
  })
  wrapper = mount(formDiscovery, {
    localVue,
    store,
    stubs: ['router-link']
  });
})

/*
    This test suite tests if a field is shown when the user selects a specific option in a select input
    and that this field is hidden when any other option is selected
*/
describe("field 'more details'", () => {
  it("is shown when the 'Other: specify' select option is selected", () => {
    wrapper.vm.$data.choiceCompanyInfos = 'Other: specify'
    wrapper.vm.$nextTick(() => {
      const input = wrapper.find('#moreDetails')
      expect(input.isVisible()).toBeTruthy()
    })
  })

  it('is hidden when a valid select option is selected', () => {
    wrapper.vm.$data.choiceCompanyInfos = 'A valid select option'
    wrapper.vm.$nextTick(() => {
      const input = wrapper.find('#moreDetails')
      expect(input.isVisible()).toBeFalsy()
    })
  })
})

/*
    This test suite tests is veevalidate generates the correct error messages
*/
describe('vee validate', () => {

  it('adds an error when a required form field is empty', async () => {
    const input = wrapper.find('[name="phone"]')
    expect(input.exists()).toBe(true)
    expect(wrapper.vm.errors.count()).toBe(0)
    store.commit('contactInfos/updatePhone', '')
    await wrapper.vm.$validator.validate('phone')
    expect(wrapper.vm.errors.count()).toBe(1)
  })

  it('adds an error when the phone has an invalid format', async () => {
    const input = wrapper.find('[name="phone"]')
    expect(input.exists()).toBe(true)
    expect(wrapper.vm.errors.count()).toBe(0)
    store.commit('contactInfos/updatePhone', '6156') // Valid format : 0?[0-9]{9}
    await wrapper.vm.$validator.validate('phone')
    expect(wrapper.vm.errors.count()).toBe(1)
    expect(wrapper.vm.errors.first('phone').indexOf('invalid')).toBeGreaterThan(-1) // The error message must contains 'invalid'
  })
})

被测组件包含多个嵌套子组件(表单域包含buefy标签和buefy输入等) 它还包含两个 nuxt-link 组件。

当我单独运行测试时,它可以工作。但是,如果尝试运行描述套件或整个文件,则会出现以下错误:

TypeError: Cannot read property '$scopedSlots' of undefined
    at updateChildComponent (.\node_modules\vue\dist\vue.common.dev.js:4100:27)
    at prepatch (.\node_modules\vue\dist\vue.common.dev.js:3128:5)

我尝试为每个测试重新创建存储,为每个测试创建一个新的 localVue 实例。没有任何工作。有什么想法吗?

【问题讨论】:

  • 想知道你找到了如何解决这个问题,我现在和你的情况一样..

标签: vue.js jestjs nuxt.js vue-test-utils


【解决方案1】:

我遇到了同样的错误,并在安装组件时使用sync: false 解决了它。所以在你的情况下,只需按照下面的代码。

 wrapper = mount(formDiscovery, {
    sync: false,
    localVue,
    store,
    stubs: ['router-link']
 });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-13
    • 2012-12-21
    • 1970-01-01
    • 2016-02-20
    • 2017-12-27
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多