@@ -4,9 +4,9 @@ import { basename, dirname, extname, isAbsolute, join, resolve } from 'pathe'
4
4
import { getColors , getType } from '@vitest/utils'
5
5
import { getWorkerState } from '../utils/global'
6
6
import { getAllMockableProperties } from '../utils/base'
7
- import { distDir } from '../constants '
7
+ import { spyOn } from '../integrations/spy '
8
8
import type { MockFactory , PendingSuiteMock } from '../types/mocker'
9
- import type { VitestRunner } from './execute'
9
+ import type { VitestExecutor } from './execute'
10
10
11
11
class RefTracker {
12
12
private idMap = new Map < any , number > ( )
@@ -38,28 +38,26 @@ function isSpecialProp(prop: Key, parentType: string) {
38
38
39
39
export class VitestMocker {
40
40
private static pendingIds : PendingSuiteMock [ ] = [ ]
41
- private static spyModulePath = resolve ( distDir , 'spy.js' )
42
- private static spyModule ?: typeof import ( '../integrations/spy' )
43
41
private resolveCache = new Map < string , Record < string , string > > ( )
44
42
45
43
constructor (
46
- public runner : VitestRunner ,
44
+ public executor : VitestExecutor ,
47
45
) { }
48
46
49
47
private get root ( ) {
50
- return this . runner . options . root
48
+ return this . executor . options . root
51
49
}
52
50
53
51
private get base ( ) {
54
- return this . runner . options . base
52
+ return this . executor . options . base
55
53
}
56
54
57
55
private get mockMap ( ) {
58
- return this . runner . options . mockMap
56
+ return this . executor . options . mockMap
59
57
}
60
58
61
59
private get moduleCache ( ) {
62
- return this . runner . moduleCache
60
+ return this . executor . moduleCache
63
61
}
64
62
65
63
public getSuiteFilepath ( ) : string {
@@ -78,7 +76,7 @@ export class VitestMocker {
78
76
}
79
77
80
78
private async resolvePath ( rawId : string , importer : string ) {
81
- const [ id , fsPath ] = await this . runner . resolveUrl ( rawId , importer )
79
+ const [ id , fsPath ] = await this . executor . resolveUrl ( rawId , importer )
82
80
// external is node_module or unresolved module
83
81
// for example, some people mock "vscode" and don't have it installed
84
82
const external = ! isAbsolute ( fsPath ) || fsPath . includes ( '/node_modules/' ) ? rawId : null
@@ -202,14 +200,6 @@ export class VitestMocker {
202
200
}
203
201
204
202
public mockObject ( object : Record < Key , any > , mockExports : Record < Key , any > = { } ) {
205
- if ( ! VitestMocker . spyModule ) {
206
- throw new Error (
207
- 'Error: Spy module is not defined. '
208
- + 'This is likely an internal bug in Vitest. '
209
- + 'Please report it to https://github.com/vitest-dev/vitest/issues' )
210
- }
211
- const spyModule = VitestMocker . spyModule
212
-
213
203
const finalizers = new Array < ( ) => void > ( )
214
204
const refs = new RefTracker ( )
215
205
@@ -271,7 +261,7 @@ export class VitestMocker {
271
261
continue
272
262
273
263
if ( isFunction ) {
274
- spyModule . spyOn ( newContainer , property ) . mockImplementation ( ( ) => undefined )
264
+ spyOn ( newContainer , property ) . mockImplementation ( ( ) => undefined )
275
265
// tinyspy retains length, but jest doesn't.
276
266
Object . defineProperty ( newContainer [ property ] , 'length' , { value : 0 } )
277
267
}
@@ -321,7 +311,7 @@ export class VitestMocker {
321
311
322
312
public async importActual < T > ( rawId : string , importee : string ) : Promise < T > {
323
313
const { id, fsPath } = await this . resolvePath ( rawId , importee )
324
- const result = await this . runner . cachedRequest ( id , fsPath , [ importee ] )
314
+ const result = await this . executor . cachedRequest ( id , fsPath , [ importee ] )
325
315
return result as T
326
316
}
327
317
@@ -335,19 +325,13 @@ export class VitestMocker {
335
325
mock = this . resolveMockPath ( fsPath , external )
336
326
337
327
if ( mock === null ) {
338
- const mod = await this . runner . cachedRequest ( id , fsPath , [ importee ] )
328
+ const mod = await this . executor . cachedRequest ( id , fsPath , [ importee ] )
339
329
return this . mockObject ( mod )
340
330
}
341
331
342
332
if ( typeof mock === 'function' )
343
333
return this . callFunctionMock ( fsPath , mock )
344
- return this . runner . dependencyRequest ( mock , mock , [ importee ] )
345
- }
346
-
347
- public async initializeSpyModule ( ) {
348
- if ( VitestMocker . spyModule )
349
- return
350
- VitestMocker . spyModule = await this . runner . executeId ( VitestMocker . spyModulePath )
334
+ return this . executor . dependencyRequest ( mock , mock , [ importee ] )
351
335
}
352
336
353
337
public async requestWithMock ( url : string , callstack : string [ ] ) {
@@ -367,7 +351,7 @@ export class VitestMocker {
367
351
const exports = { }
368
352
// Assign the empty exports object early to allow for cycles to work. The object will be filled by mockObject()
369
353
this . moduleCache . set ( mockPath , { exports } )
370
- const mod = await this . runner . directRequest ( url , url , callstack )
354
+ const mod = await this . executor . directRequest ( url , url , callstack )
371
355
this . mockObject ( mod , exports )
372
356
return exports
373
357
}
0 commit comments