-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
client.ts
127 lines (109 loc) · 3.38 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import path from 'pathe'
import { expect } from 'chai'
import type { SnapshotResult, Test } from '../../types'
import { rpc } from '../../runtime/rpc'
import { getNames } from '../../utils'
import { equals, iterableEquality, subsetEquality } from '../chai/jest-utils'
import { deepMergeSnapshot } from './port/utils'
import SnapshotState from './port/state'
export interface Context {
file: string
title?: string
fullTitle?: string
}
const resolveSnapshotPath = (testPath: string) =>
path.join(
path.join(path.dirname(testPath), '__snapshots__'),
`${path.basename(testPath)}.snap`,
)
export class SnapshotClient {
test: Test | undefined
testFile = ''
snapshotState: SnapshotState | undefined
setTest(test: Test) {
this.test = test
if (this.testFile !== this.test.file!.filepath) {
if (this.snapshotState)
this.saveSnap()
this.testFile = this.test!.file!.filepath
this.snapshotState = new SnapshotState(
resolveSnapshotPath(this.testFile),
__vitest_worker__!.config.snapshotOptions,
)
}
}
clearTest() {
this.test = undefined
}
assert(received: unknown, message?: string, isInline = false, properties?: object, inlineSnapshot?: string, error?: Error): void {
if (!this.test)
throw new Error('Snapshot cannot be used outside of test')
if (typeof properties === 'object') {
if (typeof received !== 'object' || !received)
throw new Error('Received value must be an object when the matcher has properties')
try {
const pass = equals(received, properties, [iterableEquality, subsetEquality])
if (!pass)
expect(received).equals(properties)
else
received = deepMergeSnapshot(received, properties)
}
catch (err: any) {
err.message = 'Snapshot mismatched'
throw err
}
}
const testName = [
...getNames(this.test).slice(1),
...(message ? [message] : []),
].join(' > ')
const { actual, expected, key, pass } = this.snapshotState!.match({
testName,
received,
isInline,
error,
inlineSnapshot,
})
if (!pass) {
try {
expect(actual.trim()).equals(expected ? expected.trim() : '')
}
catch (error: any) {
error.message = `Snapshot \`${key || 'unknown'}\` mismatched`
throw error
}
}
}
async saveSnap() {
if (!this.testFile || !this.snapshotState) return
const result = await packSnapshotState(this.testFile, this.snapshotState)
await rpc().snapshotSaved(result)
this.testFile = ''
this.snapshotState = undefined
}
}
export async function packSnapshotState(filepath: string, state: SnapshotState): Promise<SnapshotResult> {
const snapshot: SnapshotResult = {
filepath,
added: 0,
fileDeleted: false,
matched: 0,
unchecked: 0,
uncheckedKeys: [],
unmatched: 0,
updated: 0,
}
const uncheckedCount = state.getUncheckedCount()
const uncheckedKeys = state.getUncheckedKeys()
if (uncheckedCount)
state.removeUncheckedKeys()
const status = await state.save()
snapshot.fileDeleted = status.deleted
snapshot.added = state.added
snapshot.matched = state.matched
snapshot.unmatched = state.unmatched
snapshot.updated = state.updated
snapshot.unchecked = !status.deleted ? uncheckedCount : 0
snapshot.uncheckedKeys = Array.from(uncheckedKeys)
return snapshot
}