例如,我有一个组件看起来像这样。
<template>
<div id="app">
<button class="my-btn" @click="increment">{{ val }}</button>
</div>
</template>
<script>
export default {
data() {
return {
val: 1,
};
},
methods: {
increment() {
fetch('httpstat.us/200').then((response) => {
this.val++;
}).catch((error) => {
console.log(error);
});
},
},
}
</script>
您还可以将此小提琴作为简化的演示进行检查
为了对这个组件进行单元测试,我写了这样的代码。
// Jest used here
test('should work', () => {
// wrapper was generated by using avoriaz's mount API
// `val` was 1 when mounted.
expect(wrapper.data().val).toBe(1);
// trigger click event for testing
wrapper.find('.my-btn')[0].trigger('click');
// `val` should be 2 when the button clicked.
// But how can I force jest to wait until `val` was set?
expect(wrapper.data().val).toBe(2);
});
expect
自从run before完成后它就不起作用fetch
了,但我不知道如何让 jest 等到 promise 函数完成。
那么如何在这个用例中进行测试呢?
更新我尝试了 nextTick 但没有运气。
PASS src/Sample.spec.js
Sample.vue
✓ should work (31ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.46s
Ran all test suites.
console.error node_modules/vue/dist/vue.runtime.common.js:477
[Vue warn]: Error in nextTick: "Error: expect(received).toBe(expected)
Expected value to be (using ===):
2
Received:
1"
console.error node_modules/vue/dist/vue.runtime.common.js:564
{ Error: expect(received).toBe(expected)
Expected value to be (using ===):
2
Received:
1
at /path/to/sample-project/src/Sample.spec.js:16:30
at Array.<anonymous> (/path/to/sample-project/node_modules/vue/dist/vue.runtime.common.js:699:14)
at nextTickHandler (/path/to/sample-project/node_modules/vue/dist/vue.runtime.common.js:646:16)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
matcherResult:
{ actual: 1,
expected: 2,
message: [Function],
name: 'toBe',
pass: false } }
这是完整的测试代码。
import { mount } from 'avoriaz';
import Sample from './Sample.vue';
import Vue from 'vue';
/* eslint-disable */
describe('Sample.vue', () => {
test('should work', () => {
const wrapper = mount(Sample);
// console.log(wrapper.data().val);
expect(wrapper.data().val).toBe(1);
wrapper.find('.my-btn')[0].trigger('click');
Vue.nextTick(() => {
// console.log(wrapper.data().val);
expect(wrapper.vm.val).toBe(2);
done();
})
});
})
更新 2我添加了done
,但它仍然无法正常工作。这是错误消息。
console.error node_modules/vue/dist/vue.runtime.common.js:477
[Vue warn]: Error in nextTick: "Error: expect(received).toBe(expected)
Expected value to be (using ===):
2
Received:
1"
console.error node_modules/vue/dist/vue.runtime.common.js:564
{ Error: expect(received).toBe(expected)
Expected value to be (using ===):
2
Received:
1
at /path/to/sample-project/src/Sample.spec.js:16:30
at Array.<anonymous> (/path/to/sample-project/node_modules/vue/dist/vue.runtime.common.js:699:14)
at nextTickHandler (/path/to/sample-project/node_modules/vue/dist/vue.runtime.common.js:646:16)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
matcherResult:
{ actual: 1,
expected: 2,
message: [Function],
name: 'toBe',
pass: false } }
FAIL src/Sample.spec.js (7.262s)
● Sample.vue › should work
Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
at pTimeout (node_modules/jest-jasmine2/build/queueRunner.js:53:21)
at Timeout.callback [as _onTimeout] (node_modules/jsdom/lib/jsdom/browser/Window.js:523:19)
at ontimeout (timers.js:469:11)
at tryOnTimeout (timers.js:304:5)
at Timer.listOnTimeout (timers.js:264:5)
Sample.vue
✕ should work (5032ms)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 8.416s
Ran all test suites.