trivikr / vitest-codemod

Codemod scripts to migrate your JavaScript unit tests to vitest
MIT License
23 stars 2 forks source link

[Feature]: Transform `jest.mock` of paths using `moduleNameMapper` #139

Open billdybas opened 1 year ago

billdybas commented 1 year ago

Self-service

Template name

jest

Input code

jest.mock('src/example', () => {
  return {
    example: jest.fn().mockImplementation(() => {
      return {};
    }),
  };
});

Expected Output

import { vi } from 'vitest';

vi.mock('src/example', () => {
  return {
    example: vi.fn().mockImplementation(() => {
      return {};
    }),
  };
});

Additional context

https://jestjs.io/docs/configuration#modulenamemapper-objectstring-string--arraystring

Here's an example that allows importing from src:

moduleNameMapper: {
  '^src/(.*)$': '<rootDir>/src/$1',
},

Currently files with mocks like this are skipped w/ a jscodeshift error like Cannot find module 'src/__tests__/src/example'.

I believe that might be coming from: https://github.com/trivikr/vitest-codemod/blob/fd7f89d395c61900d5df240b0383209cdf47bff7/packages/jest/src/apis/updateDefaultExportMocks.ts#L31