Skip to content

Cannot use jest from @jest/globals in mock #10218

@tqwewe

Description

@tqwewe

🐛 Bug Report

When attempting to use jest.* within a jest.mock function, jest fails due to the variable jest being used as an out-of-scope variable.

To Reproduce

The following code fails with:

The module factory of jest.mock() is not allowed to reference any out-of-scope variables.
Invalid variable access: jest

import { jest } from '@globals/jest';

jest.mock('module', () => {
  const originalModule = jest.requireActual('module');
  
  return {
    __esModule: true,
    ...originalModule,
    method: jest.fn(),
  }
});

Expected behavior

Expect jest to be a whitelisted object for scoped-variables within mocks.

Link to repl or repo (highly encouraged)

None.

envinfo

Binaries:
  Node: 14.4.0
  Yarn: 1.22.4
  npm: 6.14.5

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions