Skip to content

feat: make Jest discovery cwd-aware - #28

Merged
TomChv merged 1 commit into
mainfrom
cwd-aware-discovery
Jul 22, 2026
Merged

TomChv merged 1 commit into
mainfrom
cwd-aware-discovery

Conversation

@TomChv

@TomChv TomChv commented Jul 21, 2026 •

Copy link
Copy Markdown
Member

Implements the cwd-aware config discovery convention from dagger/dagger#13688 for the Jest toolchain. Port of dagger/vitest#17.

Before

The constructor flattened the workspace to ws.directory("/") and test ran a single npx jest at its root. A workspace with several Jest projects had no definition of which projects a run covers, and no way to act on the project you are in.

After

projects(ws) delegates to polyfill.findConfigDirs for jest.config.{js,mjs,cjs,ts,mts,cts,json}, excluding node_modules. Paths are cwd-relative:

  • every project at or below the cwd — ., sub
  • the nearest enclosing project when the cwd holds no config of its own — ..

A jest key in package.json also configures Jest, but package.json marks every npm package, so it is deliberately not a discovery marker. Projects configured that way are not discovered; they can still be run explicitly with test --path.

testAll(ws) is the new automatic @check and runs all discovered projects: a cwd holding no config sits inside its enclosing project, so that project runs too. Scoping a run to one project is done by entering it. test keeps its behavior, loses @check, and gains a cwd-relative path (default .); list and source likewise.

Workspace can no longer be stored as a field on a module object, so it moves from the constructor to a function argument on each entry point. That also removes the source toolchain customization from the README — it targeted a constructor argument that no longer exists.

Behavior

Given two sibling projects under a config-less root:

a/jest.config.js
b/jest.config.js

from the root projects returns a, b; from a it returns ..

Adding a root project does not change the view from below — the config in the cwd shadows the enclosing one:

jest.config.js
a/jest.config.js
b/jest.config.js

from the root: ., a, b; from a: .; from b: ..

The .. case appears only when the cwd holds no config:

jest.config.js
work/            <- cwd
work/app/jest.config.js

from work: .., app, and dagger check runs both.

Tests

Five new e2e checks build synthetic workspaces from copies of kpenfound/demo-react-app (.git and its dagger.json stripped) and enter them at a chosen cwd:

  • sibling-discovery-check, nested-discovery-check, ancestor-discovery-check — the three layouts above, plus that the returned .. resolves back to the enclosing project
  • descendant-scope-check — runs real Jest: a failure planted in b/src is seen from the root and not from a, and a failure planted in a/src is seen from a
  • ancestor-scope-check — a failure planted in the enclosing project is seen from the config-less work

test-demo-app is unchanged apart from calling testAll.

$ dagger check
== CHECKS ==  ✔ 6 passed

Also verified against a real client cwd (not asWorkspace(cwd:)) in a scratch workspace: root → a b . work/app, a/ → ., work/ → .. app, and dagger check from work/ passes.

Note

engineVersion stays at v0.20.6; nothing here needs a newer engine. The e2e module keeps its own v1.0.0-beta.3, which is what Directory.asWorkspace(cwd:) requires.

Jest mounted the workspace root and ran one `npx jest` over it, which gives a
multi-project workspace no definition of which projects a run should cover, and
no way to act on the project you are actually in.

`projects` now delegates discovery to `polyfill.findConfigDirs`, per the
convention in dagger/dagger#13688: every directory
holding a `jest.config.*` at or below the client cwd, plus the nearest
enclosing project as a `..`-relative path when the cwd holds no config of its
own. A `jest` key in package.json also configures Jest, but package.json marks
every npm package, so it is not a discovery marker.

`testAll` becomes the automatic check and runs every discovered project. A cwd
holding no config of its own sits inside its enclosing project, so that project
runs too; scoping a run to a single project is done by entering it.

`test`, `list` and `source` take a cwd-relative project path and a Workspace
argument; the workspace can no longer be a field on the module object, which
also drops the `source` toolchain customization from the README.

Signed-off-by: Tom Chauveau <[email protected]>
@TomChv

TomChv commented Jul 21, 2026 •

Copy link
Copy Markdown
Member Author

Cross-posting a limitation found while reviewing the vitest equivalent (dagger/vitest#17, thread) — it reproduces here identically, since test mounts only the project directory.

Scratch monorepo:

package.json
jest.config.js
a/jest.config.js       <- require('../jest.config')
a/src/x.test.js

Two independent failures running testAll from a/:

1. Dependency root. package.json and the lockfile are in the parent, so the mount has neither. Jest fails even earlier than vitest does, in the useEnv branch that reads the package type:

Error: stat package.json: no such file or directory
  Directory.file(path: "package.json")

2. Config inheritance. Giving a/ its own package.json gets past that, then:

Error: Cannot find module '../jest.config'

They don't collapse into one fix — in a pnpm/yarn workspace each package has its own package.json while the shared config lives at the root, so fixing (1) leaves (2) broken.

Direction under discussion for the follow-up is to mount the dagger workspace root and set withWorkdir("/src/" + path), rather than trying to detect the needed root (the config half of that can't be computed without parsing arbitrary JS/TS — require of a parent config, preset resolution, projects entries). Mounting is orthogonal to discovery, so running from a/ would still run only a's tests; the trade-off is cache granularity.

Worth checking whether this belongs in github.com/dagger/node instead — jest already goes through nodejs(src, ...), so root resolution there would cover jest for free and give vitest something to adopt rather than duplicating a walk-up in both.

Not addressing it in this PR; it's correct for self-contained projects, which is what the checks cover.

@TomChv
TomChv merged commit 4f34acb into main Jul 22, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants