OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

expected "spy" to be called at least once Vitest

  • Thread starter Thread starter matmik
  • Start date Start date
M

matmik

Guest
I am getting error - expected "spy" to be called at least once, when calling

Code:
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

Code:
describe('handleServerError', () => {
    it('should set error message in store and log the error', () => {
      vi.mock('common/helpers/server-error-message.js', async () => {
        const actual = await vi.importActual('common/helpers/server-error-message.js');
        return {
          ...actual,
          getErrorCodeFromEvent: () => 27001,
        };
      });
      actions.handleServerError(context);
      expect(context.commit).toHaveBeenCalledWith('setServerErrorCode', 27001);
      expect(log.warn).toHaveBeenCalled();
    });
  });

Vue store action

Code:
handleServerError(context, e) {
    log.warn(getServerErrorLogMessage('getLicenses', e));
    const code = getErrorCodeFromEvent(e);
    context.commit('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

<p>I am getting error - expected "spy" to be called at least once, when calling</p>
<pre><code>expect(log.warn).toHaveBeenCalled();
</code></pre>
<p>I do not understand why it is happening, because the function, which I am testing is calling that function <code>log.warn(.....)</code></p>
<p>Unit test</p>
<pre><code>describe('handleServerError', () => {
it('should set error message in store and log the error', () => {
vi.mock('common/helpers/server-error-message.js', async () => {
const actual = await vi.importActual('common/helpers/server-error-message.js');
return {
...actual,
getErrorCodeFromEvent: () => 27001,
};
});
actions.handleServerError(context);
expect(context.commit).toHaveBeenCalledWith('setServerErrorCode', 27001);
expect(log.warn).toHaveBeenCalled();
});
});
</code></pre>
<p>Vue store action</p>
<pre><code>handleServerError(context, e) {
log.warn(getServerErrorLogMessage('getLicenses', e));
const code = getErrorCodeFromEvent(e);
context.commit('setServerErrorCode', code);
},
</code></pre>
<p>Maybe somebody had this situation and somehow overcame it?</p>
<p>I have tried numerous different variants from vitest documentation to test this, but none helped, also haven't found solution in stackoverflow</p>
 

Latest posts

Top