@kamaalio/jest-to-vitest-codemod
v0.0.17
Published
`@kamaalio/jest-to-vitest-codemod` is a comprehensive collection of codemods to help migrate from Jest to Vitest. It automatically transforms Jest APIs, imports, types, and project configuration to Vitest equivalents.
Downloads
40
Readme
Jest to Vitest codemod
@kamaalio/jest-to-vitest-codemod is a comprehensive collection of codemods to help migrate from Jest to Vitest. It automatically transforms Jest APIs, imports, types, and project configuration to Vitest equivalents.
- Jest to Vitest codemod
Features
This codemod performs the following transformations:
Jest API to Vitest API
jest.mock()→vi.mock()(with proper async handling forrequireActual)jest.spyOn()→vi.spyOn()jest.requireActual()→await vi.importActual()(automatically wraps functions as async)jest.setTimeout()→vi.setTimeout({ testTimeout: ... })jest.clearAllMocks()→vi.clearAllMocks()jest.resetAllMocks()→vi.resetAllMocks()jest.restoreAllMocks()→vi.restoreAllMocks()jest.useFakeTimers()→vi.useFakeTimers()jest.useRealTimers()→vi.useRealTimers()
Hook Transformations
- Converts single-expression hooks to block statements for Vitest compatibility
beforeEach(() => setup())→beforeEach(() => { setup() })
Type Transformations
jest.Mock<T>→Mock<T>vi.Mock<T>→Mock<T>
Import Management
- Removes
@jest/globalsimports - Automatically adds appropriate Vitest imports (
describe,it,expect,vi, etc.) - Handles both type and value imports correctly
- Optimizes import statements by combining related imports
Project Configuration
- Creates
vitest.config.tsif it doesn't exist - Updates
package.jsondependencies (removes Jest deps, adds Vitest) - Removes Jest configuration files
Requirements
- Node.js ≥ 22.0.0
- TypeScript files (currently supports TypeScript syntax)
Installation
npm install @kamaalio/jest-to-vitest-codemodOr using pnpm:
pnpm add @kamaalio/jest-to-vitest-codemodUsage
Basic String Transformation
import jestToVitest from '@kamaalio/jest-to-vitest-codemod';
const jestCode = `
import { describe, it, expect } from '@jest/globals';
jest.mock('fs');
describe('my test', () => {
beforeEach(() => setup());
it('should work', () => {
const spy = jest.spyOn(console, 'log');
expect(true).toBe(true);
jest.clearAllMocks();
});
});
`;
const vitestCode = await jestToVitest(jestCode);
console.log(vitestCode);Output:
import { beforeEach, describe, expect, it, vi } from 'vitest';
vi.mock('fs');
describe('my test', () => {
beforeEach(() => {
setup();
});
it('should work', () => {
const spy = vi.spyOn(console, 'log');
expect(true).toBe(true);
vi.clearAllMocks();
});
});CLI
Run the codemod over a directory directly from the command line:
npx jest-to-vitest-codemod ./srcAdvanced Usage with Codemod Framework
import { JEST_TO_VITEST_CODEMOD, JEST_TO_VITEST_LANGUAGE } from '@kamaalio/jest-to-vitest-codemod';
// Use with @kamaalio/codemod-kit or similar frameworks
const codemod = JEST_TO_VITEST_CODEMOD;File Processing with Filename Context
import jestToVitest from '@kamaalio/jest-to-vitest-codemod';
import { parseAsync } from '@ast-grep/napi';
import { JEST_TO_VITEST_LANGUAGE } from '@kamaalio/jest-to-vitest-codemod';
const fileContent = await fs.readFile('test.spec.ts', 'utf-8');
const ast = await parseAsync(JEST_TO_VITEST_LANGUAGE, fileContent);
const result = await jestToVitest(ast, 'test.spec.ts');Before/After Examples
Complex Jest Test File
Before:
import { describe, it, expect, beforeEach } from '@jest/globals';
jest.mock('./utils', () => ({
...jest.requireActual('./utils'),
helper: jest.fn(),
}));
describe('Component', () => {
let mockFn: jest.Mock<(x: number) => string>;
beforeEach(() => jest.clearAllMocks());
it('should handle mocks', () => {
const spy = jest.spyOn(console, 'log');
jest.setTimeout(10000);
expect(true).toBe(true);
});
});After:
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
vi.mock('./utils', async () => ({
default: {
...(await vi.importActual('./utils')),
helper: vi.fn(),
},
}));
describe('Component', () => {
let mockFn: Mock<(x: number) => string>;
beforeEach(() => {
vi.clearAllMocks();
});
it('should handle mocks', () => {
const spy = vi.spyOn(console, 'log');
vi.setTimeout({ testTimeout: 10000 });
expect(true).toBe(true);
});
});API Reference
Default Export
function jestToVitest(content: string | SgRoot, filename?: string): Promise<string>;Transforms Jest code to Vitest. Accepts either a string of code or a parsed AST from @ast-grep/napi.
Named Exports
// Codemod configuration for use with codemod frameworks
export const JEST_TO_VITEST_CODEMOD: Codemod;
// AST language configuration (TypeScript)
export const JEST_TO_VITEST_LANGUAGE: Lang;
// Low-level transformation function
export function jestToVitestModifications(modifications: Modifications): Promise<Modifications>;Behavior and Limitations
- File Detection: Only processes files that contain Jest global APIs (
describe,it,expect, etc.) - Language Support: Currently supports TypeScript syntax only
- Async Handling: Automatically converts functions to async when
vi.importActualis used - Import Optimization: Intelligently merges and organizes Vitest imports
- Project Setup: Post-transformation hooks set up Vitest configuration and dependencies
Development
Running Tests
pnpm test # Run tests once
pnpm test:watch # Run tests in watch mode
pnpm test:cov # Run tests with coverageBuilding
pnpm build # Build the package
pnpm dev # Build in watch modeCode Quality
pnpm lint # Lint code
pnpm format # Format code
pnpm type-check # Type check
pnpm quality # Run all quality checksContributing
Contributions are welcome! Please open an issue or submit a pull request.
Development Setup
- Clone the repository
- Install dependencies:
pnpm install - Run tests:
pnpm test - Make your changes and ensure tests pass
- Submit a pull request
License
This project is licensed under the MIT License.
