Skip to content

Commit

Permalink
use a function
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Nov 1, 2020
1 parent 2e62880 commit 41e050e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Expand Up @@ -15,7 +15,7 @@ _getJestObj().mock("./App", () => () =>
void 0,
false,
{
fileName: "/root/project/src/file.js",
fileName: _hoistedMockFactoryVariable(),
lineNumber: 1,
columnNumber: 32
},
Expand All @@ -34,6 +34,10 @@ function _getJestObj() {
return jest;
}
function _hoistedMockFactoryVariable() {
return "/root/project/src/file.js";
}
`;

Expand Down
21 changes: 19 additions & 2 deletions packages/babel-plugin-jest-hoist/src/index.ts
Expand Up @@ -146,8 +146,19 @@ FUNCTIONS.mock = args => {
const initNode = binding.path.node.init;

if (initNode && binding.constant && scope.isPure(initNode, true)) {
// replace the reference with its constant value
id.replaceWith(initNode);
const identifier = scope.generateUidIdentifier(
'hoistedMockFactoryVariable',
);

binding.path.parentPath.insertAfter(
createHoistedVariable({
HOISTED_NAME: identifier,
HOISTED_VALUE: binding.path.node.init,
}),
);

// replace reference with a call to the new function
id.replaceWith(callExpression(identifier, []));

isAllowedIdentifier = true;
}
Expand Down Expand Up @@ -191,6 +202,12 @@ function GETTER_NAME() {
}
`;

const createHoistedVariable = statement`
function HOISTED_NAME() {
return HOISTED_VALUE;
}
`;

const isJestObject = (expression: NodePath<Expression>): boolean => {
// global
if (
Expand Down

0 comments on commit 41e050e

Please sign in to comment.