Skip to content

Commit

Permalink
Merge branch 'main' into typescript-eslint-utils-7
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed May 2, 2024
2 parents c1e2f2e + df3202f commit 8158c98
Show file tree
Hide file tree
Showing 9 changed files with 606 additions and 496 deletions.
444 changes: 222 additions & 222 deletions .yarn/releases/yarn-3.8.1.cjs → .yarn/releases/yarn-3.8.2.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ plugins:
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: '@yarnpkg/plugin-interactive-tools'

yarnPath: .yarn/releases/yarn-3.8.1.cjs
yarnPath: .yarn/releases/yarn-3.8.2.cjs
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# [28.3.0](https://github.com/jest-community/eslint-plugin-jest/compare/v28.2.0...v28.3.0) (2024-04-27)


### Features

* prefer importing jest globals for specific types ([#1568](https://github.com/jest-community/eslint-plugin-jest/issues/1568)) ([c464ae3](https://github.com/jest-community/eslint-plugin-jest/commit/c464ae311b81f005af29df610d4032519125bafa))

# [28.2.0](https://github.com/jest-community/eslint-plugin-jest/compare/v28.1.1...v28.2.0) (2024-04-06)


Expand Down
36 changes: 36 additions & 0 deletions docs/rules/prefer-importing-jest-globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,42 @@ describe('foo', () => {
});
```

## Options

This rule can be configured as follows

```json
{
"type": "object",
"properties": {
"types": {
"type": "array",
"items": {
"type": "string",
"enum": ["hook", "describe", "test", "expect", "jest", "unknown"]
}
}
},
"additionalProperties": false
}
```

#### types

A list of Jest global types to enforce explicit imports for. By default, all
Jest globals are enforced.

This option is useful when you only want to enforce explicit imports for a
subset of Jest globals. For instance, when migrating to ESM, you might want to
enforce explicit imports only for the `jest` global, as of
[Jest's ESM documentation](https://jestjs.io/docs/ecmascript-modules#differences-between-esm-and-commonjs).

```json5
{
'jest/prefer-importing-jest-globals': ['error', { types: ['jest'] }],
}
```

## Further Reading

- [Documentation](https://jestjs.io/docs/api)
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-jest",
"version": "28.2.0",
"version": "28.3.0",
"description": "ESLint rules for Jest",
"keywords": [
"eslint",
Expand Down Expand Up @@ -87,7 +87,7 @@
"babel-jest": "^29.0.0",
"babel-plugin-replace-ts-export-assignment": "^0.0.2",
"dedent": "^1.5.0",
"eslint": "^7.0.0 || ^8.0.0 || ^9.0.0",
"eslint": "^7.0.0 || ^8.0.0",
"eslint-config-prettier": "^9.0.0",
"eslint-doc-generator": "^1.0.0",
"eslint-plugin-eslint-comments": "^3.1.2",
Expand Down Expand Up @@ -125,7 +125,7 @@
"optional": true
}
},
"packageManager": "yarn@3.8.1",
"packageManager": "yarn@3.8.2",
"engines": {
"node": "^16.10.0 || ^18.12.0 || >=20.0.0"
},
Expand Down
46 changes: 46 additions & 0 deletions src/rules/__tests__/prefer-importing-jest-globals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ ruleTester.run('prefer-importing-jest-globals', rule, {
`,
parserOptions: { sourceType: 'module' },
},
{
code: dedent`
test('should pass', () => {
expect(true).toBeDefined();
});
`,
options: [{ types: ['jest'] }],
parserOptions: { sourceType: 'module' },
},
{
code: dedent`
const { it } = require('@jest/globals');
it('should pass', () => {
expect(true).toBeDefined();
});
`,
options: [{ types: ['test'] }],
parserOptions: { sourceType: 'module' },
},
{
code: dedent`
// with require
Expand Down Expand Up @@ -85,6 +104,33 @@ ruleTester.run('prefer-importing-jest-globals', rule, {
},
],
},
{
code: dedent`
jest.useFakeTimers();
describe("suite", () => {
test("foo");
expect(true).toBeDefined();
})
`,
output: dedent`
import { jest } from '@jest/globals';
jest.useFakeTimers();
describe("suite", () => {
test("foo");
expect(true).toBeDefined();
})
`,
options: [{ types: ['jest'] }],
parserOptions: { sourceType: 'module' },
errors: [
{
endColumn: 5,
column: 1,
line: 1,
messageId: 'preferImportingJestGlobal',
},
],
},
{
code: dedent`
import React from 'react';
Expand Down
2 changes: 1 addition & 1 deletion src/rules/expect-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default createRule<
properties: {
assertFunctionNames: {
type: 'array',
items: [{ type: 'string' }],
items: { type: 'string' },
},
additionalTestBlockFunctions: {
type: 'array',
Expand Down
34 changes: 31 additions & 3 deletions src/rules/prefer-importing-jest-globals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
import {
type JestFnType,
createRule,
getAccessorValue,
getSourceCode,
Expand All @@ -20,6 +21,15 @@ const createFixerImports = (
: `const { ${allImportsFormatted} } = require('@jest/globals');`;
};

const allJestFnTypes: JestFnType[] = [
'hook',
'describe',
'test',
'expect',
'jest',
'unknown',
];

export default createRule({
name: __filename,
meta: {
Expand All @@ -31,10 +41,25 @@ export default createRule({
},
fixable: 'code',
type: 'problem',
schema: [],
schema: [
{
type: 'object',
properties: {
types: {
type: 'array',
items: {
type: 'string',
enum: allJestFnTypes,
},
},
},
additionalProperties: false,
},
],
},
defaultOptions: [],
defaultOptions: [{ types: allJestFnTypes }],
create(context) {
const { types = allJestFnTypes } = context.options[0] || {};
const importedFunctionsWithSource: Record<string, string> = {};
const functionsToImport = new Set<string>();
let reportingNode: TSESTree.Node;
Expand All @@ -55,7 +80,10 @@ export default createRule({
return;
}

if (jestFnCall.head.type !== 'import') {
if (
jestFnCall.head.type !== 'import' &&
types.includes(jestFnCall.type)
) {
functionsToImport.add(jestFnCall.name);
reportingNode ||= jestFnCall.head.node;
}
Expand Down

0 comments on commit 8158c98

Please sign in to comment.