Test Structure
Each recipe includes atests/ directory with two subdirectories:
Writing Tests
Create test input files
Add files to
tests/input/ representing code patterns your recipe should handle:tests/input/file-0.js
tests/input/file-1.mjs
Create expected output files
Add corresponding files to
tests/expected/ with the correct transformations:tests/expected/file-0.js
tests/expected/file-1.mjs
File names in
input/ and expected/ must match exactly.Test Coverage
Your tests should cover:Import/Require Variations
Test all common import patterns:Edge Cases
No-op Cases
Test that your recipe doesn’t modify files that don’t need changes:tests/input/no-changes.js
tests/expected/no-changes.js
Unit Testing (Advanced)
For complex recipes with helper functions, create unit tests alongside your code:src/my-helper.test.ts
Integration Testing
The default test command runs integration tests using the jssg test runner:package.json
- Loads your transformation from
src/workflow.ts - Applies it to files in
tests/input/ - Compares results against
tests/expected/ - Reports any differences
Test Fixtures
When testing file system operations or complex scenarios, use fixtures:Running Tests Locally
Before Committing
Always run the full test suite before committing:- Linting with auto-fix
- Type checking
- All tests
Individual Test Commands
CI/CD Testing
All tests run automatically in GitHub Actions on:- Every push to any branch
- Every pull request
- Multiple Node.js versions (22+)
- Multiple operating systems (Ubuntu, macOS, Windows)
.github/workflows/ci.yml
Test Warnings
Debugging Failed Tests
When a test fails:Verify the pattern
Test your AST pattern in Codemod Studio:
- Visit Codemod Studio
- Paste your code sample
- Test your pattern matching
Best Practices
Test multiple file extensions
Test multiple file extensions
Include tests for
.js, .mjs, .cjs, .ts, .tsx, etc.Test realistic code patterns
Test realistic code patterns
Use real-world examples from actual codebases, not just simple cases.
Test partial imports
Test partial imports
Ensure transformations work when only some imports need updating:
Keep tests focused
Keep tests focused
Each test file should test one specific pattern or edge case.
Name tests descriptively
Name tests descriptively
Use clear names that indicate what’s being tested:
Next Steps
Development Workflow
Learn about the development workflow and PR process