1- import { IsMockProp , Obj , Spyable , SpyOnProp , ValueOf } from "./types" ;
2-
3- export const messages = {
4- error : {
5- invalidSpy : ( o : object ) : string => {
6- const helpfulValue = `${ o ? typeof o : "" } '${ o } '` ;
7- return `Cannot spyOn on a primitive value; ${ helpfulValue } given.` ;
8- } ,
9- noMethodSpy : ( p : string ) : string =>
10- `Cannot spy on the property '${ p } ' because it is a function. Please use \`jest.spyOn\`.` ,
11- noUnconfigurableSpy : ( p : string ) : string =>
12- `Cannot spy on the property '${ p } ' because it is not configurable` ,
13- } ,
14- warn : {
15- noUndefinedSpy : ( p : string ) : string =>
16- `Spying on an undefined property '${ p } '.` ,
17- } ,
18- } ;
19-
20- export const log = ( ...args : unknown [ ] ) : void => log . default ( ...args ) ;
21- // eslint-disable-next-line no-console
22- log . default = log . warn = ( ...args : unknown [ ] ) : void => console . warn ( ...args ) ;
23-
24- const spiedOn : Map <
1+ import {
2+ ExtendJest ,
3+ IsMockProp ,
4+ MockProp ,
255 Spyable ,
26- Map < string , MockProp < ValueOf < Spyable > > >
27- > = new Map ( ) ;
6+ SpyMap ,
7+ SpyOnProp ,
8+ } from "../typings/globals" ;
9+ import log from "./utils/logging" ;
10+ import messages from "./utils/messages" ;
11+
12+ const spiedOn : SpyMap < Spyable > = new Map ( ) ;
2813const getAllSpies = ( ) => {
29- const spies : Set < MockProp < ValueOf < Spyable > > > = new Set ( ) ;
14+ const spies : Set < MockProp > = new Set ( ) ;
3015 for ( const spiedProps of spiedOn . values ( ) ) {
3116 for ( const spy of spiedProps . values ( ) ) {
3217 spies . add ( spy ) ;
@@ -35,15 +20,15 @@ const getAllSpies = () => {
3520 return spies ;
3621} ;
3722
38- export class MockProp < T > {
23+ class MockPropInstance < T , K extends keyof T > implements MockProp < T , K > {
3924 private initialPropDescriptor : PropertyDescriptor ;
40- private initialPropValue : T ;
41- private object : Obj < T > ;
42- private propName : string ;
43- private propValue : T ;
44- private propValues : T [ ] = [ ] ;
25+ private initialPropValue : T [ K ] ;
26+ private object : T ;
27+ private propName : K ;
28+ private propValue : T [ K ] ;
29+ private propValues : T [ K ] [ ] = [ ] ;
4530
46- constructor ( { object, propName } : { object : Obj < T > ; propName : string } ) {
31+ constructor ( { object, propName } : { object : T ; propName : K } ) {
4732 this . initialPropDescriptor = this . validate ( { object, propName } ) ;
4833 this . object = object ;
4934 this . propName = propName ;
@@ -81,7 +66,7 @@ export class MockProp<T> {
8166 /**
8267 * Set the value of the mocked property
8368 */
84- public mockValue = ( value : T ) : MockProp < T > => {
69+ public mockValue = ( value : T [ K ] ) : MockProp < T , K > => {
8570 this . propValues = [ ] ;
8671 this . propValue = value ;
8772 return this ;
@@ -90,7 +75,7 @@ export class MockProp<T> {
9075 /**
9176 * Next value returned when the property is accessed
9277 */
93- public mockValueOnce = ( value : T ) : MockProp < T > => {
78+ public mockValueOnce = ( value : T [ K ] ) : MockProp < T , K > => {
9479 this . propValues . push ( value ) ;
9580 return this ;
9681 } ;
@@ -102,8 +87,8 @@ export class MockProp<T> {
10287 object,
10388 propName,
10489 } : {
105- object : Obj < T > ;
106- propName : string ;
90+ object : T ;
91+ propName : K ;
10792 } ) : PropertyDescriptor => {
10893 const acceptedTypes : Set < string > = new Set ( [ "function" , "object" ] ) ;
10994 if ( object === null || ! acceptedTypes . has ( typeof object ) ) {
@@ -158,7 +143,7 @@ export class MockProp<T> {
158143 /**
159144 * Shift and return the first next, defaulting to the mocked value
160145 */
161- private nextValue = ( ) : T => this . propValues . shift ( ) || this . propValue ;
146+ private nextValue = ( ) : T [ K ] => this . propValues . shift ( ) || this . propValue ;
162147}
163148
164149export const isMockProp : IsMockProp = ( object , propName ) => {
@@ -179,10 +164,10 @@ export const spyOnProp: SpyOnProp = (object, propName) => {
179164 if ( isMockProp ( object , propName ) ) {
180165 return spiedOn . get ( object ) . get ( propName ) ;
181166 }
182- return new MockProp ( { object, propName } ) ;
167+ return new MockPropInstance ( { object, propName } ) ;
183168} ;
184169
185- export const extend = ( jestInstance : typeof jest ) : void => {
170+ export const extend : ExtendJest = ( jestInstance : typeof jest ) : void => {
186171 const jestClearAll = jestInstance . clearAllMocks ;
187172 const jestResetAll = jestInstance . resetAllMocks ;
188173 const jestRestoreAll = jestInstance . restoreAllMocks ;
@@ -194,3 +179,5 @@ export const extend = (jestInstance: typeof jest): void => {
194179 spyOnProp,
195180 } ) ;
196181} ;
182+
183+ export * from "../typings/globals" ;
0 commit comments