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
Add a ment  | 

2 Answers 2

Reset to default 3

try 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