-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #228 from playwright-community/max-expects
feat: Add `max-expects` rule
- Loading branch information
Showing
5 changed files
with
480 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Enforces a maximum number assertion calls in a test body (`max-expects`) | ||
|
||
As more assertions are made, there is a possible tendency for the test to be | ||
more likely to mix multiple objectives. To avoid this, this rule reports when | ||
the maximum number of assertions is exceeded. | ||
|
||
## Rule details | ||
|
||
This rule enforces a maximum number of `expect()` calls. | ||
|
||
The following patterns are considered warnings (with the default option of | ||
`{ "max": 5 } `): | ||
|
||
```js | ||
test('should not pass', () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
``` | ||
|
||
The following patterns are **not** considered warnings (with the default option | ||
of `{ "max": 5 } `): | ||
|
||
```js | ||
test('shout pass'); | ||
|
||
test('shout pass', () => {}); | ||
|
||
test.skip('shout pass', () => {}); | ||
|
||
test('should pass', function () { | ||
expect(true).toBeDefined(); | ||
}); | ||
|
||
test('should pass', () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
``` | ||
|
||
## Options | ||
|
||
```json | ||
{ | ||
"playwright/max-expects": [ | ||
"error", | ||
{ | ||
"max": 5 | ||
} | ||
] | ||
} | ||
``` | ||
|
||
### `max` | ||
|
||
Enforces a maximum number of `expect()`. | ||
|
||
This has a default value of `5`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Rule } from 'eslint'; | ||
import * as ESTree from 'estree'; | ||
import { getExpectType, getParent, isTestCall } from '../utils/ast'; | ||
|
||
export default { | ||
create(context) { | ||
const options = { | ||
max: 5, | ||
...((context.options?.[0] as Record<string, unknown>) ?? {}), | ||
}; | ||
|
||
let count = 0; | ||
|
||
const maybeResetCount = (node: ESTree.Node) => { | ||
const parent = getParent(node); | ||
const isTestFn = | ||
parent?.type !== 'CallExpression' || isTestCall(context, parent); | ||
|
||
if (isTestFn) { | ||
count = 0; | ||
} | ||
}; | ||
|
||
return { | ||
ArrowFunctionExpression: maybeResetCount, | ||
'ArrowFunctionExpression:exit': maybeResetCount, | ||
CallExpression(node) { | ||
if (!getExpectType(context, node)) return; | ||
|
||
count += 1; | ||
|
||
if (count > options.max) { | ||
context.report({ | ||
data: { | ||
count: count.toString(), | ||
max: options.max.toString(), | ||
}, | ||
messageId: 'exceededMaxAssertion', | ||
node, | ||
}); | ||
} | ||
}, | ||
FunctionExpression: maybeResetCount, | ||
'FunctionExpression:exit': maybeResetCount, | ||
}; | ||
}, | ||
meta: { | ||
docs: { | ||
category: 'Best Practices', | ||
description: 'Enforces a maximum number assertion calls in a test body', | ||
recommended: false, | ||
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/max-expects.md', | ||
}, | ||
messages: { | ||
exceededMaxAssertion: | ||
'Too many assertion calls ({{ count }}) - maximum allowed is {{ max }}', | ||
}, | ||
schema: [ | ||
{ | ||
additionalProperties: false, | ||
properties: { | ||
max: { | ||
minimum: 1, | ||
type: 'integer', | ||
}, | ||
}, | ||
type: 'object', | ||
}, | ||
], | ||
type: 'suggestion', | ||
}, | ||
} as Rule.RuleModule; |
Oops, something went wrong.