fix: respect diff config options in soft assertions #8696
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.
Problem
When using
expect.soft()assertions, error diffs were generated using default options instead of respecting the user's custom diff configuration fromvitest.config.ts. This meant that options liketest.diff.printBasicPrototypewere ignored for soft assertion failures, while regular assertions correctly applied these settings.For example, with this configuration:
Regular assertions would show diffs with
Object {andArray [prefixes, but soft assertions would not.Root Cause
In
packages/expect/src/utils.ts, thehandleTestErrorfunction was callingprocessError(err)without passing thediffOptionsparameter. This caused soft assertion errors to be processed with default diff settings regardless of the user's configuration.Solution
This PR makes two minimal changes:
Export
getRunnerfrom@vitest/runner- Makes the runner instance accessible to other packages that need to access runtime configuration.Pass
diffOptionswhen processing soft assertion errors - ThehandleTestErrorfunction now retrieves the runner's config and passesdiffOptionstoprocessError(), ensuring soft assertions respect the user's diff configuration.Testing
This fix ensures consistent diff formatting across all assertion types (regular and soft), providing a better developer experience when viewing test failures in any reporter or IDE integration.
Closes #XXXX
Original prompt
This section details on the original issue you should resolve
<issue_title>[Reporter] IntelliJ diff view ignores Vitest config options – shows default diff (e.g. test.diff.printBasicPrototype)</issue_title>
<issue_description>### Describe the bug
1. What the problem actually is
When a test fails, Intellij Vitest Plugin shows an “AssertionError” that contains a link called “”. In the IntelliJ (possibly the VS Code extension) clicking that link opens a diff view that is built from the
err.expectedanderr.actualvalues which are set here https://github.com/vitest-dev/vitest/blob/main/packages/utils/src/error.ts#L31-L36The diff view is rendered by
pretty‑format(seepackages/pretty-format/src/index.ts#L521).That function receives a set of diff options (
DiffOptions) that come from the user’svitest.config.ts.In the current implementation of
processError(seepackages/utils/src/error.ts) those optionsare never forwarded to the stringifier:
Because the options are omitted,
pretty‑formatfalls back to its default diff behaviour(
maxDepth: 10,showDiff: true…).That means the diff that IntelliJ shows ignores any configuration you set in
vitest.config.ts(e.g.maxDepth,showDiff,truncateThreshold, etc.)2. Where the same pattern shows up elsewhere
packages/utils/src/error.tsexpected/actualfor the error message.packages/pretty-format/src/index.tsstringify()is called without options.maxDepth: 10,showDiff: true).packages/utils/src/error.ts(issue #5353)err.actual = getObjectSubset(replacedActual, replacedExpected);followed bydiff(...)with{ ...diffOptions, ...err.diffOptions }.diffOptionswith any per‑error overrides.packages/utils/src/error.ts(issue #5073)stringify(err.expected, 10)pattern.The PR for #5353 (see the comment “After patching the
processErrorfunction”) is a perfectreference for how to merge the configuration:
3. Proposed fix
Why this works
stringify(value, depth, options?)accepts a third argument – the sameDiffOptionsthatpretty‑formatuses for diffs.as configured in
vitest.config.ts.behaviour remains unchanged.
4. Quick‑start test
Add a unit test in
packages/utils/test/error.test.ts: