eslint-plugin-jest 29.5.0 → 29.6.1

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
@@ -394,9 +394,10 @@ Manually fixable by
394
394
 
395
395
  ### Requires Type Checking
396
396
 
397
- | Name           | Description | 💼 | ⚠️ | 🔧 | 💡 |
398
- | :--------------------------------------------- | :----------------------------------------------------------- | :-- | :-- | :-- | :-- |
399
- | [unbound-method](docs/rules/unbound-method.md) | Enforce unbound methods are called with their expected scope | | | | |
397
+ | Name                     | Description | 💼 | ⚠️ | 🔧 | 💡 |
398
+ | :----------------------------------------------------------------- | :----------------------------------------------------------- | :-- | :-- | :-- | :-- |
399
+ | [no-unnecessary-assertion](docs/rules/no-unnecessary-assertion.md) | Disallow unnecessary assertions based on types | | | | |
400
+ | [unbound-method](docs/rules/unbound-method.md) | Enforce unbound methods are called with their expected scope | | | | |
400
401
 
401
402
  <!-- end auto-generated rules list -->
402
403
 
@@ -0,0 +1,35 @@
1
+ # Disallow unnecessary assertions based on types (`no-unnecessary-assertion`)
2
+
3
+ 💭 This rule requires
4
+ [type information](https://typescript-eslint.io/linting/typed-linting).
5
+
6
+ <!-- end auto-generated rule header -->
7
+
8
+ When using TypeScript, runtime assertions based on types can often be omitted
9
+ provided that the types are accurate.
10
+
11
+ ## Rule details
12
+
13
+ This rule warns when you do an assertion about being `null` or `undefined` on
14
+ something that cannot be those types, as that indicates either the assertion can
15
+ be removed or the types need to be adjusted.
16
+
17
+ The following patterns are considered warnings:
18
+
19
+ ```ts
20
+ expect('hello world'.match('sunshine') ?? []).toBeNull();
21
+
22
+ expect(User.findOrThrow(1)).toBeDefined();
23
+
24
+ expect(map.getOrInsert('key', 'default')).not.toBeUndefined();
25
+ ```
26
+
27
+ The following patterns are not considered warnings:
28
+
29
+ ```ts
30
+ expect('hello world'.match('sunshine')).toBeNull();
31
+
32
+ expect(User.findOrNull(1)).toBeDefined();
33
+
34
+ expect(map.get('key')).not.toBeUndefined();
35
+ ```
@@ -0,0 +1,83 @@
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 canBe = (firstArgumentType, flag) => {
10
+ if (firstArgumentType.isUnion()) {
11
+ return firstArgumentType.types.some(typ => typ.flags & flag);
12
+ }
13
+ return firstArgumentType.flags & flag;
14
+ };
15
+ var _default = exports.default = (0, _utils2.createRule)({
16
+ name: __filename,
17
+ meta: {
18
+ docs: {
19
+ description: 'Disallow unnecessary assertions based on types',
20
+ requiresTypeChecking: true
21
+ },
22
+ messages: {
23
+ unnecessaryAssertion: 'Unnecessary assertion, subject cannot be {{ thing }}',
24
+ noStrictNullCheck: 'This rule requires the `strictNullChecks` compiler option to be turned on to function correctly.'
25
+ },
26
+ type: 'suggestion',
27
+ schema: []
28
+ },
29
+ defaultOptions: [],
30
+ create(context) {
31
+ const services = _utils.ESLintUtils.getParserServices(context);
32
+ const compilerOptions = services.program.getCompilerOptions();
33
+ const isStrictNullChecks = compilerOptions.strictNullChecks || compilerOptions.strict && compilerOptions.strictNullChecks !== false;
34
+ if (!isStrictNullChecks) {
35
+ context.report({
36
+ loc: {
37
+ start: {
38
+ column: 0,
39
+ line: 0
40
+ },
41
+ end: {
42
+ column: 0,
43
+ line: 0
44
+ }
45
+ },
46
+ messageId: 'noStrictNullCheck'
47
+ });
48
+ }
49
+
50
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
51
+ const {
52
+ TypeFlags
53
+ } = require('typescript');
54
+ return {
55
+ CallExpression(node) {
56
+ const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
57
+ if (jestFnCall?.type !== 'expect' || jestFnCall.head.node.parent.type !== _utils.AST_NODE_TYPES.CallExpression) {
58
+ return;
59
+ }
60
+ const matcherName = (0, _utils2.getAccessorValue)(jestFnCall.matcher);
61
+ if (!['toBeNull', 'toBeDefined', 'toBeUndefined'].includes(matcherName)) {
62
+ return;
63
+ }
64
+
65
+ // todo: we should support resolving promise types
66
+ if (jestFnCall.modifiers.some(nod => (0, _utils2.getAccessorValue)(nod) !== 'not')) {
67
+ return;
68
+ }
69
+ const [argument] = jestFnCall.head.node.parent.arguments;
70
+ const isNullable = canBe(services.getTypeAtLocation(argument), matcherName === 'toBeNull' ? TypeFlags.Null : TypeFlags.Undefined);
71
+ if (!isNullable) {
72
+ context.report({
73
+ messageId: 'unnecessaryAssertion',
74
+ data: {
75
+ thing: matcherName === 'toBeNull' ? 'null' : 'undefined'
76
+ },
77
+ node
78
+ });
79
+ }
80
+ }
81
+ };
82
+ }
83
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest",
3
- "version": "29.5.0",
3
+ "version": "29.6.1",
4
4
  "description": "ESLint rules for Jest",
5
5
  "keywords": [
6
6
  "eslint",