Skip to content

Commit

Permalink
hack a "fix"
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Oct 29, 2020
1 parent 6960265 commit c07e8e8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`babel-plugin-jest-hoist automatic react runtime: automatic react runtime 1`] = `
jest.mock('./App', () => () => <div>Hello world</div>);
↓ ↓ ↓ ↓ ↓ ↓
_getJestObj().mock("./App", () => () =>
/*#__PURE__*/ _jsxDEV(
"div",
{
children: "Hello world"
},
void 0,
false,
{
fileName: _jsxFileName,
lineNumber: 1,
columnNumber: 32
},
this
)
);
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
var _jsxFileName = "";
function _getJestObj() {
const { jest } = require("@jest/globals");
_getJestObj = () => jest;
return jest;
}
`;

exports[`babel-plugin-jest-hoist top level mocking: top level mocking 1`] = `
require('x');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pluginTester({
],
},
code: `
jest.mock('./App', () => () => <div>Hello world</div>, {virtual: true});
jest.mock('./App', () => () => <div>Hello world</div>);
`,
snapshot: true,
},
Expand Down
56 changes: 30 additions & 26 deletions packages/babel-plugin-jest-hoist/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,41 +120,45 @@ FUNCTIONS.mock = args => {
moduleFactory.traverse(IDVisitor, {ids});
for (const id of ids) {
const {name} = id.node;
let found = false;
let scope = id.scope;

while (scope !== parentScope) {
if (scope.bindings[name]) {
found = true;
break;
return true;
}

scope = scope.parent;
}

if (!found) {
const isAllowedIdentifier =
(scope.hasGlobal(name) && ALLOWED_IDENTIFIERS.has(name)) ||
/^mock/i.test(name) ||
// Allow istanbul's coverage variable to pass.
/^(?:__)?cov/.test(name);

if (!isAllowedIdentifier) {
throw id.buildCodeFrameError(
'The module factory of `jest.mock()` is not allowed to ' +
'reference any out-of-scope variables.\n' +
'Invalid variable access: ' +
name +
'\n' +
'Allowed objects: ' +
Array.from(ALLOWED_IDENTIFIERS).join(', ') +
'.\n' +
'Note: This is a precaution to guard against uninitialized mock ' +
'variables. If it is ensured that the mock is required lazily, ' +
'variable names prefixed with `mock` (case insensitive) are permitted.\n',
ReferenceError,
);
}
const binding = scope.bindings[name];

// @ts-expect-error `init` does not exist
if (binding?.constant && scope.isPure(binding.path.node.init, true)) {
// how to hoist???
return true;
}

const isAllowedIdentifier =
(scope.hasGlobal(name) && ALLOWED_IDENTIFIERS.has(name)) ||
/^mock/i.test(name) ||
// Allow istanbul's coverage variable to pass.
/^(?:__)?cov/.test(name);

if (!isAllowedIdentifier) {
throw id.buildCodeFrameError(
'The module factory of `jest.mock()` is not allowed to ' +
'reference any out-of-scope variables.\n' +
'Invalid variable access: ' +
name +
'\n' +
'Allowed objects: ' +
Array.from(ALLOWED_IDENTIFIERS).join(', ') +
'.\n' +
'Note: This is a precaution to guard against uninitialized mock ' +
'variables. If it is ensured that the mock is required lazily, ' +
'variable names prefixed with `mock` (case insensitive) are permitted.\n',
ReferenceError,
);
}
}

Expand Down

0 comments on commit c07e8e8

Please sign in to comment.