Skip to content

Conversation

@aghiles-dd
Copy link

Summary

This PR 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.

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

  • 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

Configuration Flow

jest.config.js (diffFormat) 
  → Global Config 
  → Test Runner (jest-circus/jest-jasmine2)
  → Expect State 
  → Matchers 
  → jest-matcher-utils 
  → jest-diff 
  → pretty-format

Example Configuration

// jest.config.js
module.exports = {
  diffFormat: {
    printBasicPrototype: false
  }
};

Test Plan

Tests and documentation will be added based on maintainer feedback.

Checklist

  • Implementation complete
  • Tests added
  • Documentation updated
  • No breaking changes

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
@netlify
Copy link

netlify bot commented Jan 8, 2026

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit dc7f3ad
🔍 Latest deploy log https://app.netlify.com/projects/jestjs/deploys/696198d074fd9b0008e12122
😎 Deploy Preview https://deploy-preview-15937--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

linux-foundation-easycla bot commented Jan 8, 2026

CLA Not Signed

@aghiles-dd aghiles-dd marked this pull request as draft January 8, 2026 14:57
- 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.
@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 9, 2026

Open in StackBlitz

babel-jest

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

babel-plugin-jest-hoist

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

babel-preset-jest

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

create-jest

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

@jest/diff-sequences

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

expect

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

@jest/expect-utils

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

jest

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

jest-changed-files

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

jest-circus

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

jest-cli

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

jest-config

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

@jest/console

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

@jest/core

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

@jest/create-cache-key-function

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

jest-diff

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

jest-docblock

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

jest-each

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

@jest/environment

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

jest-environment-jsdom

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

@jest/environment-jsdom-abstract

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

jest-environment-node

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

@jest/expect

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

@jest/fake-timers

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

@jest/get-type

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

@jest/globals

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

jest-haste-map

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

jest-jasmine2

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

jest-leak-detector

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

jest-matcher-utils

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

jest-message-util

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

jest-mock

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

@jest/pattern

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

jest-phabricator

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

jest-regex-util

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

@jest/reporters

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

jest-resolve

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

jest-resolve-dependencies

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

jest-runner

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

jest-runtime

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

@jest/schemas

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

jest-snapshot

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

@jest/snapshot-utils

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

@jest/source-map

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

@jest/test-result

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

@jest/test-sequencer

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

@jest/transform

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

@jest/types

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

jest-util

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

jest-validate

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

jest-watcher

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

jest-worker

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

pretty-format

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

commit: dc7f3ad

- 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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant