/
mocker.ts
278 lines (221 loc) · 8.41 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
import { existsSync, readdirSync } from 'fs'
import { isNodeBuiltin } from 'mlly'
import { basename, dirname, resolve } from 'pathe'
import { normalizeRequestId, toFilePath } from 'vite-node/utils'
import type { ModuleCacheMap } from 'vite-node/client'
import { getAllProperties, getType, getWorkerState, isWindows, mergeSlashes, slash } from '../utils'
import { distDir } from '../constants'
import type { PendingSuiteMock } from '../types/mocker'
import type { ExecuteOptions } from './execute'
type Callback = (...args: any[]) => unknown
interface ViteRunnerRequest {
(dep: string): any
callstack: string[]
}
export class VitestMocker {
private static pendingIds: PendingSuiteMock[] = []
private static spyModule?: typeof import('../integrations/spy')
private request!: ViteRunnerRequest
private root: string
private callbacks: Record<string, ((...args: any[]) => unknown)[]> = {}
constructor(
public options: ExecuteOptions,
private moduleCache: ModuleCacheMap,
request?: ViteRunnerRequest,
) {
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 getWorkerState().filepath || 'global'
}
public getMocks() {
const suite = this.getSuiteFilepath()
const suiteMocks = this.mockMap.get(suite)
const globalMocks = this.mockMap.get('global')
return {
...globalMocks,
...suiteMocks,
}
}
private async resolvePath(id: string, importer: string) {
const path = await this.options.resolveId!(id, importer)
// external is node_module or unresolved module
// for example, some people mock "vscode" and don't have it installed
const external = path == null || path.id.includes('/node_modules/') ? id : null
return {
path: normalizeRequestId(path?.id || id),
external,
}
}
private async resolveMocks() {
await Promise.all(VitestMocker.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)
}))
VitestMocker.pendingIds = []
}
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 normalizeRequestId(dep.replace(this.root, '')).replace(/^\/@fs\//, isWindows ? '' : '/')
}
public normalizePath(path: string) {
return normalizeRequestId(path.replace(this.root, '')).replace(/^\/@fs\//, isWindows ? '' : '/')
}
public getFsPath(path: string, external: string | null) {
if (external)
return mergeSlashes(`/@fs/${path}`)
return normalizeRequestId(path.replace(this.root, ''))
}
public resolveMockPath(mockPath: string, external: string | null) {
const path = normalizeRequestId(external || mockPath)
// it's a node_module alias
// all mocks should be inside <root>/__mocks__
if (external || isNodeBuiltin(mockPath) || !existsSync(mockPath)) {
const mockDirname = dirname(path) // for nested mocks: @vueuse/integration/useJwt
const mockFolder = resolve(this.root, '__mocks__', mockDirname)
if (!existsSync(mockFolder))
return null
const files = readdirSync(mockFolder)
const baseFilename = basename(path)
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 mockValue(value: any) {
if (!VitestMocker.spyModule) {
throw new Error(
'Error: Spy module is not defined. '
+ 'This is likely an internal bug in Vitest. '
+ 'Please report it to https://github.com/vitest-dev/vitest/issues')
}
const type = getType(value)
if (Array.isArray(value))
return []
else if (type !== 'Object' && type !== 'Module')
return value
const newObj: Record<string | symbol, any> = {}
const properties = getAllProperties(value)
for (const k of properties) {
newObj[k] = this.mockValue(value[k])
const type = getType(value[k])
if (type.includes('Function') && !value[k]._isMockFunction) {
VitestMocker.spyModule.spyOn(newObj, k).mockImplementation(() => undefined)
Object.defineProperty(newObj[k], 'length', { value: 0 }) // tinyspy retains length, but jest doesnt
}
}
// should be defined after object, because it may contain
// special logic on getting/settings properties
// and we don't want to invoke it
Object.setPrototypeOf(newObj, Object.getPrototypeOf(value))
return newObj
}
public unmockPath(path: string) {
const suitefile = this.getSuiteFilepath()
const fsPath = this.normalizePath(path)
const mock = this.mockMap.get(suitefile)
if (mock?.[fsPath])
delete mock[fsPath]
}
public mockPath(path: string, external: string | null, factory?: () => any) {
const suitefile = this.getSuiteFilepath()
const fsPath = this.normalizePath(path)
if (!this.mockMap.has(suitefile))
this.mockMap.set(suitefile, {})
this.mockMap.get(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.mockValue(mod)
}
if (typeof mock === 'function')
return this.callFunctionMock(path, mock)
return this.requestWithMock(mock)
}
private async ensureSpy() {
if (VitestMocker.spyModule)
return
VitestMocker.spyModule = await this.request(`/@fs/${slash(resolve(distDir, 'spy.mjs'))}`) as typeof import('../integrations/spy')
}
public async requestWithMock(dep: string) {
await this.ensureSpy()
await this.resolveMocks()
const mock = this.getDependencyMock(dep)
const callstack = this.request.callstack
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.mockValue(mod)
this.emit('mocked', cacheName, { exports })
return exports
}
if (typeof mock === 'function' && !callstack.includes(`mock:${dep}`)) {
callstack.push(`mock:${dep}`)
const result = await this.callFunctionMock(dep, mock)
const indexMock = callstack.indexOf(`mock:${dep}`)
callstack.splice(indexMock, 1)
return result
}
if (typeof mock === 'string' && !callstack.includes(mock))
dep = mock
return this.request(dep)
}
public queueMock(id: string, importer: string, factory?: () => unknown) {
VitestMocker.pendingIds.push({ type: 'mock', id, importer, factory })
}
public queueUnmock(id: string, importer: string) {
VitestMocker.pendingIds.push({ type: 'unmock', id, importer })
}
public withRequest(request: ViteRunnerRequest) {
return new VitestMocker(this.options, this.moduleCache, request)
}
}