-
Notifications
You must be signed in to change notification settings - Fork 234
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]
- Loading branch information
1 parent
505258c
commit 45af6d2
Showing
6 changed files
with
183 additions
and
1 deletion.
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-importing-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-importing-jest-globals: "error" */ | ||
|
||
describe('foo', () => { | ||
it('accepts this input', () => { | ||
// ... | ||
}); | ||
}); | ||
``` | ||
|
||
Examples of **correct** code for this rule | ||
|
||
```js | ||
/* eslint jest/prefer-importing-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,64 @@ | ||
import { TSESLint } from '@typescript-eslint/utils'; | ||
import dedent from 'dedent'; | ||
import rule from '../prefer-importing-jest-globals'; | ||
import { espreeParser } from './test-utils'; | ||
|
||
const ruleTester = new TSESLint.RuleTester({ | ||
parser: espreeParser, | ||
parserOptions: { | ||
ecmaVersion: 2015, | ||
sourceType: 'module', | ||
}, | ||
}); | ||
|
||
ruleTester.run('prefer-importing-jest-globals', rule, { | ||
valid: [ | ||
{ | ||
code: dedent` | ||
import { test, expect } from '@jest/globals'; | ||
test('should pass', () => { | ||
expect(true).toBeDefined(); | ||
}); | ||
`, | ||
parserOptions: { sourceType: 'module' }, | ||
}, | ||
{ | ||
code: dedent` | ||
import { it as itChecks } from '@jest/globals'; | ||
itChecks("foo"); | ||
`, | ||
parserOptions: { sourceType: 'module' }, | ||
}, | ||
{ | ||
code: dedent` | ||
const { test } = require('@jest/globals'); | ||
test("foo"); | ||
`, | ||
parserOptions: { sourceType: 'module' }, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: dedent` | ||
describe("suite", () => { | ||
test("foo"); | ||
expect(true).toBeDefined(); | ||
}) | ||
`, | ||
output: dedent` | ||
import { describe, test, expect } from '@jest/globals'; | ||
describe("suite", () => { | ||
test("foo"); | ||
expect(true).toBeDefined(); | ||
}) | ||
`, | ||
parserOptions: { sourceType: 'module' }, | ||
errors: [ | ||
{ endColumn: 3, column: 1, messageId: 'preferImportingJestGlobal' }, | ||
], | ||
}, | ||
], | ||
}); |
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,69 @@ | ||
import globalsJson from '../globals.json'; | ||
import { createRule, parseJestFnCall } from './utils'; | ||
|
||
export default createRule({ | ||
name: __filename, | ||
meta: { | ||
docs: { | ||
category: 'Best Practices', | ||
description: 'Prefer importing Jest globals', | ||
recommended: false, | ||
}, | ||
messages: { | ||
preferImportingJestGlobal: `Import the following Jest functions from '@jest/globals': {{ jestFunctions }}`, | ||
}, | ||
fixable: 'code', | ||
type: 'problem', | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const jestGlobalFunctions = Object.keys(globalsJson); | ||
const importedJestFunctions: string[] = []; | ||
const usedJestFunctions = new Set<string>(); | ||
|
||
return { | ||
CallExpression(node) { | ||
const jestFnCall = parseJestFnCall(node, context); | ||
|
||
if (!jestFnCall) { | ||
return; | ||
} | ||
if ( | ||
jestFnCall.head.type === 'import' && | ||
jestGlobalFunctions.includes(jestFnCall.name) | ||
) { | ||
importedJestFunctions.push(jestFnCall.name); | ||
} | ||
|
||
/* istanbul ignore else */ | ||
if (jestGlobalFunctions.includes(jestFnCall.name)) { | ||
usedJestFunctions.add(jestFnCall.name); | ||
} | ||
}, | ||
'Program:exit'() { | ||
const jestFunctionsToImport = Array.from(usedJestFunctions).filter( | ||
jestFunction => !importedJestFunctions.includes(jestFunction), | ||
); | ||
|
||
if (jestFunctionsToImport.length > 0) { | ||
const node = context.getSourceCode().ast; | ||
const jestFunctionsToImportFormatted = | ||
jestFunctionsToImport.join(', '); | ||
|
||
context.report({ | ||
node, | ||
messageId: 'preferImportingJestGlobal', | ||
data: { jestFunctions: jestFunctionsToImportFormatted }, | ||
fix(fixer) { | ||
return fixer.insertTextBefore( | ||
node, | ||
`import { ${jestFunctionsToImportFormatted} } from '@jest/globals';\n`, | ||
); | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |