Skip to content

Commit 3950686

Browse files
committed
hack a "fix"
1 parent acb43e5 commit 3950686

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
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/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ FUNCTIONS.mock = args => {
132132
scope = scope.parent;
133133
}
134134

135+
if (!found) {
136+
const binding = scope.bindings[name];
137+
138+
// @ts-expect-error `init` does not exist
139+
if (binding?.constant && scope.isPure(binding.path.node.init, true)) {
140+
// how to hoist???
141+
found = true;
142+
}
143+
}
144+
135145
if (!found) {
136146
const isAllowedIdentifier =
137147
(scope.hasGlobal(name) && ALLOWED_IDENTIFIERS.has(name)) ||

0 commit comments

Comments
 (0)