![](/img/trans.png)
[英]Your test Suite must contain at least one test ( Refuses to count expect as a test when it passes )
[英]Tests pass when ran one by one but test suite throws an error
我正在嘗試測試表單組件的行為。 我有以下測試套件:
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實例。 沒事 有任何想法嗎 ?
我遇到了同樣的錯誤,並在安裝組件時使用sync: false
解決了它。 因此,就您而言,請遵循以下代碼。
wrapper = mount(formDiscovery, {
sync: false,
localVue,
store,
stubs: ['router-link']
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.