jestjs/jest · error · TypeError

jest-haste-map: the `ignorePattern` option must be a RegExp

Error message

jest-haste-map: the `ignorePattern` option must be a RegExp

What it means

The HasteMap constructor validates the `ignorePattern` option: if provided it MUST be a RegExp instance. The library merges your pattern with VCS directory patterns to build a combined matcher, which only works on a RegExp source/flags. A string, array, glob, or any other type is rejected up front rather than producing silently-wrong ignore behavior later.

Source

Thrown at packages/jest-haste-map/src/index.ts:254

      retainAllFiles: options.retainAllFiles,
      rootDir: options.rootDir,
      roots: [...new Set(options.roots)],
      skipPackageJson: !!options.skipPackageJson,
      throwOnModuleCollision: !!options.throwOnModuleCollision,
      useWatchman: options.useWatchman ?? true,
      watch: !!options.watch,
      workerThreads: options.workerThreads,
    };
    this._console = options.console || globalThis.console;

    if (options.ignorePattern) {
      if (options.ignorePattern instanceof RegExp) {
        this._options.ignorePattern = new RegExp(
          `${options.ignorePattern.source}|${VCS_DIRECTORIES}`,
          options.ignorePattern.flags,
        );
      } else {
        throw new TypeError(
          'jest-haste-map: the `ignorePattern` option must be a RegExp',
        );
      }
    } else {
      this._options.ignorePattern = new RegExp(VCS_DIRECTORIES);
    }

    if (this._options.enableSymlinks && this._options.useWatchman) {
      throw new Error(
        'jest-haste-map: enableSymlinks config option was set, but ' +
          'is incompatible with watchman.\n' +
          'Set either `enableSymlinks` to false or `useWatchman` to false.',
      );
    }

    this._ignoreFn = buildIgnoreMatcher(
      this._options.ignorePattern,
      this._options.retainAllFiles,

View on GitHub (pinned to f49721c78e)

Solutions

  1. Pass a RegExp: wrap your pattern with new RegExp(...) before handing it to HasteMap/Jest.
  2. If you have multiple patterns, combine them with alternation into one RegExp, e.g. new RegExp('node_modules|build|dist').
  3. If the value originates from jest config `haste`, ensure the upstream config layer (jest-config → HasteMap.Options) normalizes it to a RegExp.
  4. Omit ignorePattern entirely to accept the default VCS-directory-only ignore behavior.

Example fix

// before
HasteMap.create({ ignorePattern: 'node_modules', ... });

// after
HasteMap.create({ ignorePattern: /node_modules/, ... });
Defensive patterns

Strategy: type-guard

Validate before calling

if (options.ignorePattern != null && !(options.ignorePattern instanceof RegExp)) {
  throw new TypeError('ignorePattern must be a RegExp; got ' + typeof options.ignorePattern);
}

Type guard

function isRegExp(v: unknown): v is RegExp {
  return v instanceof RegExp;
}
// usage:
if (options.ignorePattern != null && !isRegExp(options.ignorePattern)) {
  // normalize or throw before passing to HasteMap
  options.ignorePattern = new RegExp(String(options.ignorePattern));
}

Prevention

When it happens

Trigger: Calling HasteMap.create({...options, ignorePattern: <not a RegExp>}) where the value is a string (e.g. 'node_modules'), an array of globs, a function, or undefined-but-truthy. The branch at index.ts:247 enters because ignorePattern is truthy, then the `instanceof RegExp` check at line 248 fails, hitting the throw at line 254.

Common situations: Migrating from a config that accepted string ignore patterns; passing a glob string from a shared config object; tooling that reads .gitignore-style strings and forwards them as ignorePattern; jest-config wiring that forwards haste.ignorePattern without converting to RegExp.

Related errors


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