sodatea / vite-jest

First-class Vite integration for Jest
MIT License
401 stars 51 forks source link

Cannot use jest.mock #25

Closed martpet closed 2 years ago

martpet commented 2 years ago

Can you use jest.mock ?

With this:

import { myFunc } from '~/api';
jest.mock('~/api');

I get: Cannot find module '~/api' from 'jest.setup.js'

If I change it: jest.mock('./src/api');

The error is gone, but myFunc is not mocked.

Here is my code: https://github.com/martpet/vite-play/blob/Add-tests/src/store/features/translations/translationsThunks.test.ts

breytex commented 2 years ago

What was the issue here? @martpet

martpet commented 2 years ago

jest.mock doesn't work with ES modules, but there is now jest.unstable_mockModule which is supposed to work.