jestjs/jest · error · Error

Changed files must be set when running with -o.

Error message

Changed files must be set when running with -o.

What it means

In SearchSource._getTestPaths, when globalConfig.onlyChanged (the -o flag) is set, Jest must compute tests from changed files. If changedFiles is not provided (undefined), there is no source of truth for what changed, so it throws. The changed files are normally gathered by jest-changed-files from SCM before this point.

Source

Thrown at packages/jest-core/src/SearchSource.ts:276

  async findTestRelatedToChangedFiles(
    changedFilesInfo: ChangedFiles,
    collectCoverage: boolean,
  ): Promise<SearchResult> {
    if (!hasSCM(changedFilesInfo)) {
      return {noSCM: true, tests: []};
    }
    const {changedFiles} = changedFilesInfo;
    return this.findRelatedTests(changedFiles, collectCoverage);
  }

  private async _getTestPaths(
    globalConfig: Config.GlobalConfig,
    projectConfig: Config.ProjectConfig,
    changedFiles?: ChangedFiles,
  ): Promise<SearchResult> {
    if (globalConfig.onlyChanged) {
      if (!changedFiles) {
        throw new Error('Changed files must be set when running with -o.');
      }

      return this.findTestRelatedToChangedFiles(
        changedFiles,
        globalConfig.collectCoverage,
      );
    }

    let paths = globalConfig.nonFlagArgs;

    if (globalConfig.findRelatedTests && 'win32' === os.platform()) {
      paths = this.filterPathsWin32(paths);
    }

    if (globalConfig.runTestsByPath && paths && paths.length > 0) {
      return this.findTestsByPaths(paths);
    } else if (globalConfig.findRelatedTests && paths && paths.length > 0) {
      return this.findRelatedTestsFromPattern(

View on GitHub (pinned to f49721c78e)

Solutions

  1. If using -o, ensure you are inside a git/hg repository so changed files can be detected
  2. Programmatic callers must call jest-changed-files and pass the result as changedFiles to getTestPaths
  3. Drop the -o flag if you do not need changed-file filtering

Example fix

// before (programmatic)
searchSource.getTestPaths({...globalConfig, onlyChanged: true}, projectConfig)
// after
const changedFilesInfo = await getChangedFilesForRoots([root], {});
searchSource.getTestPaths({...globalConfig, onlyChanged: true}, projectConfig, changedFilesInfo)
Defensive patterns

Strategy: validation

Validate before calling

if (globalConfig.onlyChanged && !changedFiles) {
  throw new Error('--onlyChanged requires changed files; run inside a git repo');
}

Prevention

When it happens

Trigger: Running jest programmatically with onlyChanged: true but not passing a changedFiles object to getTestPaths; or an internal path where SCM detection failed and produced undefined.

Common situations: Custom programmatic runners (via jest-core) that set onlyChanged but forget to hydrate changedFiles; running -o outside a git/hg repo where jest-changed-files returns no SCM info.

Related errors


AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03). Data as JSON: /data/errors/02b152dd251fabfb.json. Report an issue: GitHub.