假设我有以下组件:
import { mapState } from 'vuex';
import externalDependency from '...';
export default {
name: 'Foo',
computed: {
...mapState(['bar'])
},
watch: {
bar () {
externalDependency.doThing(this.bar);
}
}
}
测试时,我想确保externalDependency.doThing()
调用它bar
(来自 vuex 状态),如下所示:
it('should call externalDependency.doThing with bar', () => {
const wrapper = mount(Foo);
const spy = jest.spyOn(externalDependency, 'doThing');
wrapper.setComputed({bar: 'baz'});
expect(spy).toHaveBeenCalledWith('baz');
});
Vue test-utils 有一个 setComputed 方法,可以让我当前测试它,但我不断收到警告说 setComputed 将很快被删除,我不知道还有什么方法可以测试它: