-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Add diffFormat configuration option #15937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
aghiles-dd
wants to merge
7
commits into
jestjs:main
Choose a base branch
from
aghiles-dd:feat/add-diffFormat-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
+284
−7
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This commit adds a new diffFormat configuration option to Jest that allows
users to customize the formatting of diffs in test output. The initial
implementation includes a printBasicPrototype option that controls whether
type annotations (like 'Array [' and 'Object {') are displayed in diff output.
When printBasicPrototype is set to false, diffs will show clean JSON without
type annotations, making output more readable for certain use cases.
Changes:
- Add DiffFormat type to @jest/schemas with printBasicPrototype option
- Add diffFormat field to Config types (GlobalConfig and ProjectConfig)
- Thread diffFormat.printBasicPrototype through test runners to expect matchers
- Update jest-diff to accept and use printBasicPrototype from DiffOptions
- Update jest-matcher-utils to pass printBasicPrototype to diff functions
- Update expect matchers to extract and pass printBasicPrototype from state
The configuration flows as:
jest.config.js (diffFormat) → Global Config → Test Runner → Expect State →
Matchers → jest-matcher-utils → jest-diff → pretty-format
Fixes jestjs#15933
✅ Deploy Preview for jestjs ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
- Add optional chaining for diffFormat config access to prevent crashes - Add getPrintBasicPrototype helper to normalize undefined values - Thread printBasicPrototype through toThrowExpectedObject matcher - Make diffFormat optional in jestExpect adapter signature These changes ensure backward compatibility when diffFormat is not configured by properly handling undefined values throughout the chain.
Critical fixes:
- Add diffFormat to InitialOptions schema for config validation
- Add default value in Defaults.ts: {printBasicPrototype: true}
- Add normalization case in normalize.ts to merge user config with defaults
This ensures diffFormat is properly initialized and validated throughout
the Jest configuration system, preventing crashes when the option is
not configured by the user.
Critical fixes for config system: - Add diffFormat to GlobalConfig builder in index.ts - Add diffFormat to ProjectConfig builder in index.ts - Add diffFormat to ValidConfig examples for validation testing Without these changes, diffFormat would not be properly passed from the normalized config to the global and project configs, causing the option to be lost even after normalization.
Add comprehensive documentation for the new diffFormat config option in
the Configuration.md file. The documentation:
- Shows the default value: {printBasicPrototype: true}
- Explains the purpose: controls diff output formatting in test failures
- Provides examples in both JavaScript and TypeScript
- Shows side-by-side comparison of diff output with and without type annotations
- Follows the same documentation pattern as snapshotFormat
The documentation is placed alphabetically between dependencyExtractor
and displayName sections.
Add tests across multiple packages to verify diffFormat config:
1. jest-config tests (normalize.test.ts):
- Verify default diffFormat is {printBasicPrototype: true}
- Test merging user diffFormat with defaults
- Ensure partial diffFormat preserves defaults
2. jest-diff tests (diff.test.ts):
- Test printBasicPrototype: true shows type annotations
- Test printBasicPrototype: false hides type annotations
- Verify default behavior uses true
3. expect integration tests (matchers.test.js):
- Test toEqual matcher respects printBasicPrototype: false
- Test toEqual matcher respects printBasicPrototype: true
- Test toStrictEqual matcher works with printBasicPrototype
- Added afterEach hook for proper state cleanup
4. test-utils config fix (config.ts):
- Add diffFormat to DEFAULT_GLOBAL_CONFIG
- Add diffFormat to DEFAULT_PROJECT_CONFIG
- Fixes TypeScript error about missing required field
These tests verify the complete flow from config normalization
through to actual diff output in matchers.
babel-jest
babel-plugin-jest-hoist
babel-preset-jest
create-jest
@jest/diff-sequences
expect
@jest/expect-utils
jest
jest-changed-files
jest-circus
jest-cli
jest-config
@jest/console
@jest/core
@jest/create-cache-key-function
jest-diff
jest-docblock
jest-each
@jest/environment
jest-environment-jsdom
@jest/environment-jsdom-abstract
jest-environment-node
@jest/expect
@jest/fake-timers
@jest/get-type
@jest/globals
jest-haste-map
jest-jasmine2
jest-leak-detector
jest-matcher-utils
jest-message-util
jest-mock
@jest/pattern
jest-phabricator
jest-regex-util
@jest/reporters
jest-resolve
jest-resolve-dependencies
jest-runner
jest-runtime
@jest/schemas
jest-snapshot
@jest/snapshot-utils
@jest/source-map
@jest/test-result
@jest/test-sequencer
@jest/transform
@jest/types
jest-util
jest-validate
jest-watcher
jest-worker
pretty-format
commit: |
- Fix object key ordering: move diffFormat to alphabetical position - After detectOpenHandles/displayName, before errorOnDeprecated - Applied to Defaults.ts, ValidConfig.ts, index.ts, and test-utils - Fix catch parameter naming: rename 'e' to 'error_' in matchers tests - Follows Jest coding standards for catch blocks These changes address CI failures from ESLint rules.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds a new
diffFormatconfiguration option to Jest that allows users to customize the formatting of diffs in test output. The initial implementation includes aprintBasicPrototypeoption that controls whether type annotations (likeArray [andObject {) are displayed in diff output.When
printBasicPrototypeis set tofalse, diffs will show clean JSON without type annotations, making output more readable.Motivation
Related to #15933
Users have requested the ability to hide type annotations in diff output to make test failures more readable, especially when working with JSON-like data structures.
Changes
DiffFormattype to@jest/schemaswithprintBasicPrototypeoptiondiffFormatfield to Config types (GlobalConfigandProjectConfig)diffFormat.printBasicPrototypethrough test runners to expect matchersjest-diffto accept and useprintBasicPrototypefromDiffOptionsjest-matcher-utilsto passprintBasicPrototypeto diff functionsprintBasicPrototypefrom stateConfiguration Flow
Example Configuration
Test Plan
Tests and documentation will be added based on maintainer feedback.
Checklist