-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
mocker.ts
281 lines (219 loc) · 8.06 KB
/
mocker.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
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
import { existsSync, readdirSync } from 'fs'
import { isNodeBuiltin } from 'mlly'
import { basename, dirname, resolve } from 'pathe'
import type { ModuleCache } from 'vite-node'
import { toFilePath } from 'vite-node/utils'
import { isWindows, mergeSlashes, normalizeId } from '../utils'
import { distDir } from '../constants'
import type { ExecuteOptions } from './execute'
export type SuiteMocks = Record<string, Record<string, string | null | (() => unknown)>>
type Callback = (...args: any[]) => unknown
interface PendingSuiteMock {
id: string
importer: string
type: 'mock' | 'unmock'
factory?: () => unknown
}
function getObjectType(value: unknown): string {
return Object.prototype.toString.apply(value).slice(8, -1)
}
function mockPrototype(spyOn: typeof import('../integrations/jest-mock')['spyOn'], proto: any) {
if (!proto) return null
const newProto: any = {}
const protoDescr = Object.getOwnPropertyDescriptors(proto)
// eslint-disable-next-line no-restricted-syntax
for (const d in protoDescr) {
Object.defineProperty(newProto, d, protoDescr[d])
if (typeof protoDescr[d].value === 'function')
spyOn(newProto, d).mockImplementation(() => {})
}
return newProto
}
const pendingIds: PendingSuiteMock[] = []
export class VitestMocker {
private request!: (dep: string) => unknown
private root: string
private callbacks: Record<string, ((...args: any[]) => unknown)[]> = {}
private spy?: typeof import('../integrations/jest-mock')
constructor(
public options: ExecuteOptions,
private moduleCache: Map<string, ModuleCache>,
request?: (dep: string) => unknown,
) {
this.root = this.options.root
this.request = request!
}
get mockMap() {
return this.options.mockMap
}
public on(event: string, cb: Callback) {
this.callbacks[event] ??= []
this.callbacks[event].push(cb)
}
private emit(event: string, ...args: any[]) {
(this.callbacks[event] ?? []).forEach(fn => fn(...args))
}
public getSuiteFilepath(): string {
return __vitest_worker__?.filepath || 'global'
}
public getMocks() {
const suite = this.getSuiteFilepath()
const suiteMocks = this.mockMap[suite]
const globalMocks = this.mockMap.global
return {
...globalMocks,
...suiteMocks,
}
}
private async resolvePath(id: string, importer: string) {
const path = await this.options.resolveId(id, importer)
return {
path: normalizeId(path?.id || id),
external: path?.id.includes('/node_modules/') ? id : null,
}
}
private async resolveMocks() {
await Promise.all(pendingIds.map(async(mock) => {
const { path, external } = await this.resolvePath(mock.id, mock.importer)
if (mock.type === 'unmock')
this.unmockPath(path)
if (mock.type === 'mock')
this.mockPath(path, external, mock.factory)
}))
pendingIds.length = 0
}
private async callFunctionMock(dep: string, mock: () => any) {
const cacheName = `${dep}__mock`
const cached = this.moduleCache.get(cacheName)?.exports
if (cached)
return cached
const exports = await mock()
this.emit('mocked', cacheName, { exports })
return exports
}
public getDependencyMock(dep: string) {
return this.getMocks()[this.resolveDependency(dep)]
}
public resolveDependency(dep: string) {
return normalizeId(dep).replace(/^\/@fs\//, isWindows ? '' : '/')
}
public normalizePath(path: string) {
return normalizeId(path.replace(this.root, '')).replace(/^\/@fs\//, isWindows ? '' : '/')
}
public getFsPath(path: string, external: string | null) {
if (external)
return mergeSlashes(`/@fs/${path}`)
return normalizeId(path.replace(this.root, ''))
}
public resolveMockPath(mockPath: string, external: string | null) {
const path = normalizeId(external || mockPath)
// it's a node_module alias
// all mocks should be inside <root>/__mocks__
if (external || isNodeBuiltin(mockPath)) {
const mockDirname = dirname(path) // for nested mocks: @vueuse/integration/useJwt
const baseFilename = basename(path)
const mockFolder = resolve(this.root, '__mocks__', mockDirname)
if (!existsSync(mockFolder)) return null
const files = readdirSync(mockFolder)
for (const file of files) {
const [basename] = file.split('.')
if (basename === baseFilename)
return resolve(mockFolder, file).replace(this.root, '')
}
return null
}
const dir = dirname(path)
const baseId = basename(path)
const fullPath = resolve(dir, '__mocks__', baseId)
return existsSync(fullPath) ? fullPath.replace(this.root, '') : null
}
public mockObject(obj: any) {
if (!this.spy)
throw new Error('Internal Vitest error: Spy function is not defined.')
const type = getObjectType(obj)
if (Array.isArray(obj))
return []
else if (type !== 'Object' && type !== 'Module')
return obj
const newObj = { ...obj }
const proto = mockPrototype(this.spy.spyOn, Object.getPrototypeOf(obj))
Object.setPrototypeOf(newObj, proto)
// eslint-disable-next-line no-restricted-syntax
for (const k in obj) {
newObj[k] = this.mockObject(obj[k])
const type = getObjectType(obj[k])
if (type.includes('Function') && !obj[k]._isMockFunction) {
this.spy.spyOn(newObj, k).mockImplementation(() => {})
Object.defineProperty(newObj[k], 'length', { value: 0 }) // tinyspy retains length, but jest doesnt
}
}
return newObj
}
public unmockPath(path: string) {
const suitefile = this.getSuiteFilepath()
const fsPath = this.normalizePath(path)
if (this.mockMap[suitefile]?.[fsPath])
delete this.mockMap[suitefile][fsPath]
}
public mockPath(path: string, external: string | null, factory?: () => any) {
const suitefile = this.getSuiteFilepath()
const fsPath = this.normalizePath(path)
this.mockMap[suitefile] ??= {}
this.mockMap[suitefile][fsPath] = factory || this.resolveMockPath(path, external)
}
public async importActual<T>(id: string, importer: string): Promise<T> {
const { path, external } = await this.resolvePath(id, importer)
const fsPath = this.getFsPath(path, external)
const result = await this.request(fsPath)
return result as T
}
public async importMock(id: string, importer: string): Promise<any> {
const { path, external } = await this.resolvePath(id, importer)
let mock = this.getDependencyMock(path)
if (mock === undefined)
mock = this.resolveMockPath(path, external)
if (mock === null) {
await this.ensureSpy()
const fsPath = this.getFsPath(path, external)
const mod = await this.request(fsPath)
return this.mockObject(mod)
}
if (typeof mock === 'function')
return this.callFunctionMock(path, mock)
return this.requestWithMock(mock)
}
private async ensureSpy() {
if (this.spy) return
this.spy = await this.request(resolve(distDir, 'jest-mock.js')) as typeof import('../integrations/jest-mock')
}
public async requestWithMock(dep: string) {
await this.ensureSpy()
await this.resolveMocks()
const mock = this.getDependencyMock(dep)
if (mock === null) {
const cacheName = `${dep}__mock`
const cache = this.moduleCache.get(cacheName)
if (cache?.exports)
return cache.exports
const cacheKey = toFilePath(dep, this.root)
const mod = this.moduleCache.get(cacheKey)?.exports || await this.request(dep)
const exports = this.mockObject(mod)
this.emit('mocked', cacheName, { exports })
return exports
}
if (typeof mock === 'function')
return this.callFunctionMock(dep, mock)
if (typeof mock === 'string')
dep = mock
return this.request(dep)
}
public queueMock(id: string, importer: string, factory?: () => unknown) {
pendingIds.push({ type: 'mock', id, importer, factory })
}
public queueUnmock(id: string, importer: string) {
pendingIds.push({ type: 'unmock', id, importer })
}
public withRequest(request: (dep: string) => unknown) {
return new VitestMocker(this.options, this.moduleCache, request)
}
}