File tree Expand file tree Collapse file tree 4 files changed +34
-9
lines changed
babel-plugin-jest-hoist/__tests__
babel-plugin-jest-hoist/src Expand file tree Collapse file tree 4 files changed +34
-9
lines changed Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ beforeEach(() => {
15
15
run ( 'yarn' , DIR ) ;
16
16
} ) ;
17
17
18
- it ( 'sucessfully runs the tests inside `babel-plugin-jest-hoist/`' , ( ) => {
18
+ it ( 'successfully runs the tests inside `babel-plugin-jest-hoist/`' , ( ) => {
19
19
const { json} = runWithJson ( DIR , [ '--no-cache' , '--coverage' ] ) ;
20
20
expect ( json . success ) . toBe ( true ) ;
21
21
expect ( json . numTotalTestSuites ) . toBe ( 3 ) ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ import { jest } from '@jest/globals' ;
10
+
11
+ // The virtual mock call below will be hoisted above this `require` call.
12
+ const virtualModule = require ( 'virtual-module' ) ;
13
+
14
+ jest . mock ( 'virtual-module' , ( ) => 'kiwi' , { virtual : true } ) ;
15
+
16
+ test ( 'works with virtual modules' , ( ) => {
17
+ expect ( virtualModule ) . toBe ( 'kiwi' ) ;
18
+ } ) ;
Original file line number Diff line number Diff line change @@ -183,6 +183,12 @@ export default (): {visitor: Visitor} => {
183
183
path . node . _blockHoist = Infinity ;
184
184
}
185
185
} ,
186
+ ImportDeclaration ( path ) {
187
+ if ( path . node . source . value === '@jest/globals' ) {
188
+ // @ts -ignore: private, magical property
189
+ path . node . _blockHoist = Infinity ;
190
+ }
191
+ } ,
186
192
} ;
187
193
188
194
return { visitor} ;
Original file line number Diff line number Diff line change @@ -331,11 +331,6 @@ class Runtime {
331
331
modulePath = manualMock ;
332
332
}
333
333
334
- if ( moduleName === '@jest/globals' ) {
335
- // @ts -ignore: we don't care that it's not assignable to T
336
- return this . getGlobalsForFile ( from ) ;
337
- }
338
-
339
334
if ( moduleName && this . _resolver . isCoreModule ( moduleName ) ) {
340
335
return this . _requireCoreModule ( moduleName ) ;
341
336
}
@@ -526,12 +521,18 @@ class Runtime {
526
521
} ;
527
522
}
528
523
529
- requireModuleOrMock ( from : Config . Path , moduleName : string ) : unknown {
524
+ requireModuleOrMock < T = unknown > ( from : Config . Path , moduleName : string ) : T {
525
+ // this module is unmockable
526
+ if ( moduleName === '@jest/globals' ) {
527
+ // @ts -ignore: we don't care that it's not assignable to T
528
+ return this . getGlobalsForFile ( from ) ;
529
+ }
530
+
530
531
try {
531
532
if ( this . _shouldMock ( from , moduleName ) ) {
532
- return this . requireMock ( from , moduleName ) ;
533
+ return this . requireMock < T > ( from , moduleName ) ;
533
534
} else {
534
- return this . requireModule ( from , moduleName ) ;
535
+ return this . requireModule < T > ( from , moduleName ) ;
535
536
}
536
537
} catch ( e ) {
537
538
const moduleNotFound = Resolver . tryCastModuleNotFoundError ( e ) ;
You can’t perform that action at this time.
0 commit comments