Vitest Mocking modules

Viewed 24

I am migrating from jest to vitest (React application). I am having trouble with mocking modules.

From what I understood from the documentation, mocking modules is similar to jest (expect requireActual becomes importActual). But vitest keeps throwing mockFn.mockImplementaion is not a function.

My simplified test.jsx

import {vi} from "vitest"
import {submitRequestDocs} "../onboarding.api"

vi.mock('../onboarding.api',async () => {
  return {
    ...(await vi.importActual('../onboarding.api')), //tried this without await, same result.
    submitRequestDocs: vi.fn()
  };
});

describe('Request Documents',()=>{
  beforeEach(()=>{
   submitRequestDocs.mockImplementation((data)=>{
   return 'mocked'
  })

  test('renders as expected',()=>{
    expect(submitRequestDocs()).toBe('mocked') //simplified example.
  })
})

This gives - TypeError: submitRequestDocs.mockImplementation is not a function.

I reproduced this on stackblitz.

This gives similar error, but this also gives an import error asking if the module file exists.

Can someone explain why this is happening, and if my understanding / approach is right?

Also, there is one more problem I see with vitest, It keeps showing errors for a while and suddenly it passes. Is this an issue with vitest?

0 Answers
Related