-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathvitest.helper.mts
More file actions
304 lines (273 loc) · 9.54 KB
/
vitest.helper.mts
File metadata and controls
304 lines (273 loc) · 9.54 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import type { Reporter } from '@guanghechen/reporter'
import fs from 'node:fs'
import fsp from 'node:fs/promises'
import path from 'node:path'
import url from 'node:url'
import { expect, vi } from 'vitest'
import type { MockInstance } from 'vitest'
// ============================================================================
// Inline utilities from @guanghechen/std to avoid import/no-extraneous-dependencies
// ============================================================================
const identity = <T,>(data: T): T => data
const isArray = (v: unknown): v is unknown[] => Array.isArray(v)
const isNumber = (v: unknown): v is number =>
Object.prototype.toString.call(v) === '[object Number]'
const isObject = (v: unknown): v is object =>
Object.prototype.toString.call(v) === '[object Object]'
const isString = (v: unknown): v is string =>
Object.prototype.toString.call(v) === '[object String]'
// ============================================================================
// Desensitizer utilities (ported from @guanghechen/helper-jest)
// ============================================================================
export type IDesensitizer<T> = (data: T, key?: string) => T
interface IJsonDesensitizerOptions {
fallback?: IDesensitizer<unknown>
string?: IDesensitizer<string>
number?: IDesensitizer<number>
error?: IDesensitizer<Error>
}
export function createJsonDesensitizer(
valDesensitizers: IJsonDesensitizerOptions = {},
keyDesensitizer?: IDesensitizer<string>,
): IDesensitizer<unknown> {
const fallback = valDesensitizers.fallback == null ? identity : valDesensitizers.fallback
const desensitizers = {
key: keyDesensitizer == null ? identity : keyDesensitizer,
string: valDesensitizers.string == null ? fallback : valDesensitizers.string,
number: valDesensitizers.number == null ? fallback : valDesensitizers.number,
error: valDesensitizers.error == null ? fallback : valDesensitizers.error,
fallback,
}
const desensitize = (json: unknown, key?: string): unknown => {
if (isString(json)) return desensitizers.string(json, key) as string
if (isNumber(json)) return desensitizers.number(json, key) as number
if (json instanceof Error) return desensitizers.error(json, key) as Error
if (isArray(json)) {
return json.map((value, index) => desensitize(value, '' + index))
}
if (isObject(json)) {
const results: Record<string, unknown> = {}
for (const _key of Object.keys(json)) {
const k = desensitizers.key(_key) as string
results[k] = desensitize((json as Record<string, unknown>)[_key], _key)
}
return results
}
return desensitizers.fallback(json)
}
return desensitize
}
export function createFilepathDesensitizer(
baseDir: string,
replaceString = '<WORKSPACE>',
): IDesensitizer<string> {
const source = baseDir
.replace(/[\\/]*$/, '')
.replace(/[/\\]+/g, '/')
.replace(/([.+*?=^!:${}()[\]|/\\])/g, '\\$1')
.replace(/\\\//g, '[\\\\|/]')
const regex = new RegExp(source, 'g')
return (text: string) => text.replace(regex, replaceString)
}
export function composeStringDesensitizers(
...desensitizers: IDesensitizer<string>[]
): IDesensitizer<string> {
return (text: string, key?: string): string => {
let result = text
for (const desensitize of desensitizers) {
result = desensitize(result, key)
}
return result
}
}
const __dirname = path.dirname(url.fileURLToPath(import.meta.url))
export const workspaceRootDir = __dirname
/**
* Desensitize test data.
*/
export const desensitize: IDesensitizer<any> & IDesensitizer<string> = createJsonDesensitizer({
string: composeStringDesensitizers(
createFilepathDesensitizer(workspaceRootDir, '<$WORKSPACE$>'),
text => text.replace(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/, '<$Date$>'),
text => text.replace(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z/, '<$ISO-Date$>'),
),
}) as IDesensitizer<any>
/**
* Locate fixture filepath.
* @param p
* @returns
*/
export const locateFixtures = (...p: string[]): string => {
const relativePackagePath: string = path
.relative(workspaceRootDir, path.resolve())
.split(path.sep)
.slice(0, 2)
.join(path.sep)
const testRootDior: string = path.resolve(workspaceRootDir, relativePackagePath)
return path.resolve(testRootDior, '__test__/fixtures', ...p)
}
/**
* Load fixture filepath.
* @param p
* @returns
*/
export const loadFixtures = (...p: string[]): string =>
fs.readFileSync(locateFixtures(...p), 'utf-8')
// ============================================================================
// File system utilities (ported from @guanghechen/fs)
// ============================================================================
/**
* Remove all files under the given directory path.
* @param dirpath
* @param createIfNotExist
*/
export async function emptyDir(dirpath: string, createIfNotExist = true): Promise<void> {
if (fs.existsSync(dirpath)) {
if (!fs.statSync(dirpath).isDirectory()) {
throw new Error(`[emptyDir] not a directory. (${dirpath})`)
}
await rm(dirpath)
await fsp.mkdir(dirpath, { recursive: true })
} else {
if (createIfNotExist) await fsp.mkdir(dirpath, { recursive: true })
}
}
/**
* Create a path of directories.
* @param filepath the give file path
* @param isDir Whether the given path is a directory
*/
export function mkdirsIfNotExists(filepath: string, isDir: boolean): void {
const dirpath = isDir ? filepath : path.dirname(filepath)
if (fs.existsSync(dirpath)) return
fs.mkdirSync(dirpath, { recursive: true })
}
/**
* Remove filepath/dirpath recursively.
* @param fileOrDirPath
*/
export async function rm(fileOrDirPath: string): Promise<void> {
if (fs.existsSync(fileOrDirPath)) {
await fsp.rm(fileOrDirPath, { recursive: true, force: true })
}
}
/**
* Check whether if the filepath is a file path. (synchronizing)
* @param filepath file path
*/
export function isFileSync(filepath: string | null): boolean {
if (filepath == null) return false
if (!fs.existsSync(filepath)) return false
return fs.statSync(filepath).isFile()
}
/**
* If the path is not existed, created before write.
* @param filepath
* @param content
* @param options
*/
export async function writeFile(
filepath: string,
content: string | NodeJS.ArrayBufferView,
options?: fs.WriteFileOptions,
): Promise<void> {
const dirpath = path.dirname(filepath)
await fsp.mkdir(dirpath, { recursive: true })
await fsp.writeFile(filepath, content, options)
}
/**
* Remove filepaths
* @param filepaths
*/
export const unlinkSync = (...filepaths: (string | null | undefined | string[])[]): void => {
for (let filepath of filepaths) {
if (filepath == null) continue
if (!Array.isArray(filepath)) filepath = [filepath]
for (const p of filepath) if (fs.existsSync(p)) fs.unlinkSync(p)
}
}
export const assertPromiseThrow = async (
fn: () => Promise<unknown>,
errorPattern: string | RegExp,
): Promise<void> => {
await expect(() => fn()).rejects.toThrow(errorPattern)
}
export const assertPromiseNotThrow = async (fn: () => Promise<unknown>): Promise<void> => {
await expect(fn().then(() => undefined)).resolves.toBeUndefined()
}
export type IConsoleMethodField = 'debug' | 'log' | 'info' | 'warn' | 'error'
export interface IConsoleMock {
get(methodName: IConsoleMethodField): readonly (readonly unknown[])[]
getIndiscriminateAll(): readonly (readonly unknown[])[]
reset(): void
restore(): void
}
export function createConsoleMock(
methodNames: readonly IConsoleMethodField[] = ['debug', 'log', 'info', 'warn', 'error'],
desensitizeFn?: (args: unknown[]) => unknown[],
): IConsoleMock {
const mockFnMap: Record<string, MockInstance> = {}
const callsMap: Record<string, unknown[][]> = {}
const allCalls: unknown[][] = []
for (const field of methodNames) {
callsMap[field] = []
mockFnMap[field] = vi.spyOn(console, field).mockImplementation((...args: unknown[]) => {
const processedArgs = desensitizeFn ? desensitizeFn(args) : args
callsMap[field].push(processedArgs)
allCalls.push(processedArgs)
})
}
return {
get(methodName: IConsoleMethodField): readonly (readonly unknown[])[] {
return callsMap[methodName] ?? []
},
getIndiscriminateAll(): readonly (readonly unknown[])[] {
return allCalls
},
reset(): void {
for (const field of methodNames) {
callsMap[field] = []
}
allCalls.length = 0
},
restore(): void {
for (const mock of Object.values(mockFnMap)) {
mock.mockRestore()
}
},
}
}
export interface ICreateReporterMockOptions {
reporter: Reporter
desensitize?(args: readonly unknown[]): unknown[]
}
export interface IReporterMock {
getIndiscriminateAll(): readonly (readonly unknown[])[]
reset(): void
restore(): void
}
export function createReporterMock(options: ICreateReporterMockOptions): IReporterMock {
const { reporter, desensitize: desensitizeFn } = options
const allCalls: unknown[][] = []
// Enable mock mode on reporter to capture logs
reporter.mock()
return {
getIndiscriminateAll(): readonly (readonly unknown[])[] {
const entries = reporter.collect()
reporter.mock() // Re-enable mock mode after collect
for (const entry of entries) {
const args = desensitizeFn ? desensitizeFn(entry.args) : entry.args
allCalls.push(args)
}
return allCalls.slice()
},
reset(): void {
reporter.collect() // Clear by collecting
reporter.mock() // Re-enable mock mode
allCalls.length = 0
},
restore(): void {
reporter.collect() // Exit mock mode
},
}
}