forked from jest-community/eslint-plugin-jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: prefer importing jest globals [new rule]
- Fix jest-community#1101 Issue: jest-community#1101
- Loading branch information
1 parent
505258c
commit 33146cc
Showing
7 changed files
with
167 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Prefer importing Jest globals (`prefer-jest-globals`) | ||
|
||
🔧 This rule is automatically fixable by the | ||
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
This rule aims to enforce explicit imports from `@jest/globals`. | ||
|
||
1. This is useful for ensuring that the Jest APIs are imported the same way in | ||
the codebase. | ||
2. When you can't modify Jest's | ||
[`injectGlobals`](https://jestjs.io/docs/configuration#injectglobals-boolean) | ||
configuration property, this rule can help to ensure that the Jest globals | ||
are imported explicitly and facilitate a migration to `@jest/globals`. | ||
|
||
## Rule details | ||
|
||
Examples of **incorrect** code for this rule | ||
|
||
```js | ||
/* eslint jest/prefer-jest-globals: "error" */ | ||
|
||
describe('foo', () => { | ||
it('accepts this input', () => { | ||
// ... | ||
}); | ||
}); | ||
``` | ||
|
||
Examples of **correct** code for this rule | ||
|
||
```js | ||
/* eslint jest/prefer-jest-globals: "error" */ | ||
|
||
import { describe, it } from '@jest/globals'; | ||
|
||
describe('foo', () => { | ||
it('accepts this input', () => { | ||
// ... | ||
}); | ||
}); | ||
``` | ||
|
||
## Further Reading | ||
|
||
- [Documentation](https://jestjs.io/docs/api) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { TSESLint } from '@typescript-eslint/utils'; | ||
import dedent from 'dedent'; | ||
import rule from '../prefer-jest-globals'; | ||
import { espreeParser } from './test-utils'; | ||
|
||
const ruleTester = new TSESLint.RuleTester({ | ||
parser: espreeParser, | ||
parserOptions: { | ||
ecmaVersion: 2015, | ||
sourceType: 'module', | ||
}, | ||
}); | ||
|
||
ruleTester.run('prefer-jest-globals.test', rule, { | ||
valid: [ | ||
{ | ||
code: dedent` | ||
import { test, expect } from '@jest/globals'; | ||
test('should pass', () => { | ||
expect(true).toBeDefined(); | ||
}); | ||
`, | ||
parserOptions: { sourceType: 'module' }, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: dedent` | ||
it("foo"); | ||
`, | ||
parserOptions: { sourceType: 'module' }, | ||
errors: [{ endColumn: 11, column: 1, messageId: 'preferJestGlobal' }], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import globalsJson from '../globals.json'; | ||
import { createRule } from './utils'; | ||
|
||
export default createRule({ | ||
name: __filename, | ||
meta: { | ||
docs: { | ||
category: 'Best Practices', | ||
description: 'Prefer importing Jest globals', | ||
recommended: false, | ||
}, | ||
messages: { | ||
preferJestGlobal: | ||
"Jest function \"{{ jestFunction }} is used but not imported from '@jest/globals'", | ||
}, | ||
fixable: 'code', | ||
type: 'suggestion', | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const jestFunctions = Object.keys(globalsJson); | ||
const importedJestFunctions: any[] = []; | ||
const usedJestFunctions = new Set(); | ||
|
||
return { | ||
ImportDeclaration(node) { | ||
// Check if the import source is '@jest/globals' | ||
if (node.source.value === '@jest/globals') { | ||
node.specifiers.forEach(specifier => { | ||
if ( | ||
specifier.type === 'ImportSpecifier' && | ||
jestFunctions.includes(specifier.imported.name) | ||
) { | ||
importedJestFunctions.push(specifier.imported.name); | ||
} | ||
}); | ||
} | ||
}, | ||
Identifier(node) { | ||
if (jestFunctions.includes(node.name)) { | ||
usedJestFunctions.add(node.name); | ||
} | ||
}, | ||
'Program:exit'() { | ||
usedJestFunctions.forEach(jestFunction => { | ||
if (!importedJestFunctions.includes(jestFunction)) { | ||
context.report({ | ||
node: context.getSourceCode().ast, | ||
messageId: 'preferJestGlobal', | ||
data: { jestFunction }, | ||
}); | ||
} | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters