-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add eslint rule
valid-expect
(#3067)
* Add eslint rule `valid-expect` All credits goes to @tlvince and @alecxe * Update valid-expect.md * Update valid-expect-test.js * Update valid-expect-test.js * Update valid-expect.js * Minor fixes and cleanups. * Fix lint errors.
- Loading branch information
Showing
10 changed files
with
220 additions
and
21 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
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
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,41 @@ | ||
# Enforce valid `expect()` usage (valid-expect) | ||
|
||
Ensure `expect()` is called with a single argument and there is an actual expectation made. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if `expect()` is called with more than one argument or without arguments. | ||
It would also issue a warning if there is nothing called on `expect()`, e.g.: | ||
|
||
```js | ||
expect(); | ||
expect('something'); | ||
``` | ||
|
||
or when a matcher function was not called, e.g.: | ||
|
||
```js | ||
expect(true).toBeDefined | ||
``` | ||
|
||
This rule is enabled by default. | ||
|
||
### Default configuration | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
expect(); | ||
expect().toEqual('something'); | ||
expect('something', 'else'); | ||
expect('something'); | ||
expect(true).toBeDefined; | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
expect('something').toEqual('something'); | ||
expect([1, 2, 3]).toEqual([1, 2, 3]); | ||
expect(true).toBeDefined(); | ||
``` |
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
81 changes: 81 additions & 0 deletions
81
packages/eslint-plugin-jest/src/rules/__tests__/valid-expect-test.js
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,81 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @flow | ||
*/ | ||
|
||
/* eslint-disable sort-keys */ | ||
|
||
'use strict'; | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rules = require('../../').rules; | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('valid-expect', rules['valid-expect'], { | ||
valid: [ | ||
'expect("something").toEqual("else");', | ||
'expect(true).toBeDefined();', | ||
'expect([1, 2, 3]).toEqual([1, 2, 3]);', | ||
'expect(undefined).not.toBeDefined();', | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'expect().toBe(true);', | ||
errors: [ | ||
{ | ||
message: 'No arguments were passed to expect().', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'expect().toEqual("something");', | ||
errors: [ | ||
{ | ||
message: 'No arguments were passed to expect().', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'expect("something", "else").toEqual("something");', | ||
errors: [ | ||
{ | ||
message: 'More than one argument was passed to expect().', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'expect("something");', | ||
errors: [ | ||
{ | ||
message: 'No assertion was called on expect().', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'expect();', | ||
errors: [ | ||
{ | ||
message: 'No arguments were passed to expect().', | ||
}, | ||
{ | ||
message: 'No assertion was called on expect().', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'expect(true).toBeDefined;', | ||
errors: [ | ||
{ | ||
message: '"toBeDefined" was not called.', | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
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,61 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
/* | ||
* This implementation is ported from from eslint-plugin-jasmine. | ||
* MIT license, Tom Vincent. | ||
*/ | ||
|
||
import type {EslintContext, CallExpression} from './types'; | ||
|
||
module.exports = (context: EslintContext) => { | ||
return { | ||
CallExpression(node: CallExpression) { | ||
if (node.callee.name === 'expect') { | ||
// checking "expect()" arguments | ||
if (node.arguments.length > 1) { | ||
context.report({ | ||
message: 'More than one argument was passed to expect().', | ||
node, | ||
}); | ||
} else if (node.arguments.length === 0) { | ||
context.report({ | ||
message: 'No arguments were passed to expect().', | ||
node, | ||
}); | ||
} | ||
|
||
// matcher was not called | ||
if ( | ||
node.parent && | ||
node.parent.type === 'MemberExpression' && | ||
node.parent.parent && | ||
node.parent.parent.type === 'ExpressionStatement' | ||
) { | ||
context.report({ | ||
message: `"${node.parent.property.name}" was not called.`, | ||
node, | ||
}); | ||
} | ||
} | ||
}, | ||
|
||
// nothing called on "expect()" | ||
'CallExpression:exit'(node: CallExpression) { | ||
if ( | ||
node.callee.name === 'expect' && | ||
node.parent.type === 'ExpressionStatement' | ||
) { | ||
context.report({message: 'No assertion was called on expect().', node}); | ||
} | ||
}, | ||
}; | ||
}; |
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