Skip to content

Commit

Permalink
Fix bug with .not in tests for no-mixed-expectation-groups
Browse files Browse the repository at this point in the history
  • Loading branch information
maks-rafalko committed Jul 6, 2024
1 parent 8ae388f commit f6e0324
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 23 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ To enable this configuration with `.eslintrc`, use the `extends` property:
💼 Configurations enabled in.\
✅ Set in the `recommended` configuration.

| Name                             | Description | 💼 |
| :--------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------- | :- |
| [no-long-arrays-in-test-each](docs/rules/no-long-arrays-in-test-each.md) | Disallow mixing expectations for different variables between each other. ||
| [no-mixed-expectation-groups](docs/rules/no-mixed-expectation-groups.md) | Disallow mixing expectations for different variables between each other. ||
| [no-useless-matcher-to-be-defined](docs/rules/no-useless-matcher-to-be-defined.md) | Disallow using `.toBeDefined()` matcher when it is known that variable is always defined. ||
| [no-useless-matcher-to-be-null](docs/rules/no-useless-matcher-to-be-null.md) | Disallow using `.toBeNull()` when TypeScript types conflict with it. ||
| Name                             | Description | 💼 |
| :--------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------- | :- |
| [no-long-arrays-in-test-each](docs/rules/no-long-arrays-in-test-each.md) | Disallow using long arrays with objects inside `test.each()` or `it.each()`. Force moving them out of the file. ||
| [no-mixed-expectation-groups](docs/rules/no-mixed-expectation-groups.md) | Disallow mixing expectations for different variables between each other. ||
| [no-useless-matcher-to-be-defined](docs/rules/no-useless-matcher-to-be-defined.md) | Disallow using `.toBeDefined()` matcher when it is known that variable is always defined. ||
| [no-useless-matcher-to-be-null](docs/rules/no-useless-matcher-to-be-null.md) | Disallow using `.toBeNull()` when TypeScript types conflict with it. ||

<!-- end auto-generated rules list -->

Expand Down
12 changes: 11 additions & 1 deletion docs/rules/no-long-arrays-in-test-each.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
# Disallow mixing expectations for different variables between each other (`proper-tests/no-long-arrays-in-test-each`)
# Disallow using long arrays with objects inside `test.each()` or `it.each()`. Force moving them out of the file (`proper-tests/no-long-arrays-in-test-each`)

💼 This rule is enabled in the ✅ `recommended` config.

<!-- end auto-generated rule header -->

## Options

<!-- begin auto-generated rule options list -->

| Name | Type |
| :------ | :------ |
| `limit` | Integer |

<!-- end auto-generated rule options list -->

## Rule details

