Skip to content

Commit d0d73fa

Browse files
committed
hack a "fix"
1 parent f310a8f commit d0d73fa

File tree

3 files changed

+68
-27
lines changed

3 files changed

+68
-27
lines changed

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`babel-plugin-jest-hoist automatic react runtime: automatic react runtime 1`] = `
4+
5+
jest.mock('./App', () => () => <div>Hello world</div>);
6+
7+
↓ ↓ ↓ ↓ ↓ ↓
8+
9+
_getJestObj().mock("./App", () => () =>
10+
/*#__PURE__*/ _jsxDEV(
11+
"div",
12+
{
13+
children: "Hello world"
14+
},
15+
void 0,
16+
false,
17+
{
18+
fileName: _jsxFileName,
19+
lineNumber: 1,
20+
columnNumber: 32
21+
},
22+
this
23+
)
24+
);
25+
26+
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
27+
var _jsxFileName = "";
28+
29+
function _getJestObj() {
30+
const { jest } = require("@jest/globals");
31+
32+
_getJestObj = () => jest;
33+
34+
return jest;
35+
}
36+
37+
38+
`;
39+
340
exports[`babel-plugin-jest-hoist top level mocking: top level mocking 1`] = `
441
542
require('x');

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pluginTester({
2323
],
2424
},
2525
code: `
26-
jest.mock('./App', () => () => <div>Hello world</div>, {virtual: true});
26+
jest.mock('./App', () => () => <div>Hello world</div>);
2727
`,
2828
snapshot: true,
2929
},

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

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -120,41 +120,45 @@ FUNCTIONS.mock = args => {
120120
moduleFactory.traverse(IDVisitor, {ids});
121121
for (const id of ids) {
122122
const {name} = id.node;
123-
let found = false;
124123
let scope = id.scope;
125124

126125
while (scope !== parentScope) {
127126
if (scope.bindings[name]) {
128-
found = true;
129-
break;
127+
return true;
130128
}
131129

132130
scope = scope.parent;
133131
}
134132

135-
if (!found) {
136-
const isAllowedIdentifier =
137-
(scope.hasGlobal(name) && ALLOWED_IDENTIFIERS.has(name)) ||
138-
/^mock/i.test(name) ||
139-
// Allow istanbul's coverage variable to pass.
140-
/^(?:__)?cov/.test(name);
141-
142-
if (!isAllowedIdentifier) {
143-
throw id.buildCodeFrameError(
144-
'The module factory of `jest.mock()` is not allowed to ' +
145-
'reference any out-of-scope variables.\n' +
146-
'Invalid variable access: ' +
147-
name +
148-
'\n' +
149-
'Allowed objects: ' +
150-
Array.from(ALLOWED_IDENTIFIERS).join(', ') +
151-
'.\n' +
152-
'Note: This is a precaution to guard against uninitialized mock ' +
153-
'variables. If it is ensured that the mock is required lazily, ' +
154-
'variable names prefixed with `mock` (case insensitive) are permitted.\n',
155-
ReferenceError,
156-
);
157-
}
133+
const binding = scope.bindings[name];
134+
135+
// @ts-expect-error `init` does not exist
136+
if (binding?.constant && scope.isPure(binding.path.node.init, true)) {
137+
// how to hoist???
138+
return true;
139+
}
140+
141+
const isAllowedIdentifier =
142+
(scope.hasGlobal(name) && ALLOWED_IDENTIFIERS.has(name)) ||
143+
/^mock/i.test(name) ||
144+
// Allow istanbul's coverage variable to pass.
145+
/^(?:__)?cov/.test(name);
146+
147+
if (!isAllowedIdentifier) {
148+
throw id.buildCodeFrameError(
149+
'The module factory of `jest.mock()` is not allowed to ' +
150+
'reference any out-of-scope variables.\n' +
151+
'Invalid variable access: ' +
152+
name +
153+
'\n' +
154+
'Allowed objects: ' +
155+
Array.from(ALLOWED_IDENTIFIERS).join(', ') +
156+
'.\n' +
157+
'Note: This is a precaution to guard against uninitialized mock ' +
158+
'variables. If it is ensured that the mock is required lazily, ' +
159+
'variable names prefixed with `mock` (case insensitive) are permitted.\n',
160+
ReferenceError,
161+
);
158162
}
159163
}
160164

0 commit comments

Comments
 (0)