-
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.
* Add docs and tests * Add valid-expect rule * Docs * Formatting * Support `expect.soft` * Support `expect.poll` * Add rule by default * Docs
- Loading branch information
Showing
7 changed files
with
267 additions
and
12 deletions.
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,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 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,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 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,4 @@ | ||
import { Rule } from 'eslint'; | ||
import * as ESTree from 'estree'; | ||
|
||
export type NodeWithParent = ESTree.Node & Rule.NodeParentExtension; |
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,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: '' } }, | ||
], | ||
}, | ||
], | ||
}); |