| @@ -248,6 +248,7 @@ export function hoistMocks( |
|
|
| 248 |
248 |
|
| 249 |
249 |
const usedUtilityExports = new Set<string>() |
| 250 |
250 |
let hasImportMetaVitest = false |
|
251 |
+let hasMockApiCall = false |
| 251 |
252 |
|
| 252 |
253 |
esmWalker(ast, { |
| 253 |
254 |
onImportMeta(node) { |
| @@ -310,6 +311,7 @@ export function hoistMocks( |
|
|
| 310 |
311 |
usedUtilityExports.add(node.callee.object.name) |
| 311 |
312 |
|
| 312 |
313 |
if (hoistableMockMethodNames.includes(methodName)) { |
|
314 |
+hasMockApiCall = true |
| 313 |
315 |
const method = `${node.callee.object.name}.${methodName}` |
| 314 |
316 |
assertNotDefaultExport( |
| 315 |
317 |
node, |
| @@ -356,6 +358,7 @@ export function hoistMocks( |
|
|
| 356 |
358 |
// vi.doMock(import('./path')) -> vi.doMock('./path') |
| 357 |
359 |
// vi.doMock(await import('./path')) -> vi.doMock('./path') |
| 358 |
360 |
else if (dynamicImportMockMethodNames.includes(methodName)) { |
|
361 |
+hasMockApiCall = true |
| 359 |
362 |
const moduleInfo = node.arguments[0] as Positioned<Expression> |
| 360 |
363 |
let source: Positioned<Expression> | null = null |
| 361 |
364 |
if (moduleInfo.type === 'ImportExpression') { |
| @@ -377,6 +380,7 @@ export function hoistMocks( |
|
|
| 377 |
380 |
} |
| 378 |
381 |
|
| 379 |
382 |
if (hoistedMethodNames.includes(methodName)) { |
|
383 |
+hasMockApiCall = true |
| 380 |
384 |
assertNotDefaultExport( |
| 381 |
385 |
node, |
| 382 |
386 |
'Cannot export hoisted variable. You can control hoisting behavior by placing the import from this file first.', |
| @@ -406,6 +410,10 @@ export function hoistMocks( |
|
|
| 406 |
410 |
}, |
| 407 |
411 |
}) |
| 408 |
412 |
|
|
413 |
+if (!hasMockApiCall) { |
|
414 |
+return |
|
415 |
+} |
|
416 |
+ |
| 409 |
417 |
function getNodeName(node: CallExpression) { |
| 410 |
418 |
const callee = node.callee | |
| 411 |
419 |
if ( |