33 populateSpy ,
44 spies ,
55 SpyImpl ,
6+ SpyInternal ,
67 SpyInternalImpl ,
78} from './internal'
89import { assert , define , defineValue , isType } from './utils'
@@ -47,7 +48,7 @@ export function internalSpyOn<T, K extends string & keyof T>(
4748
4849 let [ accessName , accessType ] = ( ( ) : [
4950 string | symbol | number ,
50- 'value' | 'get' | 'set'
51+ 'value' | 'get' | 'set' ,
5152 ] => {
5253 if ( ! isType ( 'object' , methodName ) ) {
5354 return [ methodName , 'value' ]
@@ -97,12 +98,6 @@ export function internalSpyOn<T, K extends string & keyof T>(
9798 origin = obj [ accessName as keyof T ] as unknown as Procedure
9899 }
99100
100- if ( ! mock ) mock = origin
101-
102- let fn = createInternalSpy ( mock )
103- if ( accessType === 'value' ) {
104- prototype ( fn , origin )
105- }
106101 let reassign = ( cb : any ) => {
107102 let { value, ...desc } = originalDescriptor || {
108103 configurable : true ,
@@ -118,13 +113,26 @@ export function internalSpyOn<T, K extends string & keyof T>(
118113 originalDescriptor
119114 ? define ( obj , accessName , originalDescriptor )
120115 : reassign ( origin )
121- const state = fn [ S ]
122- defineValue ( state , 'restore' , restore )
123- defineValue ( state , 'getOriginal' , ( ) => ( ssr ? origin ( ) : origin ) )
124- defineValue ( state , 'willCall' , ( newCb : Procedure ) => {
125- state . impl = newCb
126- return fn
127- } )
116+
117+ if ( ! mock ) mock = origin
118+
119+ let fn : SpyInternal
120+ if ( origin && S in origin ) {
121+ fn = origin as SpyInternal
122+ } else {
123+ fn = createInternalSpy ( mock )
124+ if ( accessType === 'value' ) {
125+ prototype ( fn , origin )
126+ }
127+
128+ const state = fn [ S ]
129+ defineValue ( state , 'restore' , restore )
130+ defineValue ( state , 'getOriginal' , ( ) => ( ssr ? origin ( ) : origin ) )
131+ defineValue ( state , 'willCall' , ( newCb : Procedure ) => {
132+ state . impl = newCb
133+ return fn
134+ } )
135+ }
128136
129137 reassign (
130138 ssr
0 commit comments