@@ -6,7 +6,110 @@ describe('SpyObject', () => {
66 it ( 'should mock all public methods' , ( ) => {
77 const person = createSpyObject ( Person ) ;
88
9+ // Check methods without arguments but returns
910 person . sayHi . andReturn ( 'Bye!' ) ;
11+
12+ // Check method with different return types
13+ // These shouldn not compile, but will with `@ts-expect-error` applied
14+ // @ts -expect-error
15+ person . sayHi . andReturn ( false ) ;
16+ // @ts -expect-error
17+ person . sayHi . andReturn ( 100 ) ;
18+ // @ts -expect-error
19+ person . sayHi . andReturn ( { prop : true } ) ;
20+ // @ts -expect-error
21+ person . sayHi . andCallFake ( ( ) : boolean => {
22+ return false ;
23+ } ) ;
24+ // @ts -expect-error
25+ person . sayHi . andCallFake ( ( ) : number => {
26+ return 100 ;
27+ } ) ;
28+ // @ts -expect-error
29+ person . sayHi . andCallFake ( ( ) : { prop : boolean } => {
30+ return { prop : true } ;
31+ } ) ;
32+
33+ // Check methods with arguments
34+ person . saySomething . andReturn ( '' ) ;
35+ person . saySomething . withArgs ( 'Testing' ) . and . returnValue ( 'You said: Testing' ) ;
36+ person . saySomething . andCallFake ( ( something : string ) => {
37+ return `You said: ${ something } ` ;
38+ } ) ;
39+
40+ // Check method with different return types
41+ // These shouldn not compile, but will with `@ts-expect-error` applied
42+ // @ts -expect-error
43+ person . saySomething . andReturn ( false ) ;
44+ // @ts -expect-error
45+ person . saySomething . andReturn ( 100 ) ;
46+ // @ts -expect-error
47+ person . saySomething . andReturn ( { prop : true } ) ;
48+ // @ts -expect-error
49+ person . saySomething . andCallFake ( ( str : string ) : boolean => {
50+ return false ;
51+ } ) ;
52+ // @ts -expect-error
53+ person . saySomething . andCallFake ( ( str : string ) : number => {
54+ return 100 ;
55+ } ) ;
56+ // @ts -expect-error
57+ person . saySomething . andCallFake ( ( str : string ) : { prop : boolean } => {
58+ return { prop : true } ;
59+ } ) ;
60+ // @ts -expect-error
61+ person . saySomething . andCallFake ( ( num : number ) : string => {
62+ return 'Wrong Argument Type' ;
63+ } ) ;
64+ // @ts -expect-error
65+ person . saySomething . andCallFake ( ( bool : boolean ) : string => {
66+ return 'Wrong Argument Type' ;
67+ } ) ;
68+
69+ // Check pure void methods
70+ person . voidMethod . andCallFake ( ( ) => { } ) ;
71+
72+ // Check method with different return types
73+ // These shouldn not compile, but will with `@ts-expect-error` applied
74+ // @ts -expect-error
75+ person . voidMethod . andReturn ( false ) ;
76+ // @ts -expect-error
77+ person . voidMethod . andReturn ( 100 ) ;
78+ // @ts -expect-error
79+ person . voidMethod . andReturn ( { prop : true } ) ;
80+
81+ // These compile because a void method's return type isn't checked by the compiler
82+ person . voidMethod . andCallFake ( ( ) : boolean => {
83+ return false ;
84+ } ) ;
85+ person . voidMethod . andCallFake ( ( ) : number => {
86+ return 100 ;
87+ } ) ;
88+ person . voidMethod . andCallFake ( ( ) : { prop : boolean } => {
89+ return { prop : true } ;
90+ } ) ;
91+
92+ // Check void methods with arguments
93+ person . voidMethodWithArguments . andCallFake ( ( arg1 : string , arg2 : number ) => {
94+ // Do nothing
95+ } ) ;
96+
97+ // Check methods with different argument and return types
98+ // These shouldn not compile, but will with `@ts-expect-error` applied
99+ // @ts -expect-error
100+ person . voidMethod . andReturn ( false ) ;
101+ // @ts -expect-error
102+ person . voidMethod . andReturn ( 100 ) ;
103+ // @ts -expect-error
104+ person . voidMethod . andReturn ( { prop : true } ) ;
105+ // @ts -expect-error
106+ person . voidMethodWithArguments . andCallFake ( ( arg1 : string , arg2 : string ) => { } ) ;
107+ // @ts -expect-error
108+ person . voidMethodWithArguments . andCallFake ( ( arg1 : number , arg2 : string ) => { } ) ;
109+
110+ // These compile because an empty argument list is valid in Typescript.
111+ // https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-functions-with-fewer-parameters-assignable-to-functions-that-take-more-parameters
112+ person . voidMethodWithArguments . andCallFake ( ( ) => { } ) ;
10113 } ) ;
11114
12115 it ( 'should enable spying on properties' , ( ) => {
0 commit comments