eslint-plugin-jest 28.1.1 → 28.3.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
@@ -87,6 +87,27 @@ You can tell this plugin about any global Jests you have aliased using the
87
87
  }
88
88
  ```
89
89
 
90
+ #### Aliased `@jest/globals`
91
+
92
+ You can tell this plugin to treat a different package as the source of Jest
93
+ globals using the `globalPackage` setting:
94
+
95
+ ```json
96
+ {
97
+ "settings": {
98
+ "jest": {
99
+ "globalPackage": "bun:test"
100
+ }
101
+ }
102
+ }
103
+ ```
104
+
105
+ > [!WARNING]
106
+ >
107
+ > While this can be used to apply rules when using alternative testing libraries
108
+ > and frameworks like `bun`, `vitest` and `node`, there's no guarantee the
109
+ > semantics this plugin assumes will hold outside of Jest
110
+
90
111
  ### Running rules only on test-related files
91
112
 
92
113
  The rules provided by this plugin assume that the files they are checking are
@@ -42,6 +42,42 @@ describe('foo', () => {
42
42
  });
43
43
  ```
44
44
 
45
+ ## Options
46
+
47
+ This rule can be configured as follows
48
+
49
+ ```json
50
+ {
51
+ "type": "object",
52
+ "properties": {
53
+ "types": {
54
+ "type": "array",
55
+ "items": {
56
+ "type": "string",
57
+ "enum": ["hook", "describe", "test", "expect", "jest", "unknown"]
58
+ }
59
+ }
60
+ },
61
+ "additionalProperties": false
62
+ }
63
+ ```
64
+
65
+ #### types
66
+
67
+ A list of Jest global types to enforce explicit imports for. By default, all
68
+ Jest globals are enforced.
69
+
70
+ This option is useful when you only want to enforce explicit imports for a
71
+ subset of Jest globals. For instance, when migrating to ESM, you might want to
72
+ enforce explicit imports only for the `jest` global, as of
73
+ [Jest's ESM documentation](https://jestjs.io/docs/ecmascript-modules#differences-between-esm-and-commonjs).
74
+
75
+ ```json5
76
+ {
77
+ 'jest/prefer-importing-jest-globals': ['error', { types: ['jest'] }],
78
+ }
79
+ ```
80
+
45
81
  ## Further Reading
46
82
 
47
83
  - [Documentation](https://jestjs.io/docs/api)
@@ -40,9 +40,9 @@ var _default = exports.default = (0, _utils2.createRule)({
40
40
  properties: {
41
41
  assertFunctionNames: {
42
42
  type: 'array',
43
- items: [{
43
+ items: {
44
44
  type: 'string'
45
- }]
45
+ }
46
46
  },
47
47
  additionalTestBlockFunctions: {
48
48
  type: 'array',
@@ -10,6 +10,7 @@ const createFixerImports = (isModule, functionsToImport) => {
10
10
  const allImportsFormatted = Array.from(functionsToImport).sort().join(', ');
11
11
  return isModule ? `import { ${allImportsFormatted} } from '@jest/globals';` : `const { ${allImportsFormatted} } = require('@jest/globals');`;
12
12
  };
13
+ const allJestFnTypes = ['hook', 'describe', 'test', 'expect', 'jest', 'unknown'];
13
14
  var _default = exports.default = (0, _utils2.createRule)({
14
15
  name: __filename,
15
16
  meta: {
@@ -21,10 +22,27 @@ var _default = exports.default = (0, _utils2.createRule)({
21
22
  },
22
23
  fixable: 'code',
23
24
  type: 'problem',
24
- schema: []
25
+ schema: [{
26
+ type: 'object',
27
+ properties: {
28
+ types: {
29
+ type: 'array',
30
+ items: {
31
+ type: 'string',
32
+ enum: allJestFnTypes
33
+ }
34
+ }
35
+ },
36
+ additionalProperties: false
37
+ }]
25
38
  },
26
- defaultOptions: [],
39
+ defaultOptions: [{
40
+ types: allJestFnTypes
41
+ }],
27
42
  create(context) {
43
+ const {
44
+ types = allJestFnTypes
45
+ } = context.options[0] || {};
28
46
  const importedFunctionsWithSource = {};
29
47
  const functionsToImport = new Set();
30
48
  let reportingNode;
@@ -41,7 +59,7 @@ var _default = exports.default = (0, _utils2.createRule)({
41
59
  if (!jestFnCall) {
42
60
  return;
43
61
  }
44
- if (jestFnCall.head.type !== 'import') {
62
+ if (jestFnCall.head.type !== 'import' && types.includes(jestFnCall.type)) {
45
63
  functionsToImport.add(jestFnCall.name);
46
64
  reportingNode ||= jestFnCall.head.node;
47
65
  }
@@ -309,9 +309,10 @@ const resolveToJestFn = (context, accessor) => {
309
309
  return null;
310
310
  }
311
311
  if (maybeImport) {
312
- // the identifier is imported from @jest/globals,
313
- // so return the original import name
314
- if (maybeImport.source === '@jest/globals') {
312
+ const globalPackage = context.settings.jest?.globalPackage ?? '@jest/globals';
313
+
314
+ // the identifier is imported from our global package so return the original import name
315
+ if (maybeImport.source === globalPackage) {
315
316
  return {
316
317
  original: maybeImport.imported,
317
318
  local: maybeImport.local,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest",
3
- "version": "28.1.1",
3
+ "version": "28.3.0",
4
4
  "description": "ESLint rules for Jest",
5
5
  "keywords": [
6
6
  "eslint",
@@ -29,7 +29,7 @@
29
29
  "prettier:write": "prettier --write 'docs/**/*.md' README.md '.github/**' package.json tsconfig.json src/globals.json .yarnrc.yml",
30
30
  "postpublish": "pinst --enable",
31
31
  "test": "jest",
32
- "tools:regenerate-docs": "yarn prepack && eslint-doc-generator",
32
+ "regenerate-docs": "yarn prepack && eslint-doc-generator",
33
33
  "typecheck": "tsc -p ."
34
34
  },
35
35
  "commitlint": {
@@ -86,13 +86,13 @@
86
86
  "babel-jest": "^29.0.0",
87
87
  "babel-plugin-replace-ts-export-assignment": "^0.0.2",
88
88
  "dedent": "^1.5.0",
89
- "eslint": "^7.0.0 || ^8.0.0 || ^9.0.0",
89
+ "eslint": "^7.0.0 || ^8.0.0",
90
90
  "eslint-config-prettier": "^9.0.0",
91
91
  "eslint-doc-generator": "^1.0.0",
92
92
  "eslint-plugin-eslint-comments": "^3.1.2",
93
- "eslint-plugin-eslint-plugin": "^5.0.6",
93
+ "eslint-plugin-eslint-plugin": "^6.0.0",
94
94
  "eslint-plugin-import": "^2.25.1",
95
- "eslint-plugin-n": "^15.0.0",
95
+ "eslint-plugin-n": "^17.0.0",
96
96
  "eslint-plugin-prettier": "^5.0.0",
97
97
  "eslint-remote-tester": "^3.0.0",
98
98
  "eslint-remote-tester-repositories": "~1.0.0",