Skip to content

Commit e751fbb

Browse files
committed
use a function
1 parent 9e088f1 commit e751fbb

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

packages/babel-plugin-jest-hoist/src/__tests__/__snapshots__/hoistPlugin.test.ts.snap

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ _getJestObj().mock("./App", () => () =>
1515
void 0,
1616
false,
1717
{
18-
fileName: "/root/project/src/file.js",
18+
fileName: _hoistedMockFactoryVariable(),
1919
lineNumber: 1,
2020
columnNumber: 32
2121
},
@@ -34,6 +34,10 @@ function _getJestObj() {
3434
return jest;
3535
}
3636
37+
function _hoistedMockFactoryVariable() {
38+
return "/root/project/src/file.js";
39+
}
40+
3741
3842
`;
3943

packages/babel-plugin-jest-hoist/src/index.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,19 @@ FUNCTIONS.mock = args => {
146146
const initNode = binding.path.node.init;
147147

148148
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, []));
151162

152163
isAllowedIdentifier = true;
153164
}
@@ -191,6 +202,12 @@ function GETTER_NAME() {
191202
}
192203
`;
193204

205+
const createHoistedVariable = statement`
206+
function HOISTED_NAME() {
207+
return HOISTED_VALUE;
208+
}
209+
`;
210+
194211
const isJestObject = (expression: NodePath<Expression>): boolean => {
195212
// global
196213
if (

0 commit comments

Comments
 (0)