测试时如何嵌套Vue.nextTick?
How to nest Vue.nextTick when testing?
我正在尝试测试我的应用程序在连续两次 tick 中发生的情况。这是我目前所拥有的(测试在 karma devtools 中失败,但在命令行中失败):
import { mount } from 'avoriaz';
import MyComponent from './MyComponent';
describe('testing', function() {
it('should do something', (done) => {
const wrapper = mount(MyComponent, { store });
wrapper.vm.changeData();
Vue.nextTick(() => {
expect(wrapper.vm.something).to.eql(somethingElse);
wrapper.vm.changeData();
Vue.nextTick(() => {
expect(wrapper.vm.something2).to.eql(somethingElse2);
done();
});
done();
});
});
});
我也尝试过使用 then()
和 catch()
,但 karma 仍然认为我失败的测试通过了。
我应该只接一个 done()
电话吗?我不太确定这个回调在做什么。
如 here 所示,有一个更好的解决方案,可以防止 Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
:
it('should do something', (done) => {
const wrapper = mount(MyComponent, { store });
wrapper.vm.changeData();
Vue.nextTick(() => {
expect(wrapper.vm.something).to.eql(somethingElse);
wrapper.vm.changeData();
Vue.nextTick().then(() => {
expect(wrapper.vm.something2).to.eql(somethingElse2);
}).then(done, done);
});
});
我也想使用 async/await
版本,但我无法使用它。
我正在尝试测试我的应用程序在连续两次 tick 中发生的情况。这是我目前所拥有的(测试在 karma devtools 中失败,但在命令行中失败):
import { mount } from 'avoriaz';
import MyComponent from './MyComponent';
describe('testing', function() {
it('should do something', (done) => {
const wrapper = mount(MyComponent, { store });
wrapper.vm.changeData();
Vue.nextTick(() => {
expect(wrapper.vm.something).to.eql(somethingElse);
wrapper.vm.changeData();
Vue.nextTick(() => {
expect(wrapper.vm.something2).to.eql(somethingElse2);
done();
});
done();
});
});
});
我也尝试过使用 then()
和 catch()
,但 karma 仍然认为我失败的测试通过了。
我应该只接一个 done()
电话吗?我不太确定这个回调在做什么。
如 here 所示,有一个更好的解决方案,可以防止 Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
:
it('should do something', (done) => {
const wrapper = mount(MyComponent, { store });
wrapper.vm.changeData();
Vue.nextTick(() => {
expect(wrapper.vm.something).to.eql(somethingElse);
wrapper.vm.changeData();
Vue.nextTick().then(() => {
expect(wrapper.vm.something2).to.eql(somethingElse2);
}).then(done, done);
});
});
我也想使用 async/await
版本,但我无法使用它。