Skip to main content

πŸ§ͺ Jest Test & Coverage Commands

The Early CLI agent uses customizable commands to run tests and collect coverage, allowing you to align with your project’s Jest setup.


βš™οΈ Default Behavior​

The CLI agent expects two commands:

  • --test-command: Runs your Jest tests (after test generation)
  • --coverage-command: Collects coverage data (used before and after test generation)

βœ… Defaults​

If not specified, the agent uses:

--test-command "npx jest $early_filename --no-coverage --silent --json --forceExit --maxWorkers=1"

--coverage-command "npx jest --coverage --coverageProvider=v8 \
--coveragePathIgnorePatterns "node_modules" "dist" ".*\\.early\\.(spec|test)\\.[tj]sx?$" \
--coverageDirectory=$early_coverage_dir \
--silent --passWithNoTests --maxWorkers=2"

🚦 Behavior on Test Failures​

By default, if the test command reports any failing tests, the CLI will stop and the run will fail.

You can override this with:

--continue-on-test-errors

When this flag is present:

  • The agent will continue generating tests, even if existing tests fail.
  • However, if Jest itself fails to execute (for example, due to a configuration error or runtime crash), the agent will still stop.

🧩 Purpose of Each Command​

1. πŸ“Š Coverage Command (Pre-generation)​

Before generating tests, the agent runs the coverage command to understand existing coverage across your codebase.

  • The agent uses this data to focus on functions that are under-tested.
  • If coverage collection fails, the agent will stop and report an error.

2. πŸ§ͺ Test Command (Post-generation)​

After generating test files, the agent runs the test command to validate that:

  • Generated tests are valid Jest tests
  • All tests run and pass
  • There are no syntax or runtime errors

The --test-command may run multiple times on the same file as the agent iteratively refines and validates generated tests before committing them. This ensures the final committed version passes successfully.

If --continue-on-test-errors is not provided, any test failures will halt the run.
Use this flag to allow test generation to proceed even when existing tests are broken.

3. πŸ“Š Coverage Command (Post-validation)​

Finally, the agent runs the coverage command again to compare coverage before and after test generation.


πŸ” Overriding the Commands​

You can override either or both commands using CLI flags:

--test-command "npx jest $early_filename --no-coverage --silent --json --forceExit --maxWorkers=1"

--coverage-command "npx jest --coverage --coverageProvider=v8 \
--coveragePathIgnorePatterns "node_modules" "dist" ".*\\.early\\.(spec|test)\\.[tj]sx?$" \
--coverageDirectory=$early_coverage_dir \
--silent --passWithNoTests --maxWorkers=2"

Make sure to retain these required placeholders:

  • $early_filename β€” The generated test file path
  • --coverageDirectory=$early_coverage_dir β€” Where coverage output should be stored
  • --coverage
  • --coverageProvider=v8
  • --silent
  • --passWithNoTests

πŸ’‘ Use Case: Custom Coverage Output​

Suppose you want the coverage output to be compatible with CI dashboards or reporting tools like Jest JUnit or Cobertura XML, and you'd like to include multiple coverage reporters in one run.

This would include:

  • Generating JUnit-style test results (e.g., for GitHub Actions or Jenkins)
  • Creating a Cobertura XML report
  • Producing text and lcov reports for local and CI inspection

To do that, you can expand the default command as shown below.


πŸ§ͺ Example: Extended Coverage Command​

Original command:

--coverage-command "npx jest --coverage --coverageProvider=v8 \
--coveragePathIgnorePatterns \"node_modules\" \"dist\" \".*\\.early\\.(spec|test)\\.[tj]sx?$\" \
--coverageDirectory=$early_coverage_dir \
--silent --passWithNoTests --maxWorkers=2"

Updated command with additional reporters:

--coverage-command "npx jest --coverage --coverageProvider=v8 \
--reporters=default --reporters=jest-junit \
--coverageReporters=text --coverageReporters=lcov --coverageReporters=cobertura \
--coveragePathIgnorePatterns \"node_modules\" \"dist\" \".*\\.early\\.(spec|test)\\.[tj]sx?$\" \
--coverageDirectory=$early_coverage_dir \
--silent --passWithNoTests --maxWorkers=2"

This command keeps the required $early_coverage_dir placeholder while enhancing the output for downstream processing.


❗ Placeholder Reminder​

Both placeholders are required for correct operation. If omitted or renamed, the agent may fail.