{"record":{"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/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/jest-core/src/SearchSource.ts#L258-L294","documentation":"Thrown by SearchSource._getTestPaths when globalConfig.onlyChanged is true (set by the -o / --onlyChanged CLI flag) but no changedFiles argument was passed to the method. The changed-file set is required to compute which tests cover modified source files.","triggerScenarios":"Programmatic invocation of SearchSource.getTestPaths / _getTestPaths with onlyChanged=true and changedFiles undefined. The CLI path normally supplies changedFiles from jest-changed-files; this is hit via the API or a misconfigured runner.","commonSituations":"Custom Jest runners/launchers that call SearchSource directly and forget to compute and pass changed files; test harnesses that enable --onlyChanged without wiring the SCM integration.","solutions":["Compute changed files via jest-changed-files (getChangedFilesForRoots) and pass them as the changedFiles argument","If invoking programmatically, drop the onlyChanged flag and pass explicit test paths instead","When using the CLI, ensure you are inside a git/hg repo so changed files can be detected"],"exampleFix":"// before\nsearchSource.getTestPaths(\n  { ...globalConfig, onlyChanged: true },\n  projectConfig,\n  // changedFiles omitted\n);\n\n// after\nconst { getChangedFilesForRoots } = require('jest-changed-files');\nconst changedFilesInfo = await getChangedFilesForRoots([projectConfig.rootDir], false);\nawait searchSource.getTestPaths(globalConfig, projectConfig, changedFilesInfo);","handlingStrategy":"validation","validationCode":"import { getChangedFilesForRoots } from 'jest-changed-files';\nasync function getTestPathsWithChanged(globalConfig, projectConfig, searchSource) {\n  let changedFiles;\n  if (globalConfig.onlyChanged) {\n    changedFiles = await getChangedFilesForRoots([projectConfig.rootDir], false);\n  }\n  return searchSource.getTestPaths(globalConfig, projectConfig, changedFiles);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When calling SearchSource programmatically, always compute changedFiles if onlyChanged is true","Guard with an assertion: if (globalConfig.onlyChanged) invariant(changedFiles, '...')","Run inside a git/hg repo so jest-changed-files can populate the set"],"tags":["core","only-changed","scm","programmatic-api","argument-validation"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}