Skip to content

Conversation

Shinyaigeek
Copy link
Contributor

@Shinyaigeek Shinyaigeek commented Jul 22, 2025

Description

Close: #4060

スクリーンショット 2025-07-23 2 08 54

In this ticket, I implemented to display test's snapshot result on vitest-ui. Please feel free to provide any feedback about UserInterface(=vitest/ui itself) and also data flow design.

In this implementation, vitest/ui can display added/matched/unmatched/unchecked snapshots result. Vitest node environment(=vm context) executes tests and generate snapshot result, and passes that result to vitest core context via rpc(), and vitest-ui fetches some data from vitest core context. However, vitest node environment passes only snapshot diff count information to vitest core context. in current implementation. So I changed to pass more detailed snapshot result to vitest core context to display these in vitest-ui.

If you have any concern or something unclear, please feel free to pointed out!

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:.

Copy link

netlify bot commented Jul 22, 2025

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit a24c7b5
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/687fc75e729fb900081334d3
😎 Deploy Preview https://deploy-preview-8335--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.

Copy link
Member

@sheremet-va sheremet-va left a comment

Choose a reason for hiding this comment

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

I wonder if we can display them in a similar way to annotations (but hidden by default, unless there is an error). This will require storing more information in Test (maybe snapshot names and location?) Also regarding existing code, maybe if the snapshot failed, we should show the diff?


add(result: SnapshotResult): void {
addSnapshotResult(this.summary, result)
this.result = result
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need a separate variable? What does result represent?

<div v-if="snapshotData.snapshots.unmatched.length > 0" class="space-y-2">
<details class="group" open>
<summary class="cursor-pointer p-2 bg-red-50 dark:bg-red-950 border border-red-200 dark:border-red-800 rounded text-red-700 dark:text-red-300 font-medium text-sm">
❌ Unmatched Snapshots ({{ snapshotData.snapshots.unmatched.length }})
Copy link
Member

Choose a reason for hiding this comment

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

What is "unmatched" snapshot?

<div v-if="snapshotData.snapshots.added.length > 0" class="space-y-2">
<details class="group" open>
<summary class="cursor-pointer p-2 bg-green-50 dark:bg-green-950 border border-green-200 dark:border-green-800 rounded text-green-700 dark:text-green-300 font-medium text-sm">
🆕 Added Snapshots ({{ snapshotData.snapshots.added.length }})
Copy link
Member

Choose a reason for hiding this comment

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

Let's not use emojis, we have iconify icons available (see dependencies for from which library)

async getSnapshotData(): Promise<SnapshotData | void> {
try {
// Get snapshot result from the Vitest context
const snapshotResult = ctx.snapshot.result
Copy link
Member

Choose a reason for hiding this comment

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

Seems like this is sensitive to race conditions? result doesn't represent all of snapshots, or? Only for a specific file?

export interface SnapshotData {
filepath: string
snapshots: {
added: Array<{
Copy link
Member

Choose a reason for hiding this comment

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

This can probably be its own type, so we don't need to repeat it

@Shinyaigeek
Copy link
Contributor Author

@sheremet-va Thank you for your feedback!

I wonder if we can display them in a similar way to annotations (but hidden by default, unless there is an error). This will require storing more information in Test (maybe snapshot names and location?)

Is my understanding correct that, in this PR, instead of displaying Snapshots within a dedicated Snapshot Tab, you're proposing to have an expandable Annotation Block within the Code Tab, similar to Test Annotations in an HTML Reporter, at the location where Snapshot comparisons are performed in the code, and expanding it would allow users to view the Snapshot? If so, that sounds good! Also, in that case, InlineSnapshot might be ignored since its content is already present within the code itself.

Also regarding existing code, maybe if the snapshot failed, we should show the diff?

That's true! Failed snapshots are displayed in Report Tab in current implementation, so maybe we don't need to display failed snapshot results in Code(or Snapshot) Tab duplicatedly.

@sheremet-va
Copy link
Member

sheremet-va commented Jul 25, 2025

Is my understanding correct that, in this PR, instead of displaying Snapshots within a dedicated Snapshot Tab, you're proposing to have an expandable Annotation Block within the Code Tab, similar to Test Annotations in an HTML Reporter, at the location where Snapshot comparisons are performed in the code, and expanding it would allow users to view the Snapshot? If so, that sounds good! Also, in that case, InlineSnapshot might be ignored since its content is already present within the code itself.

Yes, that is what I am proposing. We can probably reuse annotations for these. If needed, the type can be extended. Or we can also introduce a separate field that is only available after the test has finished (annotations appear while the test is running and have a separate reporter event which can make the reporting slower). I think I would prefer to reduce the amount of text that we have to pass down between UI and Node because it's JSON.stringified which takes slower the longer the string is. (Maybe we should store the name of the snapshot and load the content separately)

That's true! Failed snapshots are displayed in Report Tab in current implementation, so maybe we don't need to display failed snapshot results in Code(or Snapshot) Tab duplicatedly.

Makes sense, let's not duplicate it.

@Shinyaigeek
Copy link
Contributor Author

Thank you! I could grasp what I should do! I will implement the snapshot visualization feature according to what should be done, so please let me more time to implement this. (perhaps, I will re-create PullRequest if the most of implementation in this PR is not needed, but I cannot judge at least now)

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.

Display Snapshot of the component in Vitest UI

2 participants