Skip to content

Commit 97a5fce

Browse files
hulkishSimenB
authored andcommittedAug 24, 2018
feat(rule): add allow option to expect-expect rule (#139)
1 parent e039fed commit 97a5fce

File tree

3 files changed

+98
-6
lines changed

3 files changed

+98
-6
lines changed
 

Diff for: ‎docs/rules/expect-expect.md

+51-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ Ensure that there is at least one `expect` call made in a test.
77
This rule triggers when there is no call made to `expect` in a test, to prevent
88
users from forgetting to add assertions.
99

10-
### Default configuration
11-
12-
The following patterns are considered warnings:
10+
Examples of **incorrect** code for this rule:
1311

1412
```js
1513
it('should be a test', () => {
@@ -18,7 +16,7 @@ it('should be a test', () => {
1816
test('should assert something', () => {});
1917
```
2018

21-
The following patterns are not warnings:
19+
Examples of **correct** code for this rule:
2220

2321
```js
2422
it('should be a test', () => {
@@ -28,3 +26,52 @@ it('should work with callbacks/async', () => {
2826
somePromise().then(res => expect(res).toBe('passed'));
2927
});
3028
```
29+
30+
## Options
31+
32+
```json
33+
{
34+
"jest/expect-expect": [
35+
"error",
36+
{
37+
"assertFunctionNames": ["expect"]
38+
}
39+
]
40+
}
41+
```
42+
43+
### `assertFunctionNames`
44+
45+
This array option whitelists the assertion function names to look for.
46+
47+
Examples of **incorrect** code for the `{ "assertFunctionNames": ["expect"] }`
48+
option:
49+
50+
```js
51+
/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect"] }] */
52+
53+
import { expectSaga } from 'redux-saga-test-plan';
54+
import { addSaga } from '../src/sagas';
55+
56+
test('returns sum', () => {
57+
expectSaga(addSaga, 1, 1)
58+
.returns(2)
59+
.run();
60+
});
61+
```
62+
63+
Examples of **correct** code for the
64+
`{ "assertFunctionNames": ["expect", "expectSaga"] }` option:
65+
66+
```js
67+
/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "expectSaga"] }] */
68+
69+
import { expectSaga } from 'redux-saga-test-plan';
70+
import { addSaga } from '../src/sagas';
71+
72+
test('returns sum', () => {
73+
expectSaga(addSaga, 1, 1)
74+
.returns(2)
75+
.run();
76+
});
77+
```

Diff for: ‎rules/__tests__/expect-expect.test.js

+29
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ ruleTester.run('expect-expect', rule, {
1414
'it("should pass", () => expect(true).toBeDefined())',
1515
'test("should pass", () => expect(true).toBeDefined())',
1616
'it("should pass", () => somePromise().then(() => expect(true).toBeDefined()))',
17+
{
18+
code:
19+
'test("should pass", () => { expect(true).toBeDefined(); foo(true).toBe(true); })',
20+
options: [{ assertFunctionNames: ['expect', 'foo'] }],
21+
},
22+
{
23+
code: 'it("should return undefined",() => expectSaga(mySaga).returns());',
24+
options: [{ assertFunctionNames: ['expectSaga'] }],
25+
},
1726
],
1827

1928
invalid: [
@@ -44,5 +53,25 @@ ruleTester.run('expect-expect', rule, {
4453
},
4554
],
4655
},
56+
{
57+
code: 'test("should fail", () => { foo(true).toBe(true); })',
58+
options: [{ assertFunctionNames: ['expect'] }],
59+
errors: [
60+
{
61+
message: 'Test has no assertions',
62+
type: 'CallExpression',
63+
},
64+
],
65+
},
66+
{
67+
code: 'it("should also fail",() => expectSaga(mySaga).returns());',
68+
options: [{ assertFunctionNames: ['expect'] }],
69+
errors: [
70+
{
71+
message: 'Test has no assertions',
72+
type: 'CallExpression',
73+
},
74+
],
75+
},
4776
],
4877
});

Diff for: ‎rules/expect-expect.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,35 @@ module.exports = {
1212
docs: {
1313
url: getDocsUrl(__filename),
1414
},
15+
schema: [
16+
{
17+
type: 'object',
18+
properties: {
19+
assertFunctionNames: {
20+
type: 'array',
21+
items: [{ type: 'string' }],
22+
},
23+
},
24+
additionalProperties: false,
25+
},
26+
],
1527
},
1628
create(context) {
1729
// variables should be defined here
1830
const unchecked = [];
31+
const assertFunctionNames =
32+
context.options[0] && context.options[0].assertFunctionNames
33+
? context.options[0].assertFunctionNames
34+
: ['expect'];
1935

2036
//----------------------------------------------------------------------
2137
// Helpers
2238
//----------------------------------------------------------------------
2339
const isExpectCall = node =>
2440
// if we're not calling a function, ignore
2541
node.type === 'CallExpression' &&
26-
// if we're not calling expect, ignore
27-
node.callee.name === 'expect';
42+
// if we're not calling allowed assertion
43+
assertFunctionNames.some(name => name === node.callee.name);
2844
//----------------------------------------------------------------------
2945
// Public
3046
//----------------------------------------------------------------------

0 commit comments

Comments
 (0)
Please sign in to comment.