Skip to main content
Experimental. This can change in a minor release of the CLI, but not in a patch. Pin the CLI version (npx codemod@<version>) if you build on it.
A package’s tests are a test file you can read and change: tests/workflow.test.ts. It runs the whole workflow on fixtures with codemod/testing, so it covers what the package actually does: forEachFile callbacks, inline or imported from their files, function steps, how the steps fit together, shell steps and renames. An imported callback is tested the same way, through the workflow that runs it: give its step’s files as fixtures. (codemod jssg test tests one jssg transform file on its own.) codemod init scaffolds the test file, a starter fixture, and a test script that runs them with Node.js’s built-in test runner:

The test file

tests/workflow.test.ts
  • fixtures(dir) lists the cases in tests/fixtures, one test each.
  • runWorkflow copies a case’s input into a temporary directory and runs the workflow there, so fixtures are never changed. Nothing is recorded in the run store.
  • assertTree compares the files the run left with the case’s expected output, and fails with a diff.
Run the tests:
The test file is yours: add test(...) blocks next to the fixture loop for cases that need inline files, one step on its own or mocks. Node.js runs TypeScript test files directly from version 22.18, which the scaffolded package.json requires. With Bun, run bun test; with Vitest, npx vitest.

Fixtures

A fixture is a directory under tests/fixtures with either an input/ and an expected/ directory, or an input.<ext> and an expected.<ext> file. These are the layouts codemod jssg test reads, so existing fixtures work as they are.
  • Directory fixtures compare every file: changed files, files the workflow created, and files it removed.
  • Single-file fixtures expect the run to leave exactly one file, and compare it with expected.<ext>, whatever its name.
  • Fixtures may be grouped in subdirectories; a fixture’s name is its path under tests/fixtures.
The workflow selects files by its own rules. A single-file fixture is named input.<ext>, so a step that only selects **/package.json never sees it. Use a directory fixture with the real file names for such steps.

Update expected files

After a change you meant to make, write each run’s files into its fixture’s expected output:
Review the changes to tests/fixtures before you commit them.

More from codemod/testing

codemod/testing ships with the codemod package, which codemod init adds as a dependency. Paths given to it are resolved against the current directory; pass a URL, as the test file does, to resolve them against the test file. The first argument to runWorkflow is a workflow file or a package directory. assertTree throws an AssertionError with the diff in its message and actual and expected set, so every runner reports it. If your workflow still imports the older codemod:workflow spelling, Vitest needs the codemod/vite plugin to resolve it:
vitest.config.ts

Write files inline

For small cases, give the files directly instead of a fixture directory:
A workflow.yaml’s install-skill steps are skipped in test runs, as in a dry run: a test does not install skills into the coding agents on your machine.

Test one step

Steps are named by their label. until stops after the named step: steps the workflow starts after it do not run, and the run ends with status "stopped". given replaces a step’s result without running it, so a later step can be tested on its own:
workflow.ts
tests/rewrite.test.ts
A given result has the step’s shape: a forEachFile step with a reducer gives { reduced, changes, failures }, so given: { "Count rewrites": { reduced: 3 } } is enough when the workflow only reads reduced. A label in until or given that matches no step is an error that lists the run’s steps. Steps without a label get one from their task, such as forEachFile src/**/*.ts.

Test a callback file

A forEachFile callback kept in its own file (its default export, (file) => ...) can be tested without the workflow around it. runCallback runs it on each file of the target in language, as a forEachFile step does, and applies what it writes, renames and deletes to the copy:
tests/migrate.test.ts
It runs with the codemod CLI, in the engine’s runtime, so definition() and references() work as they do in a run. Its options are target, language (required), semanticAnalysis ("file", the default, "workspace" or "none"), capabilities, keep and signal. The run it returns is a run without steps, plus outputs (what the callback returned, { file, data } by path) and failures ({ path, error }); it fails when the callback failed on any file. The same fixtures run from the command line with codemod jssg test --callback.

Mock a workflow’s imports

To mock modules the workflow imports, let your test runner load the workflow and pass the module to runWorkflow, with path set to the workflow file so relative task paths resolve:
tests/workflow.test.ts

Where the workflow runs

codemod/testing runs TypeScript workflows in your test process, on the Node.js host. The CLI runs them in the engine’s own sandboxed runtime. The two run the same orchestrator and the same tasks, and the Node.js host holds callbacks to the same rules and replays the body’s clock as the CLI does, so a test that passes there does not fail under the CLI for using fetch or a timer. A few things still differ, such as reads outside the target; see what differs on the Node.js host. To test against the runtime codemod run uses, pass host: "quickjs":
YAML workflows always run with the CLI.

What a run returns

runWorkflow resolves for every outcome. It rejects only when the workflow cannot run at all (for example, it does not build) or when an until or given label matches no step. assertTree(run, expected) fails with the run’s error and logs when the run did not succeed. expected is a directory, a single file, or files by path. Line endings are compared as \n. Pass { ignoreWhitespace: true } to ignore whitespace at the ends of lines and files.

Options

How test runs differ from real runs

  • Every approval is given unless you pass approve: false (or --no-approve).
  • Git steps (branch, commit, pullRequest) do nothing, as in a run without --managed-git. One started while other steps are still running still fails, as it would with --managed-git.
  • Shell steps run for real, in the temporary copy.
  • agent steps would call a coding agent: give their result with given instead.
  • waitForEvent steps have nobody to send their event: give the event’s data with given, under the label wait for <name>. Without it the step waits until its timeout, or for as long as your test runner allows. sleep steps wait as usual.
  • http() steps send their requests: point them at a local server, or give their result with given. A dry run (dryRun: true) skips them unless they set dryRun: "perform".
codemod/testing uses the native addon and the codemod CLI that the package installed. To test against other builds, set CODEMOD_NODE_ADDON and CODEMOD_BINARY to their paths.