Skip to content

[Bug]: jest-util is missing a dependency on jest-regex-util (and error is misleading) #14990

@ecraig12345

Description

@ecraig12345

Version

30.0.0-alpha.3 (doesn't repro in 29)

Steps to reproduce

You can see the missing dependency by looking at the code: jest-util uses jest-regex-util as shown below, but it's not listed in jest-util/package.json.

import {escapePathForRegex, replacePathSepForRegex} from 'jest-regex-util';

That code will work when using a package manager/linker that uses a hoisted node_modules layout, but it will fail to find the implicit dependency with a strict/non-hoisted installation layout, such as yarn v3/4 with nodeLinker: pnpm, pnpm itself, or probably Yarn PnP.

Repro steps to see the misleading error message caused by this issue + non-hoisted installation:

  1. Clone https://github.com/ecraig12345/repro-jest-util-dep (or make a similar repo with either current yarn+nodeLinker: pnpm setting, or pnpm)
  2. Verify yarn -v shows 4.1.1
  3. yarn
  4. yarn jest => observe that tests run but there's an "Invalid testPattern" message as shown below
  5. yarn jest other => observe that all tests run, not only the selected test

Expected behavior

The tests pass with no odd error messages.

Actual behavior

  • Running all tests works, but there's a message Invalid testPattern supplied. Running all tests instead.
  • Trying to filter tests still runs all tests, with the same error message.
$ yarn jest
  Invalid testPattern  supplied. Running all tests instead.
  Invalid testPattern  supplied. Running all tests instead.
 PASS  ./other.test.js
 PASS  ./index.test.js

Test Suites: 2 passed, 2 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        0.346 s, estimated 1 s
Ran all test suites.

$ yarn jest other
  Invalid testPattern other supplied. Running all tests instead.
  Invalid testPattern other supplied. Running all tests instead.
 PASS  ./other.test.js
 PASS  ./index.test.js

Test Suites: 2 passed, 2 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        0.27 s, estimated 1 s
Ran all test suites.

Additional context

Debugging into the code shows that the Invalid testPattern log comes from this code block in jest-config, which catches the error (in this case a module not found error) and assumes it's an invalid pattern. The root issue is the missing dependency, but the error handling here could be improved too (not assuming all errors are invalid patterns).

try {
testPathPatterns.validate();
} catch {
clearLine(process.stdout);
// eslint-disable-next-line no-console
console.log(
chalk.red(
` Invalid testPattern ${testPathPatterns.toPretty()} supplied. ` +
'Running all tests instead.',

Environment

System:
    OS: macOS 12.7.4
    CPU: (10) arm64 Apple M1 Max
  Binaries:
    Node: 18.18.0 - ~/.nvm/versions/node/v18.18.0/bin/node
    Yarn: 4.1.1 - ~/.nvm/versions/node/v18.18.0/bin/yarn
    npm: 9.8.1 - ~/.nvm/versions/node/v18.18.0/bin/npm
  npmPackages:
    jest: ^30.0.0-alpha.2 => 30.0.0-alpha.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions