jestjs/jest · error · TypeError

Filter ${filterPath} did not return a valid test list

Error message

Filter ${filterPath} did not return a valid test list

What it means

When the --filter option points to a JS module, Jest calls it with the list of test paths and expects back an object with a `filtered` array property (the subset to run). If the returned value is not an array at `.filtered`, this TypeError is thrown. The filter contract is {filtered: string[]}.

Source

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

    projectConfig: Config.ProjectConfig,
    changedFiles?: ChangedFiles,
    filter?: Filter,
  ): Promise<SearchResult> {
    const searchResult = await this._getTestPaths(
      globalConfig,
      projectConfig,
      changedFiles,
    );

    const filterPath = globalConfig.filter;

    if (filter) {
      const tests = searchResult.tests;

      const filterResult = await filter(tests.map(test => test.path));

      if (!Array.isArray(filterResult.filtered)) {
        throw new TypeError(
          `Filter ${filterPath} did not return a valid test list`,
        );
      }

      const filteredSet = new Set(filterResult.filtered);

      return {
        ...searchResult,
        tests: tests.filter(test => filteredSet.has(test.path)),
      };
    }

    return searchResult;
  }

  async findRelatedSourcesFromTestsInChangedFiles(
    changedFilesInfo: ChangedFiles,
  ): Promise<Array<string>> {

View on GitHub (pinned to f49721c78e)

Solutions

  1. Return exactly {filtered: [...paths]} from the filter module
  2. Ensure the filter does not throw and always returns the object
  3. Consult the --filter docs for the expected contract

Example fix

// before
module.exports = (tests) => ({tests});
// after
module.exports = (tests) => ({filtered: tests.filter(t => t.includes('unit'))});
Defensive patterns

Strategy: type-guard

Validate before calling

function assertFilterResult(r: unknown): asserts r is {filtered: string[]} {
  if (!r || !Array.isArray((r as any).filtered)) {
    throw new TypeError('--filter module must return {filtered: string[]}');
  }
}

Type guard

function isFilterResult(r: unknown): r is {filtered: readonly string[]} {
  return typeof r === 'object' && r !== null &&
    Array.isArray((r as {filtered: unknown}).filtered);
}

Prevention

When it happens

Trigger: A filter module that returns {tests: [...]} instead of {filtered: [...]}, returns null/undefined, or returns a bare array.

Common situations: Writing a custom --filter script and guessing the return shape; an outdated filter from an older Jest version; a filter that throws and yields undefined.

Related errors


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