admin管理员组文章数量:1279220
I am getting error - expected "spy" to be called at least once, when calling
expect(log.warn).toHaveBeenCalled();
I do not understand why it is happening, because the function, which I am testing is calling that function log.warn(.....)
Unit test
describe('handleServerError', () => {
it('should set error message in store and log the error', () => {
vi.mock('mon/helpers/server-error-message.js', async () => {
const actual = await vi.importActual('mon/helpers/server-error-message.js');
return {
...actual,
getErrorCodeFromEvent: () => 27001,
};
});
actions.handleServerError(context);
expect(contextmit).toHaveBeenCalledWith('setServerErrorCode', 27001);
expect(log.warn).toHaveBeenCalled();
});
});
Vue store action
handleServerError(context, e) {
log.warn(getServerErrorLogMessage('getLicenses', e));
const code = getErrorCodeFromEvent(e);
contextmit('setServerErrorCode', code);
},
Maybe somebody had this situation and somehow overcame it?
I have tried numerous different variants from vitest documentation to test this, but none helped, also haven't found solution in stackoverflow
I am getting error - expected "spy" to be called at least once, when calling
expect(log.warn).toHaveBeenCalled();
I do not understand why it is happening, because the function, which I am testing is calling that function log.warn(.....)
Unit test
describe('handleServerError', () => {
it('should set error message in store and log the error', () => {
vi.mock('mon/helpers/server-error-message.js', async () => {
const actual = await vi.importActual('mon/helpers/server-error-message.js');
return {
...actual,
getErrorCodeFromEvent: () => 27001,
};
});
actions.handleServerError(context);
expect(context.mit).toHaveBeenCalledWith('setServerErrorCode', 27001);
expect(log.warn).toHaveBeenCalled();
});
});
Vue store action
handleServerError(context, e) {
log.warn(getServerErrorLogMessage('getLicenses', e));
const code = getErrorCodeFromEvent(e);
context.mit('setServerErrorCode', code);
},
Maybe somebody had this situation and somehow overcame it?
I have tried numerous different variants from vitest documentation to test this, but none helped, also haven't found solution in stackoverflow
Share Improve this question asked Jan 2, 2023 at 8:49 matmikmatmik 1311 gold badge1 silver badge3 bronze badges 2- 1 Did you manage to resolve this? Getting the same error – squeekyDave Commented Oct 18, 2023 at 7:46
- Did you find any solutions? Facing the same issue. – Param Siddharth Commented Nov 16, 2023 at 13:33
2 Answers
Reset to default 3try to spy on the function before you start your test
vi.spyOn(console, 'warn');
this essentially creates a mock for that function
https://vitest.dev/api/vi.html#vi-spyon
I got the same error on one of my test cases.
eg:-
const mockFn = vi.fn();
vi.mock('path', async (importActual) => {
const actual = await importActual();
return {
...actual,
myFn: mockFn,
};
});
test('should ...', async () => {
render(<MyComponent />);
await user.click(screen.getByRole('button', { name: 'Click me' }));
expect(mockFn).toBeCalled();
});
Reason is my button hasn't clicked. Maybe a reason like it is disabled or has a pointer event none.
本文标签: javascriptexpected quotspyquot to be called at least once VitestStack Overflow
版权声明:本文标题:javascript - expected "spy" to be called at least once Vitest - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741255637a2366619.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论