Skip to content

Commit

Permalink
Do not hoist nested jest.mock calls, only add the getJestObj getter
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Feb 28, 2023
1 parent 6b9af46 commit 7ca29c5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ jest.mock('someModule', () => {
↓ ↓ ↓ ↓ ↓ ↓
_getJestObj().mock('someModule', () => {
expect(_getJestObj().mock('someNode')).toBe(1);
require('x');
expect(_getJestObj().mock('someNode')).toBe(1);
});
function _getJestObj() {
const {jest} = require('@jest/globals');
Expand Down
5 changes: 4 additions & 1 deletion packages/babel-plugin-jest-hoist/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,10 @@ const extractJestObjExprIfHoistable = (expr: NodePath): JestObjInfo | null => {
// Important: Call the function check last
// It might throw an error to display to the user,
// which should only happen if we're already sure it's a call on the Jest object.
const functionIsHoistable = FUNCTIONS[propertyName]?.(args) ?? false;
const functionIsHoistable =
expr.parentPath.isExpressionStatement() &&
(FUNCTIONS[propertyName]?.(args) ?? false);

let functionHasHoistableScope = functionIsHoistable;
for (
let path: NodePath<Node> | null = expr;
Expand Down

0 comments on commit 7ca29c5

Please sign in to comment.