File tree Expand file tree Collapse file tree 3 files changed +63
-4
lines changed
packages/jest-runtime/src Expand file tree Collapse file tree 3 files changed +63
-4
lines changed Original file line number Diff line number Diff line change 4
4
5
5
### Fixes
6
6
7
+ - ` [jest-runtime] ` Fix regression when using ` jest.isolateModules ` and mocks ([ #11882 ] ( https://github.com/facebook/jest/pull/11882 ) )
8
+
7
9
### Chore & Maintenance
8
10
9
11
### Performance
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
+ import { tmpdir } from 'os' ;
9
+ import * as path from 'path' ;
10
+ import { cleanup , createEmptyPackage , writeFiles } from '../Utils' ;
11
+ import runJest from '../runJest' ;
12
+
13
+ const DIR = path . resolve ( tmpdir ( ) , 'isolate-modules.test' ) ;
14
+
15
+ beforeEach ( ( ) => {
16
+ cleanup ( DIR ) ;
17
+ createEmptyPackage ( DIR ) ;
18
+ } ) ;
19
+
20
+ afterAll ( ( ) => cleanup ( DIR ) ) ;
21
+
22
+ test ( 'works with mocks' , ( ) => {
23
+ writeFiles ( DIR , {
24
+ 'config.js' : `
25
+ module.exports.getBoolean = function getBoolean(variableName) {
26
+ return false;
27
+ }
28
+ ` ,
29
+ 'read.js' : `
30
+ const {getBoolean} = require('./config');
31
+
32
+ const value = getBoolean('foo');
33
+ console.log("was " + value);
34
+ ` ,
35
+ 'test.js' : `
36
+ jest.mock('./config');
37
+ const config = require('./config');
38
+
39
+ test('dummy test', () => {
40
+ const configGetMock = config.getBoolean.mockImplementation(() => {
41
+ return true;
42
+ });
43
+
44
+ jest.isolateModules(() => {
45
+ require("./read");
46
+ });
47
+
48
+ expect(configGetMock).toBeCalledTimes(1);
49
+ })
50
+ ` ,
51
+ } ) ;
52
+ const { exitCode} = runJest ( DIR ) ;
53
+
54
+ expect ( exitCode ) . toBe ( 0 ) ;
55
+ } ) ;
Original file line number Diff line number Diff line change @@ -861,12 +861,14 @@ export default class Runtime {
861
861
{ conditions : this . cjsConditions } ,
862
862
) ;
863
863
864
- const mockRegistry = this . _isolatedMockRegistry || this . _mockRegistry ;
865
-
866
- if ( mockRegistry . get ( moduleID ) ) {
867
- return mockRegistry . get ( moduleID ) ;
864
+ if ( this . _isolatedMockRegistry ?. has ( moduleID ) ) {
865
+ return this . _isolatedMockRegistry . get ( moduleID ) ;
866
+ } else if ( this . _mockRegistry . has ( moduleID ) ) {
867
+ return this . _mockRegistry . get ( moduleID ) ;
868
868
}
869
869
870
+ const mockRegistry = this . _isolatedMockRegistry || this . _mockRegistry ;
871
+
870
872
if ( this . _mockFactories . has ( moduleID ) ) {
871
873
// has check above makes this ok
872
874
const module = this . _mockFactories . get ( moduleID ) ! ( ) ;
You can’t perform that action at this time.
0 commit comments