|
1 | 1 | import { existsSync, readdirSync } from 'fs'
|
2 | 2 | import { isNodeBuiltin } from 'mlly'
|
3 | 3 | import { basename, dirname, extname, join, resolve } from 'pathe'
|
4 |
| -import { normalizeRequestId, pathFromRoot, toFilePath } from 'vite-node/utils' |
| 4 | +import { normalizeRequestId, pathFromRoot } from 'vite-node/utils' |
5 | 5 | import type { ModuleCacheMap } from 'vite-node/client'
|
6 | 6 | import { getAllMockableProperties, getType, getWorkerState, mergeSlashes, slash } from '../utils'
|
7 | 7 | import { distDir } from '../constants'
|
@@ -195,7 +195,7 @@ export class VitestMocker {
|
195 | 195 | return existsSync(fullPath) ? fullPath : null
|
196 | 196 | }
|
197 | 197 |
|
198 |
| - public mockObject(object: Record<string | symbol, any>) { |
| 198 | + public mockObject(object: Record<Key, any>, mockExports: Record<Key, any> = {}) { |
199 | 199 | if (!VitestMocker.spyModule) {
|
200 | 200 | throw new Error(
|
201 | 201 | 'Error: Spy module is not defined. '
|
@@ -275,7 +275,7 @@ export class VitestMocker {
|
275 | 275 | }
|
276 | 276 | }
|
277 | 277 |
|
278 |
| - const mockedObject: Record<Key, any> = {} |
| 278 | + const mockedObject: Record<Key, any> = mockExports |
279 | 279 | mockPropertiesOf(object, mockedObject)
|
280 | 280 |
|
281 | 281 | // Plug together refs
|
@@ -360,10 +360,12 @@ export class VitestMocker {
|
360 | 360 | const cache = this.moduleCache.get(mockPath)
|
361 | 361 | if (cache?.exports)
|
362 | 362 | return cache.exports
|
363 |
| - const cacheKey = toFilePath(dep, this.root) |
364 |
| - const mod = this.moduleCache.get(cacheKey)?.exports || await this.request(dep) |
365 |
| - const exports = this.mockObject(mod) |
| 363 | + |
| 364 | + const exports = {} |
| 365 | + // Assign the empty exports object early to allow for cycles to work. The object will be filled by mockObject() |
366 | 366 | this.moduleCache.set(mockPath, { exports })
|
| 367 | + const mod = await this.request(dep) |
| 368 | + this.mockObject(mod, exports) |
367 | 369 | return exports
|
368 | 370 | }
|
369 | 371 | if (typeof mock === 'function' && !callstack.includes(mockPath)) {
|
|
0 commit comments