[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix: recursively autospy module object (#9687)

authored by

Hiroshi Ogawa and committed by
GitHub
(Feb 19, 2026, 11:00 AM +0100) 695a86b4 093c8f6b

+30 -1
+20 -1
packages/mocker/src/automocker.ts
··· 134 134 continue 135 135 } 136 136 137 + if (options.type === 'autospy' && type === 'Module') { 138 + // Replace with clean object to recursively autospy exported module objects: 139 + // export * as ns from "./ns" 140 + // or 141 + // import * as ns from "./ns" 142 + // export { ns } 143 + const exports = Object.create(null) 144 + Object.defineProperty(exports, Symbol.toStringTag, { 145 + value: 'Module', 146 + configurable: true, 147 + writable: true, 148 + }) 149 + try { 150 + newContainer[property] = exports 151 + } 152 + catch { 153 + continue 154 + } 155 + } 137 156 // Sometimes this assignment fails for some unknown reason. If it does, 138 157 // just move along. 139 - if (!define(newContainer, property, isFunction || options.type === 'autospy' ? value : {})) { 158 + else if (!define(newContainer, property, isFunction || options.type === 'autospy' ? value : {})) { 140 159 continue 141 160 } 142 161
+8
test/core/test/mocking/autospying.test.ts
··· 1 1 import axios from 'axios' 2 2 import { expect, test, vi } from 'vitest' 3 3 import { getAuthToken } from '../../src/env' 4 + import * as NamespaceModule from '../../src/mocks/autospying-namespace/index.js' 4 5 5 6 vi.mock(import('../../src/env'), { spy: true }) 6 7 7 8 vi.mock('axios', { spy: true }) 9 + vi.mock('../../src/mocks/autospying-namespace/index.js', { spy: true }) 8 10 9 11 test('getAuthToken is spied', async () => { 10 12 import.meta.env.AUTH_TOKEN = '123' ··· 22 24 // isAxiosError is not defined in __mocks__ 23 25 expect(axios.isAxiosError(new Error('test'))).toBe(false) 24 26 expect(axios.isAxiosError).toHaveBeenCalled() 27 + }) 28 + 29 + test('spies on namespace re-exports', async () => { 30 + expect(vi.isMockFunction(NamespaceModule.NamespaceTarget.computeSquare)).toBe(true) 31 + expect(NamespaceModule.NamespaceTarget.computeSquare(5)).toBe(25) 32 + expect(NamespaceModule.NamespaceTarget.computeSquare).toHaveBeenCalledTimes(1) 25 33 })
+1
test/core/src/mocks/autospying-namespace/index.ts
··· 1 + export * as NamespaceTarget from './namespaceTarget.js'
+1
test/core/src/mocks/autospying-namespace/namespaceTarget.ts
··· 1 + export const computeSquare = (n: number) => n * n