1111// eslint-disable-next-line @typescript-eslint/no-explicit-any
1212export type StubCallback < T > = ( instance : T , ...args : any ) => void ;
1313
14+ /** The type representation of a generic function that can be stubbed. */
15+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
16+ export type GenericFunction = ( ...args : any ) => any ;
17+
1418class Registration < T > {
15- // eslint-disable-next-line @typescript-eslint/no-explicit-any
16- private oldMethod : ( ( ...args : any ) => any ) | null = null ;
19+ private oldMethod : GenericFunction | null = null ;
1720
1821 constructor (
1922 readonly callback : StubCallback < T > ,
20- readonly methodNameToOverride : string ,
23+ readonly methodToOverride : GenericFunction ,
2124 readonly classPrototype : T ,
2225 readonly ensureOneCall : boolean ,
2326 ) { }
2427
2528 stubPrototype ( ) : void {
26- // TODO: Figure out how to make this work with minification.
2729 if ( this . oldMethod ) {
2830 throw new Error (
29- `Function is already stubbed: ${ this . methodNameToOverride } .` ,
31+ `Function is already stubbed: ${ this . methodToOverride . name } .` ,
3032 ) ;
3133 }
3234 // eslint-disable-next-line @typescript-eslint/no-explicit-any
3335 const genericPrototype = this . classPrototype as any ;
34- const oldMethod = genericPrototype [ this . methodNameToOverride ] as (
35- // eslint-disable-next-line @typescript-eslint/no-explicit-any
36- ...args : any
37- // eslint-disable-next-line @typescript-eslint/no-explicit-any
38- ) => any ;
39- this . oldMethod = oldMethod ;
36+ this . oldMethod = this . methodToOverride ;
4037 // eslint-disable-next-line @typescript-eslint/no-this-alias
4138 const registration = this ;
39+ const methodNameToOverride = this . methodToOverride . name ;
4240 // eslint-disable-next-line @typescript-eslint/no-explicit-any
43- genericPrototype [ this . methodNameToOverride ] = function ( ...args : any ) : any {
41+ genericPrototype [ methodNameToOverride ] = function ( ...args : any ) : any {
4442 let stubsCalled = this . _internalStubsCalled as
4543 | { [ key : string ] : boolean }
4644 | undefined ;
@@ -49,13 +47,13 @@ class Registration<T> {
4947 this . _internalStubsCalled = stubsCalled ;
5048 }
5149
52- const result = oldMethod . call ( this , ...args ) ;
50+ const result = registration . methodToOverride . call ( this , ...args ) ;
5351 if (
5452 ! registration . ensureOneCall ||
55- ! stubsCalled [ registration . methodNameToOverride ]
53+ ! stubsCalled [ registration . methodToOverride . name ]
5654 ) {
5755 registration . callback ( this as unknown as T , ...args ) ;
58- stubsCalled [ registration . methodNameToOverride ] = true ;
56+ stubsCalled [ registration . methodToOverride . name ] = true ;
5957 }
6058 return result ;
6159 } ;
@@ -96,12 +94,12 @@ export class FunctionStubber {
9694 *
9795 * @param callback The function to run when the stubbed method executes for
9896 * the first time.
99- * @param methodNameToOverride The name of the method to override.
97+ * @param methodToOverride The method within the prototype to override.
10098 * @param classPrototype The prototype of the class being stubbed.
10199 */
102100 registerInitializationStub < T > (
103101 callback : StubCallback < T > ,
104- methodNameToOverride : string ,
102+ methodToOverride : GenericFunction ,
105103 classPrototype : T ,
106104 ) {
107105 if ( this . isFinalized ) {
@@ -111,7 +109,7 @@ export class FunctionStubber {
111109 }
112110 const registration = new Registration (
113111 callback ,
114- methodNameToOverride ,
112+ methodToOverride ,
115113 classPrototype ,
116114 true ,
117115 ) ;
@@ -127,12 +125,12 @@ export class FunctionStubber {
127125 * This will throw an error if called after stubPrototypes() has been called.
128126 *
129127 * @param callback The function to run when the stubbed method executes.
130- * @param methodNameToOverride The name of the method to override.
128+ * @param methodToOverride The method within the prototype to override.
131129 * @param classPrototype The prototype of the class being stubbed.
132130 */
133131 registerMethodStub < T > (
134132 callback : StubCallback < T > ,
135- methodNameToOverride : string ,
133+ methodToOverride : GenericFunction ,
136134 classPrototype : T ,
137135 ) {
138136 if ( this . isFinalized ) {
@@ -142,7 +140,7 @@ export class FunctionStubber {
142140 }
143141 const registration = new Registration (
144142 callback ,
145- methodNameToOverride ,
143+ methodToOverride ,
146144 classPrototype ,
147145 false ,
148146 ) ;
0 commit comments