Warning
Deprecation Notice: As of Jest 26, the @jest/globals
package can be used to explicitly import
Jest functions:
import { describe, it, expect } from '@jest/globals'
Jest 26.5 further added the injectGlobals
config that you can set to false
to disable globals in your test files:
// jest.config.js
/** @type {import('jest').Config} */
const config = {
injectGlobals: false,
};
module.exports = config;
For versions of Jest older than 26, you can use the jest-without-globals
package as described below.
Explicitly import Jest globals.
npm i -D jest-without-globals
(note that jest
is a peer dependency)
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
.
Can take a look at the tests for usage of more of the globals, but basically you can now just import
any Jest global.
Basically just takes Jest's globals and exports them. The source code is currently shorter than this README, so take a look under the hood! :)
Some inspiration came from this workaround to avoid globals, which I had been using in projects previously.
I wanted a package I could re-use in all my projects' tests instead of constantly having to create a helper file and map its name, and so jest-without-globals
was born!
Can also see my PR for implementing this directly in Jest. It also adds a noJestGlobals
configuration option that would prevent these globals from being inserted into your environment as well (which is something that isn't possible with a simple user-land library like this).