Skip to main content
Test your jssg codemod using snapshot testing with input/expected file pairs.

Test structure

jssg tests use a simple directory structure where each test case contains two files:
File naming:
  • Input files must be named input.ts (or appropriate extension)
  • Expected files must be named expected.ts (or appropriate extension)
  • File extensions should match your target language (.ts, .tsx, .js, .jsx)

Command reference

Basic syntax

string
required
Language parser to use (e.g., “tsx”, “javascript”, “typescript”).
string
required
Path to your transform file (e.g., ”./scripts/codemod.ts”).
string
Path to test directory (defaults to ”./tests”).

Common options

See all available options in the CLI reference.

Creating effective test cases

1. Start simple:
2. Add edge cases:
3. Test different file types:

Understanding test results

Exit codes

  • 0: All tests passed
  • 1: At least one test failed (diffs are printed)
  • 2: Error occurred (e.g., syntax error in codemod)

Reading test output

Pro tip: Use -v flag for detailed output when debugging test failures.

Troubleshooting

Problem: Tests fail with parsing errors.

Solution: Ensure the -l flag matches your source files:
Problem: “No test files found” error.

Solution: Check your directory structure and file names:
Problem: Test passes but no changes were made.

Solution: This is normal if your transform returns null for unchanged files. If you expect changes:
Problem: Test fails with unexpected output.

Solution: Update expected output if changes are intentional:
Problem: “Syntax error in codemod” or TypeScript errors.

Solution: Check your transform file:

Workflow integration

Package script

Add to your package.json:
package.json

CI/CD integration

.github/workflows/test.yml

Best practices

  • Start simple: Begin with basic transformations before complex ones
  • Test edge cases: Include empty files, files with comments, and malformed code
  • Use descriptive names: Make test cases self-documenting
  • Keep tests focused: One transformation per test case
  • Validate locally: Always test before publishing