Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Closed
tqwewe opened this issue Jun 29, 2020 · 8 comments
Closed

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

tqwewe opened this issue Jun 29, 2020 · 8 comments

Comments

@tqwewe
Copy link

tqwewe commented Jun 29, 2020

🐛 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
@ahnpnl
Copy link
Contributor

ahnpnl commented Jul 10, 2020

Seem to be related to #10147

@SimenB
Copy link
Member

SimenB commented Sep 7, 2020

Without a reproduction it's hard to say - @acidic9 what version of babel-plugin-jest-hoist is installed in your project?

@ahnpnl
Copy link
Contributor

ahnpnl commented Sep 8, 2020

I managed to reproduce the error that @acidic9 ran into.

@SimenB You can check out this branch https://github.com/ahnpnl/ts-jest-babel-example/tree/jest-globals and run the test in foo.spec.ts.

@Katona
Copy link

Katona commented Feb 19, 2021

I am also facing this, is there any workaround?

@IAMtheIAM
Copy link

So what are we supposed to do??

@DZakh
Copy link

DZakh commented Sep 30, 2021

I've removed jest from the @globals/jest import and added /* globals jest */ comment for the file to calm down eslint. Not an ideal solution, but it works.

@SimenB
Copy link
Member

SimenB commented Sep 30, 2021

Status here is still "missing reproduction". Please open a new issue with a proper reproduction, and we can take a look

I managed to reproduce the error that @acidic9 ran into.

@SimenB You can check out this branch https://github.com/ahnpnl/ts-jest-babel-example/tree/jest-globals and run the test in foo.spec.ts.

This doesn't exist, sorry I missed it at the time

@SimenB SimenB closed this as completed Sep 30, 2021
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 31, 2021
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Projects
None yet
Development

No branches or pull requests

6 participants