Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dab6c1e

Browse files
committedMar 5, 2017
Add eslint rule valid-expect
All credits goes to @tlvince and @alecxe
1 parent 5ab8e28 commit dab6c1e

File tree

8 files changed

+197
-2
lines changed

8 files changed

+197
-2
lines changed
 

‎packages/eslint-config-fb-strict/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ module.exports = Object.assign({}, fbjsConfig, {
4141
'flowtype/object-type-delimiter': [2, 'comma'],
4242
'jest/no-focused-tests': [2],
4343
'jest/no-identical-title': [2],
44+
'jest/valid-expect': [2],
4445
'max-len': [2, {
4546
'code': 80,
4647
'ignorePattern': maxLenIgnorePattern,

‎packages/eslint-plugin-jest/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Then configure the rules you want to use under the rules section.
3131
"jest/no-disabled-tests": "warn",
3232
"jest/no-focused-tests": "error",
3333
"jest/no-identical-title": "error",
34+
"jest/valid-expect": "error",
3435
}
3536
}
3637
```
@@ -50,6 +51,7 @@ You can also whitelist the environment variables provided by Jest by doing:
5051
- [no-disabled-tests](/packages/eslint-plugin-jest/docs/rules/no-disabled-tests.md) - disallow disabled tests.
5152
- [no-focused-tests](/packages/eslint-plugin-jest/docs/rules/no-focused-tests.md) - disallow focused tests.
5253
- [no-identical-title](/packages/eslint-plugin-jest/docs/rules/no-identical-title.md) - disallow identical titles.
54+
- [valid-expect](/packages/eslint-plugin-jest/docs/rules/valid-expect.md) - disallow identical titles.
5355

5456
## Shareable configurations
5557

@@ -72,6 +74,7 @@ The rules enabled in this configuration are:
7274
- [jest/no-disabled-tests](/packages/eslint-plugin-jest/docs/rules/no-disabled-tests.md)
7375
- [jest/no-focused-tests](/packages/eslint-plugin-jest/docs/rules/no-focused-tests.md)
7476
- [jest/no-identical-title](/packages/eslint-plugin-jest/docs/rules/no-identical-title.md)
77+
- [jest/valid-expect](/packages/eslint-plugin-jest/docs/rules/valid-expect.md)
7578

7679
## Credit
7780

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Enforce valid `expect()` usage (valid-expect)
2+
3+
Ensure `expect()` is called with a single argument and there is an actual expectation made.
4+
5+
## Rule details
6+
7+
This rule triggers a warning if `expect()` is called with more than one argument or without arguments.
8+
It would also issue a warning if there is nothing called on `expect()`, e.g.:
9+
10+
```js
11+
expect();
12+
expect("something");
13+
```
14+
15+
or when a matcher function was not called, e.g.:
16+
17+
```js
18+
expect(true).toBeDefined
19+
```
20+
21+
This rule is enabled by default.
22+
23+
### Default configuration
24+
25+
The following patterns are considered warnings:
26+
27+
```js
28+
expect();
29+
expect().toEqual("something");
30+
expect("something", "else");
31+
expect("something");
32+
expect(true).toBeDefined;
33+
```
34+
35+
The following patterns are not warnings:
36+
37+
```js
38+
expect("something").toEqual("something");
39+
expect([1, 2, 3]).toEqual([1, 2, 3]);
40+
expect(true).toBeDefined();
41+
```

‎packages/eslint-plugin-jest/src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = {
1717
'jest/no-disabled-tests': 'warn',
1818
'jest/no-focused-tests': 'error',
1919
'jest/no-identical-title': 'error',
20+
'jest/valid-expect': 'error',
2021
},
2122
},
2223
},
@@ -46,5 +47,6 @@ module.exports = {
4647
'no-disabled-tests': require('./rules/no-disabled-tests'),
4748
'no-focused-tests': require('./rules/no-focused-tests'),
4849
'no-identical-title': require('./rules/no-identical-title'),
50+
'valid-expect': require('./rules/valid-expect'),
4951
},
5052
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*
8+
* @flow
9+
*/
10+
11+
// This implementation is copied from eslint-plugin-jasmine.
12+
// Credits goes to Alexander Afanasyev
13+
// TODO: Should license at the top be MIT, as that's what the code in
14+
// eslint-plugin-jasmine is?
15+
16+
/* eslint-disable sort-keys */
17+
18+
'use strict';
19+
20+
const RuleTester = require('eslint').RuleTester;
21+
const rules = require('../../').rules;
22+
23+
const ruleTester = new RuleTester();
24+
25+
ruleTester.run('valid-expect', rules['valid-expect'], {
26+
valid: [
27+
'expect("something").toEqual("else");',
28+
'expect(true).toBeDefined();',
29+
'expect([1, 2, 3]).toEqual([1, 2, 3]);',
30+
'expect(undefined).not.toBeDefined();',
31+
],
32+
33+
invalid: [
34+
{
35+
code: 'expect().toBe(true);',
36+
errors: [
37+
{
38+
message: 'No arguments passed to expect()',
39+
},
40+
],
41+
},
42+
{
43+
code: 'expect().toEqual("something");',
44+
errors: [
45+
{
46+
message: 'No arguments passed to expect()',
47+
},
48+
],
49+
},
50+
{
51+
code: 'expect("something", "else").toEqual("something");',
52+
errors: [
53+
{
54+
message: 'More than one argument passed to expect()',
55+
},
56+
],
57+
},
58+
{
59+
code: 'expect("something");',
60+
errors: [
61+
{
62+
message: 'Matcher was not called',
63+
},
64+
{
65+
message: 'Nothing called on expect()',
66+
},
67+
],
68+
},
69+
{
70+
code: 'expect();',
71+
errors: [
72+
{
73+
message: 'No arguments passed to expect()',
74+
},
75+
{
76+
message: 'Matcher was not called',
77+
},
78+
{
79+
message: 'Nothing called on expect()',
80+
},
81+
],
82+
},
83+
{
84+
code: 'expect(true).toBeDefined;',
85+
errors: [
86+
{
87+
message: 'Matcher was not called',
88+
},
89+
],
90+
},
91+
],
92+
});

‎packages/eslint-plugin-jest/src/rules/types.js

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export type CallExpression = {|
3232
type: 'CallExpression',
3333
arguments: [Literal],
3434
callee: Identifier | MemberExpression,
35+
parent: any,
3536
|};
3637

3738
export type EslintContext = {|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*
8+
* @flow
9+
*/
10+
'use strict';
11+
12+
// This implementation is copied from eslint-plugin-jasmine.
13+
// Credits goes to Alexander Afanasyev
14+
// TODO: Should license at the top be MIT, as that's what the code in
15+
// eslint-plugin-jasmine is?
16+
17+
import type {EslintContext, CallExpression} from './types';
18+
19+
module.exports = (context: EslintContext) => {
20+
return {
21+
CallExpression(node: CallExpression) {
22+
if (node.callee.name === 'expect') {
23+
// checking "expect()" arguments
24+
if (node.arguments.length > 1) {
25+
context.report({
26+
message: 'More than one argument passed to expect()',
27+
node,
28+
});
29+
} else if (node.arguments.length === 0) {
30+
context.report({message: 'No arguments passed to expect()', node});
31+
}
32+
33+
// matcher was not called
34+
if (
35+
node.parent &&
36+
node.parent.parent &&
37+
node.parent.parent.type !== 'CallExpression' &&
38+
node.parent.parent.type !== 'MemberExpression'
39+
) {
40+
context.report({message: 'Matcher was not called', node});
41+
}
42+
}
43+
},
44+
45+
// nothing called on "expect()"
46+
'CallExpression:exit'(node: CallExpression) {
47+
if (
48+
node.callee.name === 'expect' &&
49+
node.parent.type === 'ExpressionStatement'
50+
) {
51+
context.report({message: 'Nothing called on expect()', node});
52+
}
53+
},
54+
};
55+
};

‎packages/pretty-format/src/__tests__/pretty-format-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ describe('prettyFormat()', () => {
234234
});
235235

236236
it('prints a string with escapes', () => {
237-
expect(prettyFormat('\"-\"'), '"\\"-\\""');
238-
expect(prettyFormat('\\ \\\\'), '"\\\\ \\\\\\\\"');
237+
expect(prettyFormat('\"-\"')).toEqual('"\\"-\\""');
238+
expect(prettyFormat('\\ \\\\')).toEqual('"\\\\ \\\\\\\\"');
239239
});
240240

241241
it('prints a symbol', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.