Jest Latest Migration Codemod
A Codemod agent for migrating Jest tests to the latest version (v30+) and updating related packages. This agent handles:
- ✅ Matcher alias migrations (e.g.,
toBeCalled→toHaveBeenCalled) - ✅ Jest package updates to latest versions
- ✅ Jest configuration updates for compatibility
- ✅ Code formatting after migration
Prerequisites
- Node.js >= 18.0.0
- npm or yarn
- Codemod CLI installed globally:
npm install -g codemod
Quick Start
Step 1: Run the Codemod
bash
Or run on a specific directory:
bash
Step 2: Install Dependencies
After running the codemod, install the updated packages:
bash
Or use your preferred package manager:
bash
Step 3: Update Test Snapshots
Run your tests and update snapshots:
bash
Or if your test command is different:
bash
What It Does
-
Updates package.json: Updates all Jest-related packages to latest versions:
jest: ^30.0.0@types/jest: ^30.0.0babel-jest: ^30.0.0ts-jest: ^30.0.0jest-environment-jsdom: ^30.0.0
-
Migrates Test Files: Replaces deprecated matcher aliases:
toBeCalled→toHaveBeenCalledtoBeCalledTimes→toHaveBeenCalledTimestoBeCalledWith→toHaveBeenCalledWithlastCalledWith→toHaveBeenLastCalledWithnthCalledWith→toHaveBeenNthCalledWithtoReturn→toHaveReturnedtoReturnTimes→toHaveReturnedTimestoReturnWith→toHaveReturnedWithlastReturnedWith→toHaveLastReturnedWithnthReturnedWith→toHaveNthReturnedWithtoThrowError→toThrow
-
Updates Jest Config: Ensures configuration is compatible with Jest 30+
-
Formats Code: Runs Prettier (if available) to format migrated code