close

prefer-importing-jest-globals

Unreleased

Configuration

rslint.config.ts
import { defineConfig, jestPlugin } from '@rslint/core';

export default defineConfig([
  jestPlugin.configs.recommended,
  {
    rules: {
      'jest/prefer-importing-jest-globals': 'error',
    },
  },
]);

Rule Details

Prefer explicit imports from @jest/globals instead of relying on Jest's injected globals. That keeps Jest APIs imported consistently across the codebase and helps migrations when injectGlobals cannot be enabled (for example some ESM setups).

Examples of incorrect code for this rule:

describe('foo', () => {
  it('accepts this input', () => {
    expect(true).toBeDefined();
  });
});

Examples of correct code for this rule:

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

describe('foo', () => {
  it('accepts this input', () => {
    expect(true).toBeDefined();
  });
});

Options

interface Options {
  types?: Array<'hook' | 'describe' | 'test' | 'expect' | 'jest' | 'unknown'>;
}

types

A list of Jest global kinds to enforce explicit imports for. By default all Jest globals are enforced. Restricting types is useful during incremental migrations — for example enforcing only the jest helper when adopting ESM.

Examples of incorrect code with { "types": ["jest"] }:

{ "jest/prefer-importing-jest-globals": ["error", { "types": ["jest"] }] }
jest.useFakeTimers();

Examples of correct code with { "types": ["jest"] }:

{ "jest/prefer-importing-jest-globals": ["error", { "types": ["jest"] }] }
import { jest } from '@jest/globals';

jest.useFakeTimers();

// other globals may still be injected
describe('suite', () => {
  test('foo', () => {
    expect(true).toBeDefined();
  });
});

Original Documentation

Differences from ESLint

  • For const x = 1, { expect } = require('@jest/globals'), autofix keeps the existing expect binding when merging new names. ESLint only reads the first declarator, so it can drop that binding. Sibling declarators such as x = 1 are still removed on both sides.