Skip to content

Add --collectOnly flag to list tests without running them#16006

Open
alexander-turner wants to merge 3 commits intojestjs:mainfrom
alexander-turner:claude/add-jest-collect-only-2ea6Q
Open

Add --collectOnly flag to list tests without running them#16006
alexander-turner wants to merge 3 commits intojestjs:mainfrom
alexander-turner:claude/add-jest-collect-only-2ea6Q

Conversation

@alexander-turner
Copy link
Copy Markdown

@alexander-turner alexander-turner commented Mar 25, 2026

Summary

The --collectOnly flag discovers and reports all test cases without executing any test callbacks — similar to pytest's --collect-only.

Running jest --collectOnly loads test files and registers all describe/test/it blocks, then prints a tree of discovered tests and exits with code 0:

src/tests/math.test.ts
  add
    adds positive numbers
    adds negative numbers
  subtract
    subtracts positive numbers

With --json, it outputs the standard formatTestResults JSON format with all tests marked as pending. Works with --testNamePattern and --testPathPatterns for filtering.

Implementation

  • jest-cli / jest-config / jest-types / jest-schemas: Thread collectOnly as a GlobalConfig boolean option
  • jest-circus: After loading test files, the adapter calls collectTestsWithoutRunning() which walks the describe tree recursively, filters by testNamePattern using the existing getTestID utility, and returns all matching tests as pending assertion results
  • jest-core: runJest schedules tests with reporters suppressed and coverage disabled, prints the tree via VerboseReporter.groupTestsBySuites, and delegates JSON/file output to the existing processResults helper
  • jest-circus/utils: Exports the previously-private getTestNamesPath for use by the collection function

Testing

  • E2e tests (e2e/__tests__/collectOnly.test.ts) using the existing e2e/each fixture verify: tree output, valid JSON structure, --testNamePattern filtering, empty result handling, and that deliberately failing tests still exit 0 (confirming no execution)
  • Unit tests (collectTestsWithoutRunning.test.ts) verify: flat/nested collection, ancestor titles, source order preservation across describe blocks, testNamePattern filtering, and empty results

@netlify
Copy link
Copy Markdown

netlify bot commented Mar 25, 2026

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit a0e9f1d
🔍 Latest deploy log https://app.netlify.com/projects/jestjs/deploys/69c6bb567b71b90008f144ff
😎 Deploy Preview https://deploy-preview-16006--jestjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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

@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla bot commented Mar 25, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 27, 2026

Open in StackBlitz

babel-jest

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

babel-plugin-jest-hoist

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

babel-preset-jest

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

create-jest

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

@jest/diff-sequences

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

expect

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

@jest/expect-utils

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

jest

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

jest-changed-files

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

jest-circus

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

jest-cli

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

jest-config

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

@jest/console

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

@jest/core

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

@jest/create-cache-key-function

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

jest-diff

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

jest-docblock

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

jest-each

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

@jest/environment

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

jest-environment-jsdom

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

@jest/environment-jsdom-abstract

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

jest-environment-node

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

@jest/expect

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

@jest/fake-timers

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

@jest/get-type

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

@jest/globals

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

jest-haste-map

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

jest-jasmine2

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

jest-leak-detector

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

jest-matcher-utils

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

jest-message-util

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

jest-mock

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

@jest/pattern

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

jest-phabricator

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

jest-regex-util

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

@jest/reporters

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

jest-resolve

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

jest-resolve-dependencies

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

jest-runner

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

jest-runtime

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

@jest/schemas

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

jest-snapshot

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

@jest/snapshot-utils

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

@jest/source-map

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

@jest/test-result

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

@jest/test-sequencer

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

@jest/transform

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

@jest/types

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

jest-util

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

jest-validate

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

jest-watcher

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

jest-worker

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

pretty-format

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

commit: a0e9f1d

alexander-turner added a commit to alexander-turner/jest that referenced this pull request Mar 27, 2026
- Fix bug: stack-based (LIFO) traversal reversed describe block order;
  switch back to recursive traversal to preserve source order
- Fix CHANGELOG link: point to jestjs#16006 not fork PR
- Disable coverage collection in collectOnly mode (wasted work)
- Add "preserves source order" regression test
- Tighten unit tests: simplify helper, remove over-assertions,
  extract collect() shorthand, reduce from 111 to 96 lines

https://claude.ai/code/session_018SbTXFCAujDRRjBFtMTFXU
Add a new CLI flag that loads test files and registers all describe/test/it
blocks, then prints a tree showing discovered tests without running them.

- Circus adapter returns collected results with pending status when
  collectOnly is set, using getTestID for testNamePattern filtering
- runJest.ts prints test tree via VerboseReporter.groupTestsBySuites
  and delegates JSON/file output to existing processResults
- Supports --testNamePattern filtering and --json output
- Exits with code 0 regardless of test count
- Disables coverage collection (unnecessary without execution)

https://claude.ai/code/session_018SbTXFCAujDRRjBFtMTFXU
@alexander-turner alexander-turner force-pushed the claude/add-jest-collect-only-2ea6Q branch from fc5fefe to 44b35ce Compare March 27, 2026 16:52
@SimenB
Copy link
Copy Markdown
Member

SimenB commented Mar 27, 2026

Thanks for the PR!

we have --list-tests already (which just lists the test files, doesn't look in them). should this be --collect-tests to keep somewhat the same name?

Rename from --collectOnly to --collectTests for consistency with the
existing --listTests flag. The flag loads test files and registers all
describe/test/it blocks, prints a tree of discovered tests, and exits
with code 0.

- Circus adapter returns collected results with pending status,
  filtering by testNamePattern via existing getTestID utility
- runJest prints test tree via VerboseReporter.groupTestsBySuites
  and delegates JSON/file output to existing processResults
- Coverage collection disabled (unnecessary without execution)

https://claude.ai/code/session_018SbTXFCAujDRRjBFtMTFXU
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