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

jest-haste-map accepts an `ignorePattern` option to exclude file paths from the crawl. The constructor enforces that it MUST be a RegExp instance; any other type (string, array, glob) is rejected with a TypeError before any work begins. Internally the RegExp source is merged with VCS directory patterns, so a non-RegExp cannot be processed.

Solutions

  1. Wrap your ignore pattern in `new RegExp(...)`: `ignorePattern: new RegExp('node_modules|fixtures')`.
  2. If using Jest CLI `--hasteIgnorePattern`, ensure the downstream code compiles the string to a RegExp before reaching HasteMap.
  3. Omit `ignorePattern` entirely if you only need VCS exclusion — the default already ignores `.git`, `.hg`, `.sl`.

Example fix

// before
HasteMap.create({ ..., ignorePattern: '**/fixtures/**' });
// after
HasteMap.create({ ..., ignorePattern: /fixtures/ });
Defensive patterns

Strategy: type-guard

Validate before calling

// Ensure ignorePattern is a RegExp before passing to HasteMap.
function toIgnorePattern(value: unknown): RegExp | undefined {
  if (value === undefined) return undefined;
  if (value instanceof RegExp) return value;
  if (typeof value === 'string') return new RegExp(value);
  throw new TypeError(`ignorePattern must be a RegExp or string, got ${typeof value}`);
}

Type guard

function isRegExp(value: unknown): value is RegExp {
  return value instanceof RegExp;
}

Prevention

When it happens

Trigger: Passing `HasteMap.create({ ignorePattern: 'node_modules' })` or any non-RegExp value. The constructor checks `options.ignorePattern instanceof RegExp` at index.ts:248; when it fails, the TypeError is thrown at line 254.

Common situations: A Jest config or custom haste-map caller passes a glob string (e.g. '**/fixtures/**') instead of a RegExp. Also happens when migrating from an older config format that accepted strings, or when a build tool forwards user input without converting it to RegExp.

Related errors


AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10). Data as JSON: /api/errors/176f1dc3c7cd710f. Report an issue: GitHub.

Appendix: 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 8e6d128e4a)