Skip to main content
Testing is critical for ensuring migration recipes work correctly across different code patterns and edge cases.

Test Structure

Each recipe includes a tests/ directory with two subdirectories:

Writing Tests

1

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
2

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

Run the tests

Execute tests using the test script:

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
The expected output should be identical:
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
Run unit tests:

Integration Testing

The default test command runs integration tests using the jssg test runner:
package.json
This command:
  1. Loads your transformation from src/workflow.ts
  2. Applies it to files in tests/input/
  3. Compares results against tests/expected/
  4. Reports any differences

Test Fixtures

When testing file system operations or complex scenarios, use fixtures:
Reference fixtures in tests:

Running Tests Locally

Before Committing

Always run the full test suite before committing:
This runs:
  1. Linting with auto-fix
  2. Type checking
  3. 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)
The CI workflow runs:
.github/workflows/ci.yml

Test Warnings

Some integration tests modify fixture files when running the entire codemod. Remember to restore these files before committing:

Debugging Failed Tests

When a test fails:
1

Check the diff

The test runner shows differences between actual and expected output:
2

Run the transformation manually

Test your transformation on a single file:
3

Add debug logging

Add console.log statements in your transformation:
4

Verify the pattern

Test your AST pattern in Codemod Studio:
  1. Visit Codemod Studio
  2. Paste your code sample
  3. Test your pattern matching

Best Practices

Include tests for .js, .mjs, .cjs, .ts, .tsx, etc.
Use real-world examples from actual codebases, not just simple cases.
Ensure transformations work when only some imports need updating:
Each test file should test one specific pattern or edge case.
Use clear names that indicate what’s being tested:

Next Steps

Development Workflow

Learn about the development workflow and PR process