我遇到了问题。当我挂载组件Jest而不是等待解决promises.fetch API不在生命周期挂载中执行时。我尝试了flushPromises但无法正常工作。我也尝试过mocha但存在相同的问题。
describe('HelloWorld.vue', () => {
it('should render correct contents', async () => {
try {
const wrapper = mount(HelloWorld, {
store,
localVue,
})
await wrapper.vm.$nextTick()
await flushPromise()
expect(wrapper.contains('.hellonew')).toEqual(true)
} catch (error) {
console.log(error)
}
})
afterEach(() => {
fetchMock.restore()
})
})
我的组件包含生命周期,可以从API获取数据并将其存储
import { mapGetters, mapActions } from "vuex";
export default {
name: "HelloWorld",
data() {
return {
msg: "Welcome to Your Vue.js App",
loading: false
};
},
methods: {
...mapActions(["setData"])
},
mounted() {
this.setData();
},
computed:{
...mapGetters(['getData']),
gg(){
if(this.getData){
console.log('99999')
}
}
}
};