{"id":"02b152dd251fabfb","repo":"jestjs/jest","slug":"changed-files-must-be-set-when-running-with-o","errorCode":null,"errorMessage":"Changed files must be set when running with -o.","messagePattern":"Changed files must be set when running with -o\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-core/src/SearchSource.ts","lineNumber":276,"sourceCode":"  async findTestRelatedToChangedFiles(\n    changedFilesInfo: ChangedFiles,\n    collectCoverage: boolean,\n  ): Promise<SearchResult> {\n    if (!hasSCM(changedFilesInfo)) {\n      return {noSCM: true, tests: []};\n    }\n    const {changedFiles} = changedFilesInfo;\n    return this.findRelatedTests(changedFiles, collectCoverage);\n  }\n\n  private async _getTestPaths(\n    globalConfig: Config.GlobalConfig,\n    projectConfig: Config.ProjectConfig,\n    changedFiles?: ChangedFiles,\n  ): Promise<SearchResult> {\n    if (globalConfig.onlyChanged) {\n      if (!changedFiles) {\n        throw new Error('Changed files must be set when running with -o.');\n      }\n\n      return this.findTestRelatedToChangedFiles(\n        changedFiles,\n        globalConfig.collectCoverage,\n      );\n    }\n\n    let paths = globalConfig.nonFlagArgs;\n\n    if (globalConfig.findRelatedTests && 'win32' === os.platform()) {\n      paths = this.filterPathsWin32(paths);\n    }\n\n    if (globalConfig.runTestsByPath && paths && paths.length > 0) {\n      return this.findTestsByPaths(paths);\n    } else if (globalConfig.findRelatedTests && paths && paths.length > 0) {\n      return this.findRelatedTestsFromPattern(","sourceCodeStart":258,"sourceCodeEnd":294,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-core/src/SearchSource.ts#L258-L294","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["If using -o, ensure you are inside a git/hg repository so changed files can be detected","Programmatic callers must call jest-changed-files and pass the result as changedFiles to getTestPaths","Drop the -o flag if you do not need changed-file filtering"],"exampleFix":"// before (programmatic)\nsearchSource.getTestPaths({...globalConfig, onlyChanged: true}, projectConfig)\n// after\nconst changedFilesInfo = await getChangedFilesForRoots([root], {});\nsearchSource.getTestPaths({...globalConfig, onlyChanged: true}, projectConfig, changedFilesInfo)","handlingStrategy":"validation","validationCode":"if (globalConfig.onlyChanged && !changedFiles) {\n  throw new Error('--onlyChanged requires changed files; run inside a git repo');\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run -o only inside git/hg repositories","For programmatic use, always hydrate changedFiles via jest-changed-files before calling getTestPaths"],"tags":["cli","scm","changed-files","programmatic"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}