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.

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

See original GitHub issue

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.)

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ecraig12345commented, May 17, 2022

I’m fine with waiting on the work you have in progress.

I had noticed the no-jest-import rule as well. Expanding it to cover @jest/globals seems like a breaking change, especially with that rule being part of the recommended config. The intent is also different: importing from jest does literally nothing and is never desirable that I’m aware of, whereas whether to import from @jest/globals vs. using ambient globals (and referencing @types/jest for typescript) is a style preference.

1reaction
SimenBcommented, May 10, 2022

Note that you can run jest with injectGlobals: false and at least get runtime errors today. I doubt we’ll ever change the default, but who knows 🙂 It’s at least only a config option away

Read more comments on GitHub >

github_iconTop Results From Across the Web

Globals - Jest
You don't have to require or import anything to use them. However, if you prefer explicit imports, you can do import {describe, expect,...
Read more >
How can I import Jest? - Stack Overflow
Try the code below: import {describe, expect, it } from '@jest/globals'. if you prefer explicit imports, you can do import {describe, ...
Read more >
Export Controls - Trade Commissioner Service
Export controls are applied by many governments as a means to regulate, and sometimes deny, trade in specific lists of military and dual-use ......
Read more >
jest-without-globals - npm
Explicitly import Jest globals. Latest version: 0.0.3, last published: 3 years ago. Start using jest-without-globals in your project by ...
Read more >
[new-rule] prefer importing from @jest/globals - Devscope.io
jest-community/eslint-plugin-jest: [new-rule] prefer importing from @jest/globals.
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