Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Sep 27, 2023
1 parent 0a9c230 commit 4542cdd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 41 deletions.
18 changes: 8 additions & 10 deletions packages/snapshot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@ import { SnapshotClient } from '@vitest/snapshot'
import { NodeSnapshotEnvironment } from '@vitest/snapshot/environment'
import { SnapshotManager } from '@vitest/snapshot/manager'

export class CustomSnapshotClient extends SnapshotClient {
// by default, @vitest/snapshot checks equality with `!==`
// you need to provide your own equality check implementation
const client = new SnapshotClient({
// you need to provide your own equality check implementation if you use it
// this function is called when `.toMatchSnapshot({ property: 1 })` is called
equalityCheck(received, expected) {
return equals(received, expected, [iterableEquality, subsetEquality])
}
}
isEqual: (received, expected) => equals(received, expected, [iterableEquality, subsetEquality]),
})

const client = new CustomSnapshotClient()
// class that implements snapshot saving and reading
// by default uses fs module, but you can provide your own implementation depending on the environment
const environment = new NodeSnapshotEnvironment()
Expand All @@ -30,6 +26,8 @@ function getCurrentTestName() {
return 'test1'
}

// example for inline snapshots, nothing is required to support regular snapshots,
// just call `assert` with `isInline: false`
function wrapper(received) {
function __INLINE_SNAPSHOT__(inlineSnapshot, message) {
client.assert({
Expand All @@ -55,14 +53,14 @@ const options = {
snapshotEnvironment: environment,
}

await client.setTest(getCurrentFilepath(), getCurrentTestName(), options)
await client.startCurrentRun(getCurrentFilepath(), getCurrentTestName(), options)

// uses "pretty-format", so it requires quotes
// also naming is hard-coded when parsing test files
wrapper('text 1').toMatchInlineSnapshot()
wrapper('text 2').toMatchInlineSnapshot('"text 2"')

const result = await client.resetCurrent() // this saves files and returns SnapshotResult
const result = await client.finishCurrentRun() // this saves files and returns SnapshotResult

// you can use manager to manage several clients
const manager = new SnapshotManager(options)
Expand Down
25 changes: 10 additions & 15 deletions packages/snapshot/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,29 @@ interface AssertOptions {
rawSnapshot?: RawSnapshotInfo
}

export interface SnapshotClientOptions {
isEqual?: (received: unknown, expected: unknown) => boolean
}

export class SnapshotClient {
filepath?: string
name?: string
snapshotState: SnapshotState | undefined
snapshotStateMap = new Map<string, SnapshotState>()

constructor(private Service = SnapshotState) {}
constructor(private options: SnapshotClientOptions = {}) {}

async setTest(filepath: string, name: string, options: SnapshotStateOptions) {
async startCurrentRun(filepath: string, name: string, options: SnapshotStateOptions) {
this.filepath = filepath
this.name = name

if (this.snapshotState?.testFilePath !== filepath) {
this.resetCurrent()
await this.finishCurrentRun()

if (!this.getSnapshotState(filepath)) {
this.snapshotStateMap.set(
filepath,
await this.Service.create(
await SnapshotState.create(
filepath,
options,
),
Expand All @@ -80,15 +84,6 @@ export class SnapshotClient {
this.snapshotState?.markSnapshotsAsCheckedForTest(name)
}

/**
* Should be overridden by the consumer.
*
* Vitest checks equality with @vitest/expect.
*/
equalityCheck(received: unknown, expected: unknown) {
return received === expected
}

assert(options: AssertOptions): void {
const {
filepath = this.filepath,
Expand All @@ -111,7 +106,7 @@ export class SnapshotClient {
throw new Error('Received value must be an object when the matcher has properties')

try {
const pass = this.equalityCheck(received, properties)
const pass = this.options.isEqual?.(received, properties) ?? false
// const pass = equals(received, properties, [iterableEquality, subsetEquality])
if (!pass)
throw createMismatchError('Snapshot properties mismatched', received, properties)
Expand Down Expand Up @@ -169,7 +164,7 @@ export class SnapshotClient {
return this.assert(options)
}

async resetCurrent() {
async finishCurrentRun() {
if (!this.snapshotState)
return null
const result = await this.snapshotState.pack()
Expand Down
14 changes: 9 additions & 5 deletions packages/vitest/src/integrations/snapshot/chai.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import type { ChaiPlugin } from '@vitest/expect'
import { equals, iterableEquality, subsetEquality } from '@vitest/expect'
import { SnapshotClient, addSerializer, stripSnapshotIndentation } from '@vitest/snapshot'
import type { Test } from '@vitest/runner'
import { getNames } from '@vitest/runner/utils'
import type { SnapshotClient } from '@vitest/snapshot'
import { addSerializer, stripSnapshotIndentation } from '@vitest/snapshot'
import { recordAsyncExpect } from '../../../../expect/src/utils'
import { VitestSnapshotClient } from './client'

let _client: SnapshotClient

export function getSnapshotClient(): SnapshotClient {
if (!_client)
_client = new VitestSnapshotClient()
if (!_client) {
_client = new SnapshotClient({
isEqual: (received, expected) => {
return equals(received, expected, [iterableEquality, subsetEquality])
},
})
}
return _client
}

Expand Down
8 changes: 0 additions & 8 deletions packages/vitest/src/integrations/snapshot/client.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/vitest/src/runtime/runners/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export class VitestTestRunner implements VitestRunner {
this.snapshotClient.clear()
}

async onAfterRunFile() {
const result = await this.snapshotClient.resetCurrent()
async onAfterRunFiles() {
const result = await this.snapshotClient.finishCurrentRun()
if (result)
await rpc().snapshotSaved(result)
}
Expand Down Expand Up @@ -63,7 +63,7 @@ export class VitestTestRunner implements VitestRunner {
}

clearModuleMocks(this.config)
await this.snapshotClient.setTest(test.file!.filepath, name, this.workerState.config.snapshotOptions)
await this.snapshotClient.startCurrentRun(test.file!.filepath, name, this.workerState.config.snapshotOptions)

this.workerState.current = test
}
Expand Down

0 comments on commit 4542cdd

Please sign in to comment.