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
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.
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
Example fix
// before
searchSource.getTestPaths(
{ ...globalConfig, onlyChanged: true },
projectConfig,
// changedFiles omitted
);
// after
const { getChangedFilesForRoots } = require('jest-changed-files');
const changedFilesInfo = await getChangedFilesForRoots([projectConfig.rootDir], false);
await searchSource.getTestPaths(globalConfig, projectConfig, changedFilesInfo); Defensive patterns
Strategy: validation
Validate before calling
import { getChangedFilesForRoots } from 'jest-changed-files';
async function getTestPathsWithChanged(globalConfig, projectConfig, searchSource) {
let changedFiles;
if (globalConfig.onlyChanged) {
changedFiles = await getChangedFilesForRoots([projectConfig.rootDir], false);
}
return searchSource.getTestPaths(globalConfig, projectConfig, changedFiles);
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- "cwd" must be an absolute path. cwd
- Filter did not return a valid test list
- file must export a function at
- Can't find a root directory while resolving a config file…
- Cannot find `global.gc` function. Please run node with…
AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10).
Data as JSON: /api/errors/02b152dd251fabfb.
Report an issue: GitHub.
Appendix: 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 8e6d128e4a)