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.0
- babel-jest: ^30.0.0
- ts-jest: ^30.0.0
- jest-environment-jsdom: ^30.0.0
-
Migrates Test Files: Replaces deprecated matcher aliases:
- toBeCalled → toHaveBeenCalled
- toBeCalledTimes → toHaveBeenCalledTimes
- toBeCalledWith → toHaveBeenCalledWith
- lastCalledWith → toHaveBeenLastCalledWith
- nthCalledWith → toHaveBeenNthCalledWith
- toReturn → toHaveReturned
- toReturnTimes → toHaveReturnedTimes
- toReturnWith → toHaveReturnedWith
- lastReturnedWith → toHaveLastReturnedWith
- nthReturnedWith → toHaveNthReturnedWith
- toThrowError → toThrow
-
Updates Jest Config: Ensures configuration is compatible with Jest 30+
-
Formats Code: Runs Prettier (if available) to format migrated code