Skip to content

fix(jest-config): honor explicit --config path when cwd has no discoverable config - #16458

Open
Dextheking1 wants to merge 6 commits into
jestjs:mainfrom
Dextheking1:fix/16457-explicit-config-path
Open

Dextheking1 wants to merge 6 commits into
jestjs:mainfrom
Dextheking1:fix/16457-explicit-config-path

Conversation

@Dextheking1

Copy link
Copy Markdown

Fixes #16457

Summary

jest --config <file> failed with Could not find a config file based on provided values when run from a directory (and ancestors) containing no discoverable config, even though the explicitly passed config file resolved fine.

Root cause

Regression from #16411. In readConfigs (packages/jest-config/src/index.ts), the suppliesGlobalConfig check calls resolveConfigPath(root, cwd, projectIsCwd) on the project root to compare it against the already-resolved configPath. When the project root has no discoverable config, resolveConfigPath throws instead of simply not matching, so the whole run dies before the per-project readConfig -- which would have honored the explicit --config -- ever runs.

Fix

Guard the discovery call with try/catch: if no config can be discovered for the project root, it simply does not supply the global config through discovery. No behavior change when a config is discoverable.

Testing

  • Added packages/jest-config/src/__tests__/readConfigsExplicitConfig.test.ts: calls readConfigs with an explicit --config file from a cwd with no discoverable config (mocked process.cwd() into a fresh temp dir, mirroring the issue's mktemp -d repro). Fails on the base commit with the exact Could not find a config file error from the issue; passes with the fix.
  • Existing suites readConfigs, readConfig, readInitialOptions, resolveConfigPath all pass (62/62).

…erable config

readConfigs computed suppliesGlobalConfig by calling resolveConfigPath on
the project root, which throws 'Could not find a config file' when the cwd
has no discoverable config - even when an explicit --config <file> was
passed and already resolved successfully. Guard the discovery call: a
project root with no discoverable config simply does not supply the global
config through discovery.

Fixes jestjs#16457
@netlify

netlify Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 78f5630
🔍 Latest deploy log https://app.netlify.com/projects/jestjs/deploys/6ab456d6eb13f80008b939ae
😎 Deploy Preview https://deploy-preview-16458--jestjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions github-actions Bot added the require-changelog If a PR does requires a changelog entry label Sep 23, 2026
@pkg-pr-new

pkg-pr-new Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

babel-jest

npm i https://pkg.pr.new/babel-jest@16458

babel-plugin-jest-hoist

npm i https://pkg.pr.new/babel-plugin-jest-hoist@16458

babel-preset-jest

npm i https://pkg.pr.new/babel-preset-jest@16458

create-jest

npm i https://pkg.pr.new/create-jest@16458

@jest/diff-sequences

npm i https://pkg.pr.new/@jest/diff-sequences@16458

expect

npm i https://pkg.pr.new/expect@16458

@jest/expect-utils

npm i https://pkg.pr.new/@jest/expect-utils@16458

jest

npm i https://pkg.pr.new/jest@16458

jest-changed-files

npm i https://pkg.pr.new/jest-changed-files@16458

jest-circus

npm i https://pkg.pr.new/jest-circus@16458

jest-cli

npm i https://pkg.pr.new/jest-cli@16458

jest-config

npm i https://pkg.pr.new/jest-config@16458

@jest/console

npm i https://pkg.pr.new/@jest/console@16458

@jest/core

npm i https://pkg.pr.new/@jest/core@16458

@jest/create-cache-key-function

npm i https://pkg.pr.new/@jest/create-cache-key-function@16458

jest-diff

npm i https://pkg.pr.new/jest-diff@16458

jest-docblock

npm i https://pkg.pr.new/jest-docblock@16458

jest-each

npm i https://pkg.pr.new/jest-each@16458

@jest/environment

npm i https://pkg.pr.new/@jest/environment@16458

jest-environment-jsdom

npm i https://pkg.pr.new/jest-environment-jsdom@16458

@jest/environment-jsdom-abstract

npm i https://pkg.pr.new/@jest/environment-jsdom-abstract@16458

jest-environment-node

npm i https://pkg.pr.new/jest-environment-node@16458

@jest/expect

npm i https://pkg.pr.new/@jest/expect@16458

@jest/fake-timers

npm i https://pkg.pr.new/@jest/fake-timers@16458

@jest/get-type

npm i https://pkg.pr.new/@jest/get-type@16458

@jest/globals

npm i https://pkg.pr.new/@jest/globals@16458

jest-haste-map

npm i https://pkg.pr.new/jest-haste-map@16458

jest-jasmine2

npm i https://pkg.pr.new/jest-jasmine2@16458

jest-leak-detector

npm i https://pkg.pr.new/jest-leak-detector@16458

jest-matcher-utils

npm i https://pkg.pr.new/jest-matcher-utils@16458

jest-message-util

npm i https://pkg.pr.new/jest-message-util@16458

jest-mock

npm i https://pkg.pr.new/jest-mock@16458

@jest/pattern

npm i https://pkg.pr.new/@jest/pattern@16458

jest-phabricator

npm i https://pkg.pr.new/jest-phabricator@16458

jest-regex-util

npm i https://pkg.pr.new/jest-regex-util@16458

@jest/reporters

npm i https://pkg.pr.new/@jest/reporters@16458

jest-resolve

npm i https://pkg.pr.new/jest-resolve@16458

jest-resolve-dependencies

npm i https://pkg.pr.new/jest-resolve-dependencies@16458

jest-runner

npm i https://pkg.pr.new/jest-runner@16458

jest-runtime

npm i https://pkg.pr.new/jest-runtime@16458

@jest/schemas

npm i https://pkg.pr.new/@jest/schemas@16458

jest-snapshot

npm i https://pkg.pr.new/jest-snapshot@16458

@jest/snapshot-utils

npm i https://pkg.pr.new/@jest/snapshot-utils@16458

@jest/source-map

npm i https://pkg.pr.new/@jest/source-map@16458

@jest/test-result

npm i https://pkg.pr.new/@jest/test-result@16458

@jest/test-sequencer

npm i https://pkg.pr.new/@jest/test-sequencer@16458

@jest/transform

npm i https://pkg.pr.new/@jest/transform@16458

@jest/types

npm i https://pkg.pr.new/@jest/types@16458

jest-util

npm i https://pkg.pr.new/jest-util@16458

jest-validate

npm i https://pkg.pr.new/jest-validate@16458

jest-watcher

npm i https://pkg.pr.new/jest-watcher@16458

jest-worker

npm i https://pkg.pr.new/jest-worker@16458

pretty-format

npm i https://pkg.pr.new/pretty-format@16458

commit: 78f5630

…atforms

The new regression test for jestjs#16457 failed CI in three ways, all in the
test itself (the fix is unaffected):

- Static Checks: `jest.SpyInstance` is not exported by @jest/globals 30;
  annotate the spy as `jest.SpiedFunction<() => string>`.
- macOS: `os.tmpdir()` (/var/...) differs from the realpath jest reports
  (/private/var/...); create the fixture dir through `fs.realpathSync`
  so assertions compare canonical paths.
- Windows: the mocked cwd contained an 8.3 short-name segment, so jest's
  internal `tryRealpath(process.cwd())` no longer matched the project
  path and the test fell into the config-discovery path. Mocking with the
  canonical path keeps `projectIsCwd` true.
Follow-up to the cross-platform stabilization:

- `import * as fs from 'fs'` is banned by `no-restricted-imports`; use
  `graceful-fs` like the other jest-config tests.
- sort-keys: `{force: true, recursive: true}` in the `rmSync` cleanup.
…test

`fs.realpathSync` does not expand Windows 8.3 short names while jest's
internal `tryRealpath` (realpathSync.native) does, so the mocked cwd
still mismatched on Windows. Use the same `tryRealpath` from jest-util
that the source uses, making the canonicalization identical.
Wrap the tryRealpath(mkdtempSync(...)) call to satisfy prettier line
length in the Static Checks job.
@Dextheking1
Dextheking1 marked this pull request as ready for review September 23, 2026 23:54
@Dextheking1

Copy link
Copy Markdown
Author

The "Node LTS on Ubuntu with leak detection" failure is unrelated to this PR: the failing suite is packages/jest-mock/src/__tests__/whenCalledWith.test.ts, while this change only touches packages/jest-config (a try/catch guard, one new jest-config test, CHANGELOG). The leak job's test script doesn't even include jest-config suites. This looks like a flaky GC-timing false positive from the experimental leak detector. Could a maintainer please re-run that one job?

@Dextheking1

Copy link
Copy Markdown
Author

The leak-detection job failed on packages/jest-mock whenCalledWith.test.ts, which this PR does not touch (the change is in jest-config). The leak detector is experimental and this reads as a flake. Could a maintainer re-run that job?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

require-changelog If a PR does requires a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: --config <file> fails with "Could not find a config file" when the cwd has no discoverable config (jest-config >= 30.5.1)

1 participant