Skip to content

feat(snapshot): support custom snapshot matcher#9973

Open
hi-ogawa wants to merge 61 commits intovitest-dev:mainfrom
hi-ogawa:feat-support-custom-snapshot-matcher
Open

feat(snapshot): support custom snapshot matcher#9973
hi-ogawa wants to merge 61 commits intovitest-dev:mainfrom
hi-ogawa:feat-support-custom-snapshot-matcher

Conversation

@hi-ogawa
Copy link
Copy Markdown
Collaborator

@hi-ogawa hi-ogawa commented Mar 26, 2026

Description

This PR adds composable toMatchSnapshot, toMatchInlineSnapshot, and toMatchFileSnapshot helper exported from vitest/runtime, enabling users to build custom snapshot matchers with expect.extend similar to jest-snapshot allows.

This closes the gap between expect.extend and vitest's snapshot infrastructure — custom matchers now get full snapshot lifecycle support (creation, mismatch detection, inline rewriting, update) without workarounds.

Additionally, builtin snapshot matchers are refactored to go through toMatchSnapshotImpl as single snapshot integration point internally within packages/vitest/src/integrations/snapshot/chai.ts.

Example

import { toMatchFileSnapshot, toMatchInlineSnapshot, toMatchSnapshot } from 'vitest/runtime'

expect.extend({
  toMatchTrimmedSnapshot(received: string) {
    return toMatchSnapshot.call(this, received.slice(0, 10))
  },
  toMatchTrimmedInlineSnapshot(received: string, inlineSnapshot?: string) {
    return toMatchInlineSnapshot.call(this, received.slice(0, 10), inlineSnapshot)
  },
  async toMatchTrimmedFileSnapshot(received: string, filepath: string) {
    return toMatchFileSnapshot.call(this, received.slice(0, 10), filepath)
  },
})

test('file snapshot', () => {
  expect('extra long string oh my gerd').toMatchTrimmedSnapshot()
})

test('inline snapshot', () => {
  expect('extra long string oh my gerd').toMatchTrimmedInlineSnapshot(`"extra long"`)
})

test('raw file snapshot', async () => {
  await expect('extra long string oh my gerd').toMatchFileTrimmedSnapshot('./raw.txt')
})

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

@netlify
Copy link
Copy Markdown

netlify bot commented Mar 26, 2026

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

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

@hi-ogawa hi-ogawa marked this pull request as ready for review March 27, 2026 08:15
@sheremet-va sheremet-va moved this to Approved in Team Board Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Approved

Development

Successfully merging this pull request may close these issues.

Support building custom snapshot matchers with expect.extend

2 participants