npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@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.

Features

This codemod performs the following transformations:

Jest API to Vitest API

  • jest.mock()vi.mock() (with proper async handling for requireActual)
  • 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/globals imports
  • 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.ts if it doesn't exist
  • Updates package.json dependencies (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-codemod

Or using pnpm:

pnpm add @kamaalio/jest-to-vitest-codemod

Usage

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 ./src

Advanced 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.importActual is 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 coverage

Building

pnpm build         # Build the package
pnpm dev          # Build in watch mode

Code Quality

pnpm lint         # Lint code
pnpm format       # Format code
pnpm type-check   # Type check
pnpm quality      # Run all quality checks

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

Development Setup

  1. Clone the repository
  2. Install dependencies: pnpm install
  3. Run tests: pnpm test
  4. Make your changes and ensure tests pass
  5. Submit a pull request

License

This project is licensed under the MIT License.