File tree Expand file tree Collapse file tree 3 files changed +34
-1
lines changed Expand file tree Collapse file tree 3 files changed +34
-1
lines changed Original file line number Diff line number Diff line change 22
22
- ` [jest-haste-map] ` Don't throw on missing mapper in Node crawler ([ #8558 ] ( https://github.com/facebook/jest/pull/8558 ) )
23
23
- ` [jest-core] ` Fix incorrect ` passWithNoTests ` warning ([ #8595 ] ( https://github.com/facebook/jest/pull/8595 ) )
24
24
- ` [jest-snapshots] ` Fix test retries that contain snapshots ([ #8629 ] ( https://github.com/facebook/jest/pull/8629 ) )
25
+ - ` [jest-mock] ` Fix incorrect assignments when restoring mocks in instances where they originally didn't exist ([ #8631 ] ( https://github.com/facebook/jest/pull/8631 ) )
25
26
26
27
### Chore & Maintenance
27
28
Original file line number Diff line number Diff line change @@ -542,6 +542,32 @@ describe('moduleMocker', () => {
542
542
} ) ;
543
543
} ) ;
544
544
545
+ it ( 'mocks the method in the passed object itself' , ( ) => {
546
+ const parent = { func : ( ) => 'abcd' } ;
547
+ const child = Object . create ( parent ) ;
548
+
549
+ moduleMocker . spyOn ( child , 'func' ) . mockReturnValue ( 'efgh' ) ;
550
+
551
+ expect ( child . hasOwnProperty ( 'func' ) ) . toBe ( true ) ;
552
+ expect ( child . func ( ) ) . toEqual ( 'efgh' ) ;
553
+ expect ( parent . func ( ) ) . toEqual ( 'abcd' ) ;
554
+ } ) ;
555
+
556
+ it ( 'should delete previously inexistent methods when restoring' , ( ) => {
557
+ const parent = { func : ( ) => 'abcd' } ;
558
+ const child = Object . create ( parent ) ;
559
+
560
+ moduleMocker . spyOn ( child , 'func' ) . mockReturnValue ( 'efgh' ) ;
561
+
562
+ moduleMocker . restoreAllMocks ( ) ;
563
+ expect ( child . func ( ) ) . toEqual ( 'abcd' ) ;
564
+
565
+ moduleMocker . spyOn ( parent , 'func' ) . mockReturnValue ( 'jklm' ) ;
566
+
567
+ expect ( child . hasOwnProperty ( 'func' ) ) . toBe ( false ) ;
568
+ expect ( child . func ( ) ) . toEqual ( 'jklm' ) ;
569
+ } ) ;
570
+
545
571
it ( 'supports mock value returning undefined' , ( ) => {
546
572
const obj = {
547
573
func : ( ) => 'some text' ,
Original file line number Diff line number Diff line change @@ -1009,9 +1009,15 @@ class ModuleMockerClass {
1009
1009
) ;
1010
1010
}
1011
1011
1012
+ const isMethodOwner = object . hasOwnProperty ( methodName ) ;
1013
+
1012
1014
// @ts -ignore overriding original method with a Mock
1013
1015
object [ methodName ] = this . _makeComponent ( { type : 'function' } , ( ) => {
1014
- object [ methodName ] = original ;
1016
+ if ( isMethodOwner ) {
1017
+ object [ methodName ] = original ;
1018
+ } else {
1019
+ delete object [ methodName ] ;
1020
+ }
1015
1021
} ) ;
1016
1022
1017
1023
// @ts -ignore original method is now a Mock
You can’t perform that action at this time.
0 commit comments