@vitest/snapshot
Lightweight implementation of Jest's snapshots.
Usage
import { SnapshotClient } from '@vitest/snapshot'
import { NodeSnapshotEnvironment } from '@vitest/snapshot/environment'
import { SnapshotManager } from '@vitest/snapshot/manager'
export class CustomSnapshotClient extends SnapshotClient {
equalityCheck(received, expected) {
return equals(received, expected, [iterableEquality, subsetEquality])
}
}
const client = new CustomSnapshotClient()
const environment = new NodeSnapshotEnvironment()
function getCurrentFilepath() {
return '/file.spec.ts'
}
function getCurrentTestName() {
return 'test1'
}
function wrapper(received) {
function __INLINE_SNAPSHOT__(inlineSnapshot, message) {
client.assert({
received,
message,
isInline: true,
inlineSnapshot,
filepath: getCurrentFilepath(),
name: getCurrentTestName(),
})
}
return {
toMatchInlineSnapshot: (...args) => __INLINE_SNAPSHOT__(...args),
}
}
const options = {
updateSnapshot: 'new',
snapshotEnvironment: environment,
}
await client.setTest(getCurrentFilepath(), getCurrentTestName(), options)
wrapper('text 1').toMatchInlineSnapshot()
wrapper('text 2').toMatchInlineSnapshot('"text 2"')
const result = await client.resetCurrent()
const manager = new SnapshotManager(options)
manager.add(result)
console.log(manager.summary)