File tree 2 files changed +24
-3
lines changed
packages/babel-plugin-jest-hoist/src
2 files changed +24
-3
lines changed Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ _getJestObj().mock("./App", () => () =>
15
15
void 0,
16
16
false,
17
17
{
18
- fileName : " /root/project/src/file.js " ,
18
+ fileName : _hoistedMockFactoryVariable () ,
19
19
lineNumber : 1 ,
20
20
columnNumber : 32
21
21
} ,
@@ -34,6 +34,10 @@ function _getJestObj() {
34
34
return jest ;
35
35
}
36
36
37
+ function _hoistedMockFactoryVariable() {
38
+ return " /root/project/src/file.js" ;
39
+ }
40
+
37
41
38
42
` ;
39
43
Original file line number Diff line number Diff line change @@ -146,8 +146,19 @@ FUNCTIONS.mock = args => {
146
146
const initNode = binding . path . node . init ;
147
147
148
148
if ( initNode && binding . constant && scope . isPure ( initNode , true ) ) {
149
- // replace the reference with its constant value
150
- id . replaceWith ( initNode ) ;
149
+ const identifier = scope . generateUidIdentifier (
150
+ 'hoistedMockFactoryVariable' ,
151
+ ) ;
152
+
153
+ binding . path . parentPath . insertAfter (
154
+ createHoistedVariable ( {
155
+ HOISTED_NAME : identifier ,
156
+ HOISTED_VALUE : binding . path . node . init ,
157
+ } ) ,
158
+ ) ;
159
+
160
+ // replace reference with a call to the new function
161
+ id . replaceWith ( callExpression ( identifier , [ ] ) ) ;
151
162
152
163
isAllowedIdentifier = true ;
153
164
}
@@ -191,6 +202,12 @@ function GETTER_NAME() {
191
202
}
192
203
` ;
193
204
205
+ const createHoistedVariable = statement `
206
+ function HOISTED_NAME() {
207
+ return HOISTED_VALUE;
208
+ }
209
+ ` ;
210
+
194
211
const isJestObject = ( expression : NodePath < Expression > ) : boolean => {
195
212
// global
196
213
if (
You can’t perform that action at this time.
0 commit comments