eslint-plugin-jest 28.0.0 → 28.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -328,6 +328,7 @@ set to warn in.\
|
|
|
328
328
|
| [prefer-expect-resolves](docs/rules/prefer-expect-resolves.md) | Prefer `await expect(...).resolves` over `expect(await ...)` syntax | | | 🔧 | |
|
|
329
329
|
| [prefer-hooks-in-order](docs/rules/prefer-hooks-in-order.md) | Prefer having hooks in a consistent order | | | | |
|
|
330
330
|
| [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases | | | | |
|
|
331
|
+
| [prefer-importing-jest-globals](docs/rules/prefer-importing-jest-globals.md) | Prefer importing Jest globals | | | 🔧 | |
|
|
331
332
|
| [prefer-lowercase-title](docs/rules/prefer-lowercase-title.md) | Enforce lowercase test names | | | 🔧 | |
|
|
332
333
|
| [prefer-mock-promise-shorthand](docs/rules/prefer-mock-promise-shorthand.md) | Prefer mock resolved/rejected shorthands for promises | | | 🔧 | |
|
|
333
334
|
| [prefer-snapshot-hint](docs/rules/prefer-snapshot-hint.md) | Prefer including a hint with external snapshots | | | | |
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# Prefer importing Jest globals (`prefer-importing-jest-globals`)
|
|
2
|
+
|
|
3
|
+
🔧 This rule is automatically fixable by the
|
|
4
|
+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
|
|
5
|
+
|
|
6
|
+
<!-- end auto-generated rule header -->
|
|
7
|
+
|
|
8
|
+
This rule aims to enforce explicit imports from `@jest/globals`.
|
|
9
|
+
|
|
10
|
+
1. This is useful for ensuring that the Jest APIs are imported the same way in
|
|
11
|
+
the codebase.
|
|
12
|
+
2. When you can't modify Jest's
|
|
13
|
+
[`injectGlobals`](https://jestjs.io/docs/configuration#injectglobals-boolean)
|
|
14
|
+
configuration property, this rule can help to ensure that the Jest globals
|
|
15
|
+
are imported explicitly and facilitate a migration to `@jest/globals`.
|
|
16
|
+
|
|
17
|
+
## Rule details
|
|
18
|
+
|
|
19
|
+
Examples of **incorrect** code for this rule
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
/* eslint jest/prefer-importing-jest-globals: "error" */
|
|
23
|
+
|
|
24
|
+
describe('foo', () => {
|
|
25
|
+
it('accepts this input', () => {
|
|
26
|
+
// ...
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Examples of **correct** code for this rule
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
/* eslint jest/prefer-importing-jest-globals: "error" */
|
|
35
|
+
|
|
36
|
+
import { describe, it } from '@jest/globals';
|
|
37
|
+
|
|
38
|
+
describe('foo', () => {
|
|
39
|
+
it('accepts this input', () => {
|
|
40
|
+
// ...
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Further Reading
|
|
46
|
+
|
|
47
|
+
- [Documentation](https://jestjs.io/docs/api)
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _utils = require("@typescript-eslint/utils");
|
|
8
|
+
var _utils2 = require("./utils");
|
|
9
|
+
const createFixerImports = (isModule, functionsToImport) => {
|
|
10
|
+
const allImportsFormatted = Array.from(functionsToImport).sort().join(', ');
|
|
11
|
+
return isModule ? `import { ${allImportsFormatted} } from '@jest/globals';` : `const { ${allImportsFormatted} } = require('@jest/globals');`;
|
|
12
|
+
};
|
|
13
|
+
var _default = exports.default = (0, _utils2.createRule)({
|
|
14
|
+
name: __filename,
|
|
15
|
+
meta: {
|
|
16
|
+
docs: {
|
|
17
|
+
description: 'Prefer importing Jest globals'
|
|
18
|
+
},
|
|
19
|
+
messages: {
|
|
20
|
+
preferImportingJestGlobal: `Import the following Jest functions from '@jest/globals': {{ jestFunctions }}`
|
|
21
|
+
},
|
|
22
|
+
fixable: 'code',
|
|
23
|
+
type: 'problem',
|
|
24
|
+
schema: []
|
|
25
|
+
},
|
|
26
|
+
defaultOptions: [],
|
|
27
|
+
create(context) {
|
|
28
|
+
const importedFunctionsWithSource = {};
|
|
29
|
+
const functionsToImport = new Set();
|
|
30
|
+
let reportingNode;
|
|
31
|
+
return {
|
|
32
|
+
ImportDeclaration(node) {
|
|
33
|
+
node.specifiers.forEach(specifier => {
|
|
34
|
+
if (specifier.type === 'ImportSpecifier') {
|
|
35
|
+
importedFunctionsWithSource[specifier.local.name] = node.source.value;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
CallExpression(node) {
|
|
40
|
+
const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
|
|
41
|
+
if (!jestFnCall) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (jestFnCall.head.type !== 'import') {
|
|
45
|
+
functionsToImport.add(jestFnCall.name);
|
|
46
|
+
reportingNode ||= jestFnCall.head.node;
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
'Program:exit'() {
|
|
50
|
+
// this means we found at least one function to import
|
|
51
|
+
if (!reportingNode) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const isModule = context.parserOptions.sourceType === 'module';
|
|
55
|
+
context.report({
|
|
56
|
+
node: reportingNode,
|
|
57
|
+
messageId: 'preferImportingJestGlobal',
|
|
58
|
+
data: {
|
|
59
|
+
jestFunctions: Array.from(functionsToImport).join(', ')
|
|
60
|
+
},
|
|
61
|
+
fix(fixer) {
|
|
62
|
+
const sourceCode = (0, _utils2.getSourceCode)(context);
|
|
63
|
+
const [firstNode] = sourceCode.ast.body;
|
|
64
|
+
|
|
65
|
+
// check if "use strict" directive exists
|
|
66
|
+
if (firstNode.type === _utils.AST_NODE_TYPES.ExpressionStatement && (0, _utils2.isStringNode)(firstNode.expression, 'use strict')) {
|
|
67
|
+
return fixer.insertTextAfter(firstNode, `\n${createFixerImports(isModule, functionsToImport)}`);
|
|
68
|
+
}
|
|
69
|
+
const importNode = sourceCode.ast.body.find(node => node.type === _utils.AST_NODE_TYPES.ImportDeclaration && node.source.value === '@jest/globals');
|
|
70
|
+
if (importNode?.type === _utils.AST_NODE_TYPES.ImportDeclaration) {
|
|
71
|
+
for (const specifier of importNode.specifiers) {
|
|
72
|
+
if (specifier.type === _utils.AST_NODE_TYPES.ImportSpecifier && specifier.imported?.name) {
|
|
73
|
+
functionsToImport.add(specifier.imported.name);
|
|
74
|
+
}
|
|
75
|
+
if (specifier.type === _utils.AST_NODE_TYPES.ImportDefaultSpecifier) {
|
|
76
|
+
functionsToImport.add(specifier.local.name);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return fixer.replaceText(importNode, createFixerImports(isModule, functionsToImport));
|
|
80
|
+
}
|
|
81
|
+
const requireNode = sourceCode.ast.body.find(node => node.type === _utils.AST_NODE_TYPES.VariableDeclaration && node.declarations.some(declaration => declaration.init?.type === _utils.AST_NODE_TYPES.CallExpression && (0, _utils2.isIdentifier)(declaration.init.callee, 'require') && (0, _utils2.isStringNode)(declaration.init.arguments[0], '@jest/globals') && (declaration.id.type === _utils.AST_NODE_TYPES.Identifier || declaration.id.type === _utils.AST_NODE_TYPES.ObjectPattern)));
|
|
82
|
+
if (requireNode?.type !== _utils.AST_NODE_TYPES.VariableDeclaration) {
|
|
83
|
+
return fixer.insertTextBefore(reportingNode, `${createFixerImports(isModule, functionsToImport)}\n`);
|
|
84
|
+
}
|
|
85
|
+
if (requireNode.declarations[0]?.id.type === _utils.AST_NODE_TYPES.ObjectPattern) {
|
|
86
|
+
for (const property of requireNode.declarations[0].id.properties) {
|
|
87
|
+
if (property.type === _utils.AST_NODE_TYPES.Property && (0, _utils2.isSupportedAccessor)(property.key)) {
|
|
88
|
+
functionsToImport.add((0, _utils2.getAccessorValue)(property.key));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return fixer.replaceText(requireNode, `${createFixerImports(isModule, functionsToImport)}`);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-jest",
|
|
3
|
-
"version": "28.
|
|
3
|
+
"version": "28.1.0",
|
|
4
4
|
"description": "ESLint rules for Jest",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -130,8 +130,5 @@
|
|
|
130
130
|
},
|
|
131
131
|
"publishConfig": {
|
|
132
132
|
"provenance": true
|
|
133
|
-
},
|
|
134
|
-
"resolutions": {
|
|
135
|
-
"@typescript-eslint/typescript-estree@5.62.0": "patch:@typescript-eslint/typescript-estree@npm:^5.62.0#./.yarn/patches/@typescript-eslint-typescript-estree-npm-5.62.0-5d1ea132a9.patch"
|
|
136
133
|
}
|
|
137
134
|
}
|