42

I would like to get rid of global variables in my Jest test code. Specifically describe, it, and expect:

describe('Welcome (Snapshot)', () => {
  it('Welcome renders hello world', () => {
    // ...
  });
});

So I tried to add:

import { describe, it } from 'jest';

and

import jest from 'jest';

jest.describe('Welcome (Snapshot)', () => {
  jest.it('Welcome renders hello world', () => {
    // ...
  });
});

And other variation but it's not working.

How can I get my Jest test code working without globals?

Harsh Patel
  • 6,334
  • 10
  • 40
  • 73
guy mograbi
  • 27,391
  • 16
  • 83
  • 122

4 Answers4

31

The simplest solution for this is adding jest: true to your env configuration in ESLint, like so:

"env": {
  "browser": true,
  "node": true,
  "jasmine": true,
  "jest": true,
  "es6": true
},
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alejandro Garcia Anglada
  • 2,373
  • 1
  • 25
  • 41
  • 1
    For reference, here are [all the ways to configure the ESLint environment](https://eslint.org/docs/user-guide/configuring#specifying-environments) (for Jest or other situations) – Cato Minor Nov 21 '17 at 08:44
30

Try the code below:

import {describe, expect, it} from '@jest/globals'

if you prefer explicit imports, you can do import {describe, expect, test} from '@jest/globals'.

Source https://jestjs.io/docs/en/api

derekbaker783
  • 8,109
  • 4
  • 36
  • 50
  • If I do that, method `toBeInTheDocument` is marked as missing. – Danon Oct 31 '20 at 14:42
  • @Danon, The following SO post suggests you're importing from the wrong location. "You need to install jest-dom to enable it." https://stackoverflow.com/questions/56547215/react-testing-library-why-is-tobeinthedocument-not-a-function#answer-56557915 – derekbaker783 Oct 31 '20 at 15:27
  • Note that in order to import `jest` from `@jest/globals`, for now you need to disable injecting globals by passing `--injectGlobals=false` as an argument to the jest command. For more on this issue: https://github.com/facebook/jest/issues/9920 – wristbands Nov 05 '21 at 00:28
  • this worked for me also to eliminate the "not found in assertion type" which appeared in intellij – Fernando Gabrieli May 26 '22 at 23:50
  • 1
    "Do not import `@jest/globals` outside of the Jest test environment" Sadly this doesn't cure my storybook woes. – Jefferson Jul 20 '22 at 15:26
25

After I realized Jest is running in Node.js, it realized I could do this:

let { describe, it } = global;

It is not perfect, but one step closer... now I don't need to configure my linter with global variables any more.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
guy mograbi
  • 27,391
  • 16
  • 83
  • 122
6

I similarly don't like using or relying on global variables, and after copy/pasting various workarounds in different projects, I decided to create jest-without-globals as a very tiny wrapper to support importing Jest's features.

Per the usage documentation, it's straightforward to use:

import { describe, it, expect } from 'jest-without-globals'

describe('describe should create a section', () => {
  it('it should checkmark', () => {
    expect('').toBe('')
   })
})

All of the functions available in Jest's API, as well as jest and expect, can be imported from jest-without-globals.

Using jest-without-globals, I don't need to copy/paste workarounds anymore and don't need to configure settings like ESLint's jest environment, it just works as a simple import.

It's also written in TypeScript, so you have typings out-of-the-box and it's fully tested to ensure it keeps working correctly.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
agilgur5
  • 667
  • 12
  • 30
  • 1
    You can now explicitly import all of these directly from Jest. See [my answer](https://stackoverflow.com/questions/41324636/how-can-i-import-jest/64396131#64396131) for details. – derekbaker783 Apr 30 '21 at 16:35