[英]How to test Vue Component method call within an async method
I believe I am struggling to properly mock my methods here.我相信我正在努力在这里正确地嘲笑我的方法。 Here is my situation, I have a component with two methods;这是我的情况,我有一个有两种方法的组件;
name: 'MyComponent',
methods: {
async submitAction(input) {
// does await things
// then ...
this.showToastMessage();
},
showToastMessage() {
// does toast message things
},
}
And I want to write a test that will assert that showToastMessage()
is called when submitAction(input)
is called.我想编写一个测试来断言showToastMessage()
在调用submitAction(input)
时被调用。 My basic test looking something like this;我的基本测试看起来像这样;
test('the toast alert method is called', () => {
let showToastMessage = jest.fn();
const spy = jest.spyOn(MyComponent.methods, 'showToastMessage');
const wrapper = shallowMount(MyComponent, { localVue });
const input = // some input data
wrapper.vm.submitAction(input); // <--- this calls showToastMessage
expect(spy).toHaveBeenCalled();
};
NOTE: localVue is declare as such at the top of the file const localVue = createLocalVue();
注意:localVue 在文件顶部声明为const localVue = createLocalVue();
I confirmed that both submitAction()
and showToastMessage()
methods are being called during the tests, by sneaking a couple of console.log()'s and observing it in the test output, however the test still fails;我确认在测试期间调用了submitAction()
和showToastMessage()
方法,方法是偷偷摸摸几个 console.log() 并在测试 output 中观察它,但是测试仍然失败;
expect(jest.fn()).toHaveBeenCalledWith(...expected)
Expected: called with 0 arguments
Number of calls: 0
566 | const wrapper = shallowMount(MyComponent, { localVue } );
567 | wrapper.vm.submitAction(input);
> 568 | expect(spy).toHaveBeenCalledWith();
I've tried spying on both methods as well我也试过监视这两种方法
const parentSpy = jest.spyOn(MyComponent.methods, 'submitAction');
const spy = jest.spyOn(MyComponent.methods, 'showToastMessage');
// ...
expect(spy).toHaveBeenCalled()
same results, test fail.同样的结果,测试失败。
What am I missing?我错过了什么?
Tech Stack: vue 3, jest, node 14技术堆栈:vue 3、jest、node 14
@TekkSparrow you can pass a heap of stuff into the shallowMount
function. It accepts an object as a second argument which can look something like @TekkSparrow 你可以将一堆东西传递给shallowMount
function。它接受 object 作为第二个参数,它看起来像
import { shallowMount, createLocalVue } from '@vue/test-utils'
import Vuex from 'vuex'
const localVue = createLocalVue()
localVue.use(Vuex)
let mocks = {
// this could be something like the below examples
// I had in a previous project
$route: {
query: '',
path: '/some-path'
},
$router: [],
$validator: {
validateAll: jest.fn()
},
$toast: {
show: jest.fn(),
error: jest.fn()
},
}
let propsData = {
// some props you want to overwrite or test.
// needs to be called propsData
}
let methods = {
showToastMessage: jest.fn()
}
let store = new Vuex.Store({
actions: {
UPLOAD_ASSET: jest.fn(),
},
})
const wrapper = shallowMount(MyComponent, { mocks, propsData, methods, store, localVue })
I believe that by doing similar to the above, your mocked function will run and be recorded by the Jest spy.我相信通过执行与上述类似的操作,您模拟的 function 将会运行并被 Jest 间谍记录下来。
Took me a minute to realize/try this, but looks like since my calling function is async that I was suppose to make my test async, and await the main method call.我花了一分钟才意识到/尝试这个,但看起来因为我的调用 function 是异步的,所以我应该让我的测试异步,并等待主方法调用。 This seems to have done the trick.这似乎已经成功了。 Here's what ended up being my solution:这是我最终的解决方案:
test('the toast alert method is called', async () => {
let showToastMessage = jest.fn();
const spy = jest.spyOn(MyComponent.methods, 'showToastMessage');
const wrapper = shallowMount(MyComponent, { localVue });
const input = // some input data
await wrapper.vm.submitAction(input);
expect(spy).toHaveBeenCalled();
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.