question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

@jest/globals breaks jest.mock

See original GitHub issue

šŸ’„ Regression Report

In my project I am using babel-jest and jest 25.3.0. I have 2 places using jest.mock, one is in setupFiles script (letā€™s call it setup.js):

jest.mock('lodash.debounce', () => func => func);

The other is on top level of one of the test files:

jest.mock('axios', () => {
  const axiosMock = jest.fn();
  axiosMock.CancelToken = {
    source: () => ({ cancel: jest.fn() }),
  };
  return axiosMock;
});

This was working fine until today I added a new npm package (thus updated yarn.lock).

Initially all tests failed at setup.js with this error:

Do not import `@jest/globals` outside of the Jest test environment

After commenting out the jest.mock in setup.js, the test file with mock axios code also failed with the same error. Removing jest.mock will remove this error (but the tests will fail)

Last working version

I was using version 25.3.0, however since this happens, the error wonā€™t go away even if I delete yarn.lock, node_modules folder and reinstall everything. I compared yarn.lock changes and noticed that @jest/globals wasnā€™t there before I add new dependency today. It seems that this is introduced at 25.5.0, but since most of the jest packages reference other jest packages with versioning ^25.3.0, @jest/globals will still be installed even if I reinstall jest 25.3.0.

The only workaround I can find out now is to revert jest to 24.9.0

Worked up to version: probably 24.9.0

Stopped working in version: probably all 25 and 26 versions

To Reproduce

Please refer to Regression Report where I described what my test code looks like

Expected behavior

Test codes should run and not throw error.

Link to repl or repo (highly encouraged)

Itā€™s a private project so canā€™t provide the full repo, Iā€™ll see if I can make a minimal repo later.

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: Linux 4.15 Ubuntu 18.04.3 LTS (Bionic Beaver)
    CPU: (2) x64 Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
  Binaries:
    Node: 10.18.1 - ~/.nvm/versions/node/v10.18.1/bin/node
    Yarn: 1.22.0 - ~/.nvm/versions/node/v10.18.1/bin/yarn
    npm: 6.13.4 - ~/.nvm/versions/node/v10.18.1/bin/npm
  npmPackages:
    jest: 24.9.0 => 24.9.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:18
  • Comments:16

github_iconTop GitHub Comments

5reactions
dmitrypapka1commented, Jun 15, 2020

Spec file starting with:

jest.mock('path/to/some/dependency');

results:

Do not import `@jest/globals` outside of the Jest test environment

Problem is present while using babel-jest version 25.5.1. Upgrading babel-jest to the latest version (at the moment is 26.0.1) results to the same problem.

Workaround is use babel-jest 21.2.0

This worked for me. So, unfortunatelly, it looks like Iā€™m forced to use outdated babel-jest versionā€¦ Thanks, @yubaoquan!

4reactions
yubaoquancommented, Jun 11, 2020

Iā€™m not sure weither it is a same problem. I am using ā€œbabel-jestā€: ā€œ^26.0.1ā€ and ā€œvue-jestā€: ā€œ^3.0.5ā€. The jest version I found in node_modules is 24.9.0.

Every time when there is a jest.mock invoke in test file, the exception will throw:

Cannot find module '@jest/globals' from xxxx

Below is a demo:

import $ajax from 'axios'

jest.mock('axios', () => {
  return {
    __esModule: true,
    default: jest.fn(() => 'mock-ajax'),
  }
})

describe('mock test', () => {
  it('Mock ajax success', () => {
    console.info($ajax)
    expect(1).toEqual(1)
  })
})

Workaround is use babel-jest 21.2.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

Globals - Jest
Globals. In your test files, Jest puts each of these methods and objects into the global environment. You don't have to require or...
Read more >
Mocking globals in Jest - Stack Overflow
As every test suite run its own environment, you can mock globals by just overwriting them. All global variables can be accessed by...
Read more >
Mocking es6 modules and globals with Jest | by Emily M Coco
Mocking es6 modules and globals with Jest. Navigate around the immovable objects between you and your code coverage. There are many excellentĀ ...
Read more >
Globals Ā· Jest
Globals. In your test files, Jest puts each of these methods and objects into the global ... requireMock(moduleName); test(name, fn); test.only(name, fn)Ā ...
Read more >
babel-jest - Awesome JS
[@jest/globals, jest-mock] Add jest.Spied* utility types (#13440) ... [jest-mock] Revert #13145 which broke mocking of transpiled ES modules.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found