Skip to content

Commit 9010a89

Browse files
authored
feat: add strong typings to function spies on SpyObject (#707)
1 parent 05c09ed commit 9010a89

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

projects/spectator/src/lib/mock.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import { FactoryProvider, Type, AbstractType } from '@angular/core';
33

44
type Writable<T> = { -readonly [P in keyof T]: T[P] };
55

6+
export declare type UnknownFunction = (...args: Array<unknown>) => unknown & Function;
7+
68
/**
79
* @publicApi
810
*/
9-
export interface CompatibleSpy extends jasmine.Spy {
11+
export interface CompatibleSpy<F extends UnknownFunction = UnknownFunction> extends jasmine.Spy<(...args: Parameters<F>) => ReturnType<F>> {
1012
/**
1113
* By chaining the spy with and.returnValue, all calls to the function will return a specific
1214
* value.
@@ -17,7 +19,7 @@ export interface CompatibleSpy extends jasmine.Spy {
1719
* By chaining the spy with and.callFake, all calls to the spy will delegate to the supplied
1820
* function.
1921
*/
20-
andCallFake(fn: Function): this;
22+
andCallFake(fn: UnknownFunction): this;
2123

2224
/**
2325
* removes all recorded calls
@@ -28,7 +30,7 @@ export interface CompatibleSpy extends jasmine.Spy {
2830
/**
2931
* @publicApi
3032
*/
31-
export type SpyObject<T> = T & { [P in keyof T]: T[P] extends Function ? T[P] & CompatibleSpy : T[P] } & {
33+
export type SpyObject<T> = T & { [P in keyof T]: T[P] extends UnknownFunction ? T[P] & CompatibleSpy<T[P]> : T[P] } & {
3234
/**
3335
* Casts to type without readonly properties
3436
*/

0 commit comments

Comments
 (0)