-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathmock.ts
313 lines (285 loc) · 9.89 KB
/
mock.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import type { Import } from 'unimport'
import { walk } from 'estree-walker'
import type { CallExpression, Expression, ExpressionStatement, Identifier, ImportDeclaration, ImportSpecifier, Literal, Node } from 'estree'
import type { AstNode, TransformPluginContext, TransformResult } from 'rollup'
import MagicString from 'magic-string'
import type { Component } from '@nuxt/schema'
import type { Plugin } from 'vite'
import { createUnplugin } from 'unplugin'
export interface MockPluginContext {
imports: Import[]
components: Component[]
}
const PLUGIN_NAME = 'nuxt:vitest:mock-transform'
const HELPER_MOCK_IMPORT = 'mockNuxtImport'
const HELPER_MOCK_COMPONENT = 'mockComponent'
const HELPER_MOCK_HOIST = '__NUXT_VITEST_MOCKS'
const HELPERS_NAME = [HELPER_MOCK_IMPORT, HELPER_MOCK_COMPONENT]
interface MockImportInfo {
name: string
import: Import
factory: string
}
interface MockComponentInfo {
path: string
factory: string
}
export const createMockPlugin = (ctx: MockPluginContext) => createUnplugin(() => {
function transform(this: TransformPluginContext, code: string, id: string): TransformResult | Promise<TransformResult> {
if (!HELPERS_NAME.some(n => code.includes(n))) return
if (id.includes('/node_modules/')) return
let ast: AstNode
try {
ast = this.parse(code, {
// @ts-expect-error compatibility with rollup v3
sourceType: 'module', ecmaVersion: 'latest', ranges: true,
})
}
catch {
return
}
let insertionPoint = 0
let hasViImport = false
const s = new MagicString(code)
const mocksImport: MockImportInfo[] = []
const mocksComponent: MockComponentInfo[] = []
const importPathsList: Set<string> = new Set()
// @ts-expect-error mismatch between acorn/estree types
walk(ast, {
enter: (node, parent) => {
// find existing vi import
if (isImportDeclaration(node)) {
if (node.source.value === 'vitest' && !hasViImport) {
const viImport = node.specifiers.find(
i =>
isImportSpecifier(i) && i.imported.type === 'Identifier' && i.imported.name === 'vi',
)
if (viImport) {
insertionPoint = endOf(node)
hasViImport = true
}
return
}
}
if (!isCallExpression(node)) return
// mockNuxtImport
if (
isIdentifier(node.callee)
&& node.callee.name === HELPER_MOCK_IMPORT
) {
if (node.arguments.length !== 2) {
return this.error(
new Error(
`${HELPER_MOCK_IMPORT}() should have exactly 2 arguments`,
),
startOf(node),
)
}
const importName = node.arguments[0]
if (!isLiteral(importName) || typeof importName.value !== 'string') {
return this.error(
new Error(
`The first argument of ${HELPER_MOCK_IMPORT}() must be a string literal`,
),
startOf(importName),
)
}
const name = importName.value
const importItem = ctx.imports.find(_ => name === (_.as || _.name))
if (!importItem) {
console.log({ imports: ctx.imports })
return this.error(`Cannot find import "${name}" to mock`)
}
s.overwrite(
isExpressionStatement(parent)
? startOf(parent)
: startOf(node.arguments[0]),
isExpressionStatement(parent)
? endOf(parent)
: endOf(node.arguments[1]),
'',
)
mocksImport.push({
name,
import: importItem,
factory: code.slice(
startOf(node.arguments[1]),
endOf(node.arguments[1]),
),
})
}
// mockComponent
if (
isIdentifier(node.callee)
&& node.callee.name === HELPER_MOCK_COMPONENT
) {
if (node.arguments.length !== 2) {
return this.error(
new Error(
`${HELPER_MOCK_COMPONENT}() should have exactly 2 arguments`,
),
startOf(node),
)
}
const componentName = node.arguments[0]
if (!isLiteral(componentName) || typeof componentName.value !== 'string') {
return this.error(
new Error(
`The first argument of ${HELPER_MOCK_COMPONENT}() must be a string literal`,
),
startOf(componentName),
)
}
const pathOrName = componentName.value
const component = ctx.components.find(
_ => _.pascalName === pathOrName || _.kebabName === pathOrName,
)
const path = component?.filePath || pathOrName
s.overwrite(
isExpressionStatement(parent)
? startOf(parent)
: startOf(node.arguments[1]),
isExpressionStatement(parent)
? endOf(parent)
: endOf(node.arguments[1]),
'',
)
mocksComponent.push({
path: path,
factory: code.slice(
startOf(node.arguments[1]),
endOf(node.arguments[1]),
),
})
}
},
})
if (mocksImport.length === 0 && mocksComponent.length === 0) return
const mockLines = []
if (mocksImport.length) {
const mockImportMap = new Map<string, MockImportInfo[]>()
for (const mock of mocksImport) {
if (!mockImportMap.has(mock.import.from)) {
mockImportMap.set(mock.import.from, [])
}
mockImportMap.get(mock.import.from)!.push(mock)
}
mockLines.push(
...Array.from(mockImportMap.entries()).flatMap(
([from, mocks]) => {
importPathsList.add(from)
const lines = [
`vi.mock(${JSON.stringify(from)}, async (importOriginal) => {`,
` const mocks = globalThis.${HELPER_MOCK_HOIST}`,
` if (!mocks[${JSON.stringify(from)}]) {`,
` mocks[${JSON.stringify(from)}] = { ...await importOriginal(${JSON.stringify(from)}) }`,
` }`,
]
for (const mock of mocks) {
if (mock.import.name === 'default') {
lines.push(
` mocks[${JSON.stringify(from)}]["default"] = await (${mock.factory})();`,
)
}
else {
lines.push(
` mocks[${JSON.stringify(from)}][${JSON.stringify(mock.name)}] = await (${mock.factory})();`,
)
}
}
lines.push(` return mocks[${JSON.stringify(from)}] `)
lines.push(`});`)
return lines
},
),
)
}
if (mocksComponent.length) {
mockLines.push(
...mocksComponent.flatMap((mock) => {
return [
`vi.mock(${JSON.stringify(mock.path)}, async () => {`,
` const factory = (${mock.factory});`,
` const result = typeof factory === 'function' ? await factory() : await factory`,
` return 'default' in result ? result : { default: result }`,
'});',
]
}),
)
}
if (!mockLines.length) return
s.prepend(`vi.hoisted(() => {
if(!globalThis.${HELPER_MOCK_HOIST}){
vi.stubGlobal(${JSON.stringify(HELPER_MOCK_HOIST)}, {})
}
});\n`)
if (!hasViImport) s.prepend(`import {vi} from "vitest";\n`)
s.appendLeft(insertionPoint, mockLines.join('\n') + '\n')
// do an import to trick vite to keep it
// if not, the module won't be mocked
importPathsList.forEach((p) => {
s.append(`\n import ${JSON.stringify(p)};`)
})
return {
code: s.toString(),
map: s.generateMap(),
}
}
return {
name: PLUGIN_NAME,
enforce: 'post',
vite: {
transform,
// Place Vitest's mock plugin after all Nuxt plugins
async configResolved(config) {
const plugins = (config.plugins || []) as Plugin[]
// `vite:mocks` was a typo in Vitest before v0.34.0
const vitestPlugins = plugins.filter(p => (p.name === 'vite:mocks' || p.name.startsWith('vitest:')) && (p.enforce || ('order' in p && p.order)) === 'post')
const lastNuxt = findLastIndex(
plugins,
i => i.name?.startsWith('nuxt:'),
)
if (lastNuxt === -1) return
for (const plugin of vitestPlugins) {
const index = plugins.indexOf(plugin)
if (index < lastNuxt) {
plugins.splice(index, 1)
plugins.splice(lastNuxt, 0, plugin)
}
}
},
},
}
})
// Polyfill Array.prototype.findLastIndex for legacy Node.js
function findLastIndex<T>(arr: T[], predicate: (item: T) => boolean) {
for (let i = arr.length - 1; i >= 0; i--) {
if (predicate(arr[i])) return i
}
return -1
}
function isImportDeclaration(node: Node): node is ImportDeclaration {
return node.type === 'ImportDeclaration'
}
function isImportSpecifier(node: Node): node is ImportSpecifier {
return node.type === 'ImportSpecifier'
}
function isCallExpression(node: Node): node is CallExpression {
return node.type === 'CallExpression'
}
function isIdentifier(node: Node): node is Identifier {
return node.type === 'Identifier'
}
function isLiteral(node: Node | Expression): node is Literal {
return node.type === 'Literal'
}
function isExpressionStatement(node: Node | null): node is ExpressionStatement {
return node?.type === 'ExpressionStatement'
}
// TODO: need to fix in rollup types, probably
function startOf(node: Node) {
return 'range' in node && node.range ? node.range[0] : ('start' in node ? node.start as number : undefined as never)
}
function endOf(node: Node) {
return 'range' in node && node.range ? node.range[1] : ('end' in node ? node.end as number : undefined as never)
}