Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(snapshot): ability to skip snapshot from custom serializer + attest example #6852

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ test/**/__screenshots__/**/*
test/browser/fixtures/update-snapshot/basic.test.ts
test/cli/fixtures/browser-multiple/basic-*
.vitest-reports
.attest
5 changes: 5 additions & 0 deletions packages/pretty-format/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ function printPlugin(
)
}
catch (error: any) {
if (error instanceof PrettyFormatSkipSnapshotError) {
throw error
}
throw new PrettyFormatPluginError(error.message, error.stack)
}
if (typeof printed !== 'string') {
Expand Down Expand Up @@ -543,3 +546,5 @@ export const plugins: {
ReactElement,
ReactTestComponent,
}

export class PrettyFormatSkipSnapshotError extends Error {}
2 changes: 1 addition & 1 deletion packages/snapshot/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { SnapshotClient } from './client'

export { stripSnapshotIndentation } from './port/inlineSnapshot'
export { addSerializer, getSerializers } from './port/plugins'
export { addSerializer, getSerializers, skipSnapshot } from './port/plugins'
export { default as SnapshotState } from './port/state'

export type {
Expand Down
6 changes: 5 additions & 1 deletion packages/snapshot/src/port/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
Plugin as PrettyFormatPlugin,
Plugins as PrettyFormatPlugins,
} from '@vitest/pretty-format'
import { plugins as prettyFormatPlugins } from '@vitest/pretty-format'
import { plugins as prettyFormatPlugins, PrettyFormatSkipSnapshotError } from '@vitest/pretty-format'

import MockSerializer from './mockSerializer'

Expand Down Expand Up @@ -39,3 +39,7 @@ export function addSerializer(plugin: PrettyFormatPlugin): void {
export function getSerializers(): PrettyFormatPlugins {
return PLUGINS
}

export function skipSnapshot(): never {
throw new PrettyFormatSkipSnapshotError()
}
20 changes: 15 additions & 5 deletions packages/snapshot/src/port/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import type {
} from '../types'
import type { InlineSnapshot } from './inlineSnapshot'
import type { RawSnapshot, RawSnapshotInfo } from './rawSnapshot'
import { PrettyFormatSkipSnapshotError } from '@vitest/pretty-format'
import { parseErrorStacktrace } from '../../../utils/src/source-map'

import { saveInlineSnapshots } from './inlineSnapshot'
import { saveRawSnapshots } from './rawSnapshot'

import {
addExtraLineBreaks,
getSnapshotData,
Expand Down Expand Up @@ -250,10 +251,19 @@ export default class SnapshotState {
this._uncheckedKeys.delete(key)
}

let receivedSerialized
= rawSnapshot && typeof received === 'string'
? (received as string)
: serialize(received, undefined, this._snapshotFormat)
let receivedSerialized: string
try {
receivedSerialized
= rawSnapshot && typeof received === 'string'
? (received as string)
: serialize(received, undefined, this._snapshotFormat)
}
catch (e) {
if (e instanceof PrettyFormatSkipSnapshotError) {
return { pass: true, actual: '', key, count }
}
throw e
}

if (!rawSnapshot) {
receivedSerialized = addExtraLineBreaks(receivedSerialized)
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ export type {
SnapshotUpdateState,
UncheckedSnapshot,
} from '@vitest/snapshot'
export { skipSnapshot } from '@vitest/snapshot'

/** @deprecated import from `vitest/node` instead */
export type BrowserScript = BrowserScript_
Expand Down
Loading
Loading