Skip to content

[new-rule] prefer importing from @jest/globals #1101

Closed
@ecraig12345

Description

@ecraig12345

It would be nice to have a rule (with fixer) enforcing that APIs such as expect, describe, it, jest, etc should be imported from @jest/globals. If there's interest in this rule, I'd probably be willing to implement it.

Motivation:

  • Reduce global type pollution by not needing to put "types": ["jest"] in a tsconfig
  • Allow removing dependency on @types/jest which can be outdated, mismatched, and/or pull in extra versions of packages (this is an issue as of writing--jest 28 has released but the types are still on 27 and pull in some outdated deps from 27)
  • Unblock adoption of future mode with globals unavailable once that's implemented (I'm not sure where that is on their roadmap)

Here's an example of code with the rule enabled, and an error it might catch:

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

describe('foo', () => {
  // ERROR - you forgot to import beforeAll
  beforeAll(() => {
    jest.useFakeTimers();
  });

  it('does stuff', () => {
    expect(foo()).toBe(0);
  });
});

(I assume this would be blocked by #556, but looks like there's an approved PR #1094 addressing that.)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions