-
Notifications
You must be signed in to change notification settings - Fork 43
Valid expect #82
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
Merged
Merged
Valid expect #82
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b49bd51
Add docs and tests
mskelton 5d9e11c
Add valid-expect rule
mskelton ef20793
Docs
mskelton fbe230c
Formatting
mskelton 9b824f8
Support `expect.soft`
mskelton bfe27ab
Support `expect.poll`
mskelton d1dbd1d
Merge branch 'main' into valid-expect
mskelton 4e8c83b
Add rule by default
mskelton f5a5d6b
Docs
mskelton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,39 @@ | ||
# Enforce valid `expect()` usage (`valid-expect`) | ||
|
||
Ensure `expect()` is called with a matcher. | ||
|
||
## Rule details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```javascript | ||
expect(); | ||
expect('something'); | ||
expect(true).toBeDefined; | ||
``` | ||
|
||
Example of **correct** code for this rule: | ||
|
||
```javascript | ||
expect(locator).toHaveText('howdy'); | ||
expect('something').toBe('something'); | ||
expect(true).toBeDefined(); | ||
``` | ||
|
||
## Options | ||
|
||
```json | ||
{ | ||
"minArgs": 1, | ||
"maxArgs": 2 | ||
} | ||
``` | ||
|
||
### `minArgs` & `maxArgs` | ||
|
||
Enforces the minimum and maximum number of arguments that `expect` can take, and | ||
is required to take. | ||
|
||
`minArgs` defaults to 1 while `maxArgs` deafults to `2` to support custom expect | ||
messages. If you want to enforce `expect` always or never has a custom message, | ||
you can adjust these two option values to your preference. |
This file contains hidden or 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 hidden or 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,103 @@ | ||
import { Rule } from 'eslint'; | ||
import { isExpectCall, isIdentifier } from '../utils/ast'; | ||
import { NodeWithParent } from '../utils/types'; | ||
|
||
function isMatcherFound(node: NodeWithParent): boolean { | ||
if (node.parent.type !== 'MemberExpression') { | ||
return false; | ||
} | ||
|
||
return !( | ||
isIdentifier(node.parent.property, 'not') && | ||
node.parent.parent.type !== 'MemberExpression' | ||
); | ||
} | ||
|
||
function isMatcherCalled(node: NodeWithParent): boolean { | ||
return node.parent.type === 'MemberExpression' | ||
? // If the parent is a member expression, we continue traversing upward to | ||
// handle matcher chains of unknown length. e.g. expect().not.something. | ||
isMatcherCalled(node.parent) | ||
: // Just asserting that the parent is a call expression is not enough as | ||
// the node could be an argument of a call expression which doesn't | ||
// determine if it is called. To determine if it is called, we verify | ||
// that the parent call expression callee is the same as the node. | ||
node.parent.type === 'CallExpression' && node.parent.callee === node; | ||
} | ||
|
||
const getAmountData = (amount: number) => ({ | ||
amount: amount.toString(), | ||
s: amount === 1 ? '' : 's', | ||
}); | ||
|
||
export default { | ||
create(context) { | ||
const options = { | ||
minArgs: 1, | ||
maxArgs: 2, | ||
...((context.options?.[0] as {}) ?? {}), | ||
}; | ||
|
||
const minArgs = Math.min(options.minArgs, options.maxArgs); | ||
const maxArgs = Math.max(options.minArgs, options.maxArgs); | ||
|
||
return { | ||
CallExpression(node) { | ||
if (!isExpectCall(node)) return; | ||
|
||
if (!isMatcherFound(node)) { | ||
context.report({ node, messageId: 'matcherNotFound' }); | ||
} else if (!isMatcherCalled(node)) { | ||
context.report({ node, messageId: 'matcherNotCalled' }); | ||
} | ||
|
||
if (node.arguments.length < minArgs) { | ||
context.report({ | ||
messageId: 'notEnoughArgs', | ||
data: getAmountData(minArgs), | ||
node, | ||
}); | ||
} | ||
|
||
if (node.arguments.length > maxArgs) { | ||
context.report({ | ||
messageId: 'tooManyArgs', | ||
data: getAmountData(maxArgs), | ||
node, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
meta: { | ||
docs: { | ||
category: 'Possible Errors', | ||
description: 'Enforce valid `expect()` usage', | ||
recommended: true, | ||
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/valid-expect.md', | ||
}, | ||
messages: { | ||
tooManyArgs: 'Expect takes at most {{amount}} argument{{s}}.', | ||
notEnoughArgs: 'Expect requires at least {{amount}} argument{{s}}.', | ||
matcherNotFound: 'Expect must have a corresponding matcher call.', | ||
matcherNotCalled: 'Matchers must be called to assert.', | ||
}, | ||
type: 'problem', | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
minArgs: { | ||
type: 'number', | ||
minimum: 1, | ||
}, | ||
maxArgs: { | ||
type: 'number', | ||
minimum: 1, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
}, | ||
} as Rule.RuleModule; |
This file contains hidden or 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 hidden or 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,4 @@ | ||
import { Rule } from 'eslint'; | ||
import * as ESTree from 'estree'; | ||
|
||
export type NodeWithParent = ESTree.Node & Rule.NodeParentExtension; |
This file contains hidden or 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,93 @@ | ||
import { runRuleTester } from '../utils/rule-tester'; | ||
import rule from '../../src/rules/valid-expect'; | ||
|
||
const invalid = (code: string, messageId: string) => ({ | ||
code, | ||
errors: [{ messageId }], | ||
}); | ||
|
||
runRuleTester('valid-expect', rule, { | ||
valid: [ | ||
'expect("something").toBe("else")', | ||
'expect.soft("something").toBe("else")', | ||
'expect.poll(() => "something").toBe("else")', | ||
'expect(true).toBeDefined()', | ||
'expect(undefined).not.toBeDefined()', | ||
'expect([1, 2, 3]).toEqual([1, 2, 3])', | ||
'expect(1, "1 !== 2").toBe(2)', | ||
'expect.soft(1, "1 !== 2").toBe(2)', | ||
'expect.poll(() => 1, { message: "1 !== 2" }).toBe(2)', | ||
// minArgs | ||
{ | ||
code: 'expect(1, "1 !== 2").toBe(2)', | ||
options: [{ minArgs: 2 }], | ||
}, | ||
{ | ||
code: 'expect(1, 2, 3).toBe(4)', | ||
options: [{ minArgs: 3 }], | ||
}, | ||
// maxArgs | ||
{ | ||
code: 'expect(1).toBe(2)', | ||
options: [{ maxArgs: 1 }], | ||
}, | ||
{ | ||
code: 'expect(1, 2, 3).toBe(4)', | ||
options: [{ maxArgs: 3 }], | ||
}, | ||
], | ||
invalid: [ | ||
// Matcher not found | ||
invalid('expect(foo)', 'matcherNotFound'), | ||
invalid('expect(foo).not', 'matcherNotFound'), | ||
invalid('expect.soft(foo)', 'matcherNotFound'), | ||
invalid('expect.soft(foo).not', 'matcherNotFound'), | ||
invalid('expect.poll(foo)', 'matcherNotFound'), | ||
invalid('expect.poll(foo).not', 'matcherNotFound'), | ||
// Matcher not called | ||
invalid('expect(foo).toBe', 'matcherNotCalled'), | ||
invalid('expect(foo).not.toBe', 'matcherNotCalled'), | ||
invalid('something(expect(foo).not.toBe)', 'matcherNotCalled'), | ||
invalid('expect.soft(foo).toBe', 'matcherNotCalled'), | ||
invalid('expect.soft(foo).not.toBe', 'matcherNotCalled'), | ||
invalid('something(expect.soft(foo).not.toBe)', 'matcherNotCalled'), | ||
invalid('expect.poll(() => foo).toBe', 'matcherNotCalled'), | ||
invalid('expect.poll(() => foo).not.toBe', 'matcherNotCalled'), | ||
invalid('something(expect.poll(() => foo).not.toBe)', 'matcherNotCalled'), | ||
// minArgs | ||
{ | ||
code: 'expect().toBe(true)', | ||
errors: [{ messageId: 'notEnoughArgs', data: { amount: 1, s: '' } }], | ||
}, | ||
{ | ||
code: 'expect(foo).toBe(true)', | ||
options: [{ minArgs: 2 }], | ||
errors: [{ messageId: 'notEnoughArgs', data: { amount: 2, s: 's' } }], | ||
}, | ||
// maxArgs | ||
{ | ||
code: 'expect(foo, "bar", "baz").toBe(true)', | ||
errors: [{ messageId: 'tooManyArgs', data: { amount: 2, s: 's' } }], | ||
}, | ||
{ | ||
code: 'expect(foo, "bar").toBe(true)', | ||
options: [{ maxArgs: 1 }], | ||
errors: [{ messageId: 'tooManyArgs', data: { amount: 1, s: '' } }], | ||
}, | ||
// Multiple errors | ||
{ | ||
code: 'expect()', | ||
errors: [ | ||
{ messageId: 'matcherNotFound' }, | ||
{ messageId: 'notEnoughArgs', data: { amount: 1, s: '' } }, | ||
], | ||
}, | ||
{ | ||
code: 'expect().toHaveText', | ||
errors: [ | ||
{ messageId: 'matcherNotCalled' }, | ||
{ messageId: 'notEnoughArgs', data: { amount: 1, s: '' } }, | ||
], | ||
}, | ||
], | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!