Skip to content

fix(browser): take failure screenshot if toMatchScreenshot can't capture a stable screenshot#9847

Merged
sheremet-va merged 4 commits intovitest-dev:mainfrom
macarie:fix/screenshot-on-unstable
Mar 24, 2026
Merged

fix(browser): take failure screenshot if toMatchScreenshot can't capture a stable screenshot#9847
sheremet-va merged 4 commits intovitest-dev:mainfrom
macarie:fix/screenshot-on-unstable

Conversation

@macarie
Copy link
Member

@macarie macarie commented Mar 11, 2026

Description

Resolves #9690

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

Comment on lines +123 to +126
meta: {
assertion: 'toMatchScreenshot',
outcome: result.outcome,
},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is the best way to accomplish this, but I didn't find other ways to check source/origin of an error and to attach additional data

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, but one question. Is assertion here and expectAssertionName available in JestExtendPlugin always same?

([expectAssertionName, expectAssertion]) => {

We could always add assertion as error metadata in the plugin level, then move properties like JestExtendError.contenxt: { assertionName, meta }.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could definitely do that — I wasn't sure if it made sense to add it to all errors, but I guess it might be useful in the future 👍🏼

@macarie macarie marked this pull request as ready for review March 11, 2026 16:16
@macarie macarie requested a review from sheremet-va March 11, 2026 16:17
Comment on lines +77 to +81
setContext(assertionName: string, meta?: object): this {
this.context = { assertionName, meta }

return this
}
Copy link
Member Author

@macarie macarie Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be the 4th constructor parameter instead?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. I was wondering JestExtendError.constructor is somehow public, but probably not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it doesn't seem to be public. It's only used in this file

@hi-ogawa
Copy link
Collaborator

Is this testable?

@macarie macarie force-pushed the fix/screenshot-on-unstable branch from 7510173 to e272d74 Compare March 23, 2026 09:51
@netlify
Copy link

netlify bot commented Mar 23, 2026

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit e272d74
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/69c10d121ab3d80008dc68dd
😎 Deploy Preview https://deploy-preview-9847--vitest-dev.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.


describe('failure screenshots', () => {
describe('`toMatchScreenshot`', () => {
test('usually does NOT produce a failure screenshot', async () => {
Copy link
Member Author

@macarie macarie Mar 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should technically control all exit paths of the assertion, but I don't think it's worth the added time in each CI run. It might be easier to just test the cases that expect the opposite (like the one below) as the condition is quite straightforward.

Edit: since the line this comment is talking about is not clear in the UI, the subject is the test with the usually does NOT produce a failure screenshot description.

@macarie macarie requested a review from hi-ogawa March 23, 2026 11:24
@sheremet-va sheremet-va merged commit faace1f into vitest-dev:main Mar 24, 2026
38 of 40 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.

[Regression] Browser mode: Failure screenshot not created when unstable error occurs in v4.1.0-beta.4

3 participants