[new-rule] prefer importing from @jest/globals
See original GitHub issueIt 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:
- Created a year ago
- Comments:10 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 fromjest
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.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