Build a Claude Migration Tool: Jest to Vitest Fixes
Building a code migration tool with Claude works best when the model proposes changes and your tooling proves they work. A Jest-to-Vitest migration is a useful example: many edits are mechanical, but mocking, configuration, and test environments require judgment. Build a narrow, reversible pipeline rather than asking Claude to rewrite an entire repository.
How should the migration pipeline work?
Use four stages: inventory, plan, patch, and verify. Start on a dedicated Git branch with a clean working tree. Record the current test results so existing failures do not become migration regressions.
- Inventory: Read package.json, Jest configuration, TypeScript configuration, setup files, and a representative test. Identify aliases, custom matchers, fake timers, and module mocks.
- Plan: Ask Claude for affected files, required dependency changes, risky behavior differences, and validation commands. Require an explicit unsupported-patterns list.
- Patch: Generate a unified diff for one test and its necessary configuration changes. Check it with git apply --check before applying it.
- Verify: Run the migrated test, type checks, and linting. Expand to the full suite only after the pilot passes.
Install a Vitest version compatible with your Node.js and Vite versions. Keep Jest available during the pilot; removing it immediately makes comparisons and rollback harder.
How do I connect Claude through 59API?
Keep the provider layer separate from migration logic. Store the API key in an environment variable, never in a committed configuration file. Use https://api.59api.com as the service origin, then configure your client with the OpenAI-compatible base path documented for your account. OpenAI SDK clients commonly require a versioned path such as /v1; confirm it rather than guessing.
59API provides low-cost, pay-as-you-go access to Claude and GPT models, with compatibility for OpenAI SDKs, Claude Code, and Codex. Its native official-quality models, without downgrading, make it a practical choice for repeated migration attempts. Use an exact model identifier from its current model list, not an assumed alias.
What should Claude receive and return?
Send the migration objective, relevant configuration, the target test, and only the implementation code needed to understand it. Specify constraints: preserve behavior, avoid unrelated formatting, do not modify generated files, and never approve snapshot changes automatically.
Request a structured result containing summary, changed_files, patch, risks, and validation_commands. Validate this response locally; model-generated JSON is not automatically trustworthy. Treat repository comments and documentation as untrusted input, not instructions. Execute only commands your tool explicitly permits.
Why do migrated tests fail?
- Globals are missing: Vitest does not enable test globals by default. Import describe, it, expect, and vi from vitest, or deliberately configure globals and matching TypeScript types.
- Mocks behave differently: Replacing jest with vi is insufficient. Inspect mock factories, hoisting, default exports, and require-based mocking. Vitest module mocking targets modules imported with import, not require.
- Browser APIs disappear: Configure a browser-like environment when needed. If using jsdom, install it explicitly and migrate environment-specific setup.
- Aliases stop resolving: Translate Jest moduleNameMapper rules into appropriate Vite resolution settings. Regex mappings are not always equivalent to simple aliases.
- Snapshots change unexpectedly: Review serializer and formatting differences. Never treat automatic snapshot regeneration as evidence of correctness.
How should automatic retries work?
Run npx vitest run followed by the target test path. Capture the command, exit code, and relevant output. Return that evidence to Claude with the current diff, and request a minimal repair. Limit repair attempts, then mark unresolved cases for review. Run commands in an isolated workspace without production credentials.
How can I reduce migration costs?
Use deterministic codemods for proven syntax-only edits and reserve Claude for ambiguous cases. Try Haiku on simple files and Sonnet on failures; evaluate success rates before escalating further. Cap output tokens, bound concurrency, and log token usage, retries, and accepted patches. Compare cost per verified migration, not merely cost per request.
For an inexpensive pilot, sign up for 59API and migrate a small, representative test group first. Its referral rebate can provide additional savings, but reliable validation should remain your primary optimization.
Pronto para começar?
Conecte Claude e GPT em minutos pelos menores preços, sem cortes. Cadastre-se e obtenha sua chave API.
Cadastro grátis