This rule disallows mixing expectations for different variables between each other.
Expand Down
19 changes: 19 additions & 0 deletions src/custom-rules/no-mixed-expectation-groups.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,25 @@ ruleTester.run('no-mixed-expectation-groups', noMixedExpectationGroups, {
},
],
},
{
name: 'when expectations for variable1 are mixed with variable2 in one "test", with "not"',
code: `
test('should fail1', function () {
expect(variable1).toBeDefined();
expect(variable2).toEqual({});
expect(variable1).not.toEqual({});
});
`,
output: null,
errors: [
{
messageId: 'noMixedExpectationGroups',
data: {
variable: 'variable1',
},
},
],
},
{
name: 'when expectations for objects variable1.property1 are mixed with variable2 in one "test"',
code: `
Expand Down
17 changes: 4 additions & 13 deletions src/custom-rules/no-mixed-expectation-groups.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AST_NODE_TYPES, ESLintUtils, TSESTree } from '@typescript-eslint/utils';

import { getExpectCallExpression } from './utils/get-expect-call-expression';
import { isParentATestFunction } from './utils/is-parent-a-test-function';

type MessageIds = 'noMixedExpectationGroups';
Expand All @@ -24,23 +25,13 @@ export const noMixedExpectationGroups = ESLintUtils.RuleCreator.withoutDocs<Opti
ArrowFunctionExpression: resetMatchersIfIsTestFunction,
'ArrowFunctionExpression:exit': resetMatchersIfIsTestFunction,
CallExpression(node) {
if (node.callee.type !== AST_NODE_TYPES.MemberExpression) {
return;
}
const expectCallIdentifier = getExpectCallExpression(node);

if (node.callee.property.type !== AST_NODE_TYPES.Identifier) {
return;
}

if (
node.callee.object.type !== AST_NODE_TYPES.CallExpression ||
node.callee.object.callee.type !== AST_NODE_TYPES.Identifier ||
node.callee.object.callee.name !== 'expect'
) {
if (expectCallIdentifier === null) {
return;
}

const expectFirstArgument = node.callee.object.arguments[0];
const expectFirstArgument = expectCallIdentifier.arguments[0];

let variableName = '';

Expand Down
2 changes: 1 addition & 1 deletion src/custom-rules/no-useless-matcher-to-be-defined.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils';
import * as ts from 'typescript';

import { isTypeFlagSet } from './utils/isTypeFlagSet';
import { isTypeFlagSet } from './utils/is-type-flag-set';

type MessageIds = 'noUselessMatchers';
type Options = [];
Expand Down
2 changes: 1 addition & 1 deletion src/custom-rules/no-useless-matcher-to-be-null.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ParserServicesWithTypeInformation } from '@typescript-eslint/types
import { AST_NODE_TYPES, ESLintUtils, TSESLint, TSESTree } from '@typescript-eslint/utils';
import * as ts from 'typescript';

import { isTypeFlagSet } from './utils/isTypeFlagSet';
import { isTypeFlagSet } from './utils/is-type-flag-set';

type MessageIds = 'noUselessMatchers';
type Options = [];
Expand Down
32 changes: 32 additions & 0 deletions src/custom-rules/utils/get-expect-call-expression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';

export const getExpectCallExpression = (node: TSESTree.CallExpression): TSESTree.CallExpression | null => {
if (node.callee.type !== AST_NODE_TYPES.MemberExpression) {
return null;
}

if (node.callee.property.type !== AST_NODE_TYPES.Identifier) {
return null;
}

// case like expect(x).toBe(Y);
if (
node.callee.object.type === AST_NODE_TYPES.CallExpression &&
node.callee.object.callee.type === AST_NODE_TYPES.Identifier &&
node.callee.object.callee.name === 'expect'
) {
return node.callee.object;
}

// case like expect(x).not.toBe(Y);
if (
node.callee.object.type === AST_NODE_TYPES.MemberExpression &&
node.callee.object.object.type === AST_NODE_TYPES.CallExpression &&
node.callee.object.object.callee.type === AST_NODE_TYPES.Identifier &&
node.callee.object.object.callee.name === 'expect'
) {
return node.callee.object.object;
}

return null;
};
File renamed without changes.
3 changes: 2 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Linter } from '@typescript-eslint/utils/ts-eslint';

import recommended from './configs/recommended';
import { noLongArraysInTestEach } from './custom-rules/no-long-arrays-in-test-each';
import { noMixedExpectationGroups } from './custom-rules/no-mixed-expectation-groups';
import { noUselessMatcherToBeDefined } from './custom-rules/no-useless-matcher-to-be-defined';
import { noUselessMatcherToBeNull } from './custom-rules/no-useless-matcher-to-be-null';
Expand All @@ -13,6 +14,6 @@ export = {
'no-useless-matcher-to-be-defined': noUselessMatcherToBeDefined,
'no-useless-matcher-to-be-null': noUselessMatcherToBeNull,
'no-mixed-expectation-groups': noMixedExpectationGroups,
'no-long-arrays-in-test-each': noMixedExpectationGroups,
'no-long-arrays-in-test-each': noLongArraysInTestEach,
},
} satisfies Linter.Plugin;

0 comments on commit f6e0324

Please sign in to comment.