Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add prefer-to-have-count rule #165

Merged
merged 2 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ command line option.\
| | 🔧 | | [prefer-lowercase-title](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-lowercase-title.md) | Enforce lowercase test names |
| | 🔧 | | [prefer-to-be](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-to-be.md) | Suggest using `toBe()` |
| | 🔧 | | [prefer-to-contain](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-to-contain.md) | Suggest using `toContain()` |
| | 🔧 | | [prefer-to-have-count](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-to-have-count.md) | Suggest using `toHaveCount()` |
| | 🔧 | | [prefer-to-have-length](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-to-have-length.md) | Suggest using `toHaveLength()` |
| ✔ | 🔧 | | [prefer-web-first-assertions](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-web-first-assertions.md) | Suggest using web first assertions |
| | | | [require-top-level-describe](https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/require-top-level-describe.md) | Require test cases and hooks to be inside a `test.describe` block |
Expand Down
23 changes: 23 additions & 0 deletions docs/rules/prefer-to-have-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Suggest using `toHaveCount()` (`prefer-to-have-count`)

In order to have a better failure message, `toHaveCount()` should be used upon
asserting expectations on locators `count()` method.

## Rule details

This rule triggers a warning if `toBe()`, `toEqual()` or `toStrictEqual()` is
used to assert locators `count()` method.

The following patterns are considered warnings:

```javascript
expect(await files.count()).toBe(1);
expect(await files.count()).toEqual(1);
expect(await files.count()).toStrictEqual(1);
```

The following pattern is **not** a warning:

```javascript
await expect(files).toHaveCount(1);
```
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import preferLowercaseTitle from './rules/prefer-lowercase-title';
import preferStrictEqual from './rules/prefer-strict-equal';
import preferToBe from './rules/prefer-to-be';
import preferToContain from './rules/prefer-to-contain';
import preferToHaveCount from './rules/prefer-to-have-count';
import preferToHaveLength from './rules/prefer-to-have-length';
import preferWebFirstAssertions from './rules/prefer-web-first-assertions';
import requireSoftAssertions from './rules/require-soft-assertions';
Expand Down Expand Up @@ -113,6 +114,7 @@ export = {
'prefer-strict-equal': preferStrictEqual,
'prefer-to-be': preferToBe,
'prefer-to-contain': preferToContain,
'prefer-to-have-count': preferToHaveCount,
'prefer-to-have-length': preferToHaveLength,
'prefer-web-first-assertions': preferWebFirstAssertions,
'require-soft-assertions': requireSoftAssertions,
Expand Down
65 changes: 65 additions & 0 deletions src/rules/prefer-to-have-count.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Rule } from 'eslint';
import { replaceAccessorFixer } from '../utils/fixer';
import { parseExpectCall } from '../utils/parseExpectCall';

const matchers = new Set(['toBe', 'toEqual', 'toStrictEqual']);

export default {
create(context) {
return {
CallExpression(node) {
const expectCall = parseExpectCall(node);
if (!expectCall || !matchers.has(expectCall.matcherName)) {
return;
}

const [argument] = node.arguments;
if (
argument.type !== 'AwaitExpression' ||
argument.argument.type !== 'CallExpression' ||
argument.argument.callee.type !== 'MemberExpression'
) {
return;
}

const callee = argument.argument.callee;
context.report({
fix(fixer) {
return [
// remove the "await" expression
fixer.removeRange([
argument.range![0],
argument.range![0] + 'await'.length + 1,
]),
// remove the "count()" method accessor
fixer.removeRange([
callee.property.range![0] - 1,
argument.argument.range![1],
]),
// replace the current matcher with "toHaveCount"
replaceAccessorFixer(fixer, expectCall.matcher, 'toHaveCount'),
// insert "await" to before "expect()"
fixer.insertTextBefore(node, 'await '),
];
},
messageId: 'useToHaveCount',
node: expectCall.matcher,
});
},
};
},
meta: {
docs: {
category: 'Best Practices',
description: 'Suggest using `toHaveCount()`',
recommended: false,
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-to-have-count.md',
},
fixable: 'code',
messages: {
useToHaveCount: 'Use toHaveCount() instead',
},
schema: [],
type: 'suggestion',
},
} as Rule.RuleModule;
66 changes: 66 additions & 0 deletions test/spec/prefer-to-have-count.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import rule from '../../src/rules/prefer-to-have-count';
import { runRuleTester } from '../utils/rule-tester';

runRuleTester('prefer-to-have-count', rule, {
invalid: [
{
code: 'expect(await files.count()).toBe(1)',
errors: [
{ column: 29, endColumn: 33, line: 1, messageId: 'useToHaveCount' },
],
output: 'await expect(files).toHaveCount(1)',
},
{
code: 'expect(await files.count()).not.toBe(1)',
errors: [
{ column: 33, endColumn: 37, line: 1, messageId: 'useToHaveCount' },
],
output: 'await expect(files).not.toHaveCount(1)',
},
{
code: 'expect.soft(await files["count"]()).not.toBe(1)',
errors: [
{ column: 41, endColumn: 45, line: 1, messageId: 'useToHaveCount' },
],
output: 'await expect.soft(files).not.toHaveCount(1)',
},
{
code: 'expect(await files["count"]()).not["toBe"](1)',
errors: [
{ column: 36, endColumn: 42, line: 1, messageId: 'useToHaveCount' },
],
output: 'await expect(files).not["toHaveCount"](1)',
},
{
code: 'expect(await files.count())[`toEqual`](1)',
errors: [
{ column: 29, endColumn: 38, line: 1, messageId: 'useToHaveCount' },
],
output: 'await expect(files)[`toHaveCount`](1)',
},
{
code: 'expect(await files.count()).toStrictEqual(1)',
errors: [
{ column: 29, endColumn: 42, line: 1, messageId: 'useToHaveCount' },
],
output: 'await expect(files).toHaveCount(1)',
},
{
code: 'expect(await files.count()).not.toStrictEqual(1)',
errors: [
{ column: 33, endColumn: 46, line: 1, messageId: 'useToHaveCount' },
],
output: 'await expect(files).not.toHaveCount(1)',
},
],
valid: [
'await expect(files).toHaveCount(1)',
"expect(files.name).toBe('file')",
"expect(files['name']).toBe('file')",
"expect(files[`name`]).toBe('file')",
'expect(result).toBe(true)',
`expect(user.getUserName(5)).not.toEqual('Paul')`,
`expect(user.getUserName(5)).not.toEqual('Paul')`,
'expect(a)',
],
});
Loading