Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mocker): set cache before mocking to allow circular dependencies #2391

Merged
merged 1 commit into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions packages/vitest/src/runtime/mocker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { existsSync, readdirSync } from 'fs'
import { isNodeBuiltin } from 'mlly'
import { basename, dirname, extname, join, resolve } from 'pathe'
import { normalizeRequestId, pathFromRoot, toFilePath } from 'vite-node/utils'
import { normalizeRequestId, pathFromRoot } from 'vite-node/utils'
import type { ModuleCacheMap } from 'vite-node/client'
import { getAllMockableProperties, getType, getWorkerState, mergeSlashes, slash } from '../utils'
import { distDir } from '../constants'
Expand Down Expand Up @@ -195,7 +195,7 @@ export class VitestMocker {
return existsSync(fullPath) ? fullPath : null
}

public mockObject(object: Record<string | symbol, any>) {
public mockObject(object: Record<Key, any>, mockExports: Record<Key, any> = {}) {
if (!VitestMocker.spyModule) {
throw new Error(
'Error: Spy module is not defined. '
Expand Down Expand Up @@ -275,7 +275,7 @@ export class VitestMocker {
}
}

const mockedObject: Record<Key, any> = {}
const mockedObject: Record<Key, any> = mockExports
mockPropertiesOf(object, mockedObject)

// Plug together refs
Expand Down Expand Up @@ -360,10 +360,12 @@ export class VitestMocker {
const cache = this.moduleCache.get(mockPath)
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.mockObject(mod)

const exports = {}
// Assign the empty exports object early to allow for cycles to work. The object will be filled by mockObject()
this.moduleCache.set(mockPath, { exports })
const mod = await this.request(dep)
this.mockObject(mod, exports)
Copy link
Contributor Author

@danez danez Nov 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted to provide the exports object to mockObject() to prevent having to copy the object once more here.

So this could also be, but needs to copy everything again.

Object.assign(exports, this.mockObject(mod))

return exports
}
if (typeof mock === 'function' && !callstack.includes(mockPath)) {
Expand Down
12 changes: 12 additions & 0 deletions test/core/test/mocked-circular.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { expect, it, vi } from 'vitest'
// The order of the two imports here matters: B before A
import { circularB } from '../src/circularB'
import { circularA } from '../src/circularA'

vi.mock('../src/circularB')

it('circular', () => {
circularA()

expect(circularB).toHaveBeenCalledOnce()
})