eslint-plugin-jest 21.21.0 → 21.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,9 +7,7 @@ Ensure that there is at least one `expect` call made in a test.
7
7
  This rule triggers when there is no call made to `expect` in a test, to prevent
8
8
  users from forgetting to add assertions.
9
9
 
10
- ### Default configuration
11
-
12
- The following patterns are considered warnings:
10
+ Examples of **incorrect** code for this rule:
13
11
 
14
12
  ```js
15
13
  it('should be a test', () => {
@@ -18,7 +16,7 @@ it('should be a test', () => {
18
16
  test('should assert something', () => {});
19
17
  ```
20
18
 
21
- The following patterns are not warnings:
19
+ Examples of **correct** code for this rule:
22
20
 
23
21
  ```js
24
22
  it('should be a test', () => {
@@ -28,3 +26,52 @@ it('should work with callbacks/async', () => {
28
26
  somePromise().then(res => expect(res).toBe('passed'));
29
27
  });
30
28
  ```
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
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest",
3
- "version": "21.21.0",
3
+ "version": "21.22.0",
4
4
  "description": "Eslint rules for Jest",
5
5
  "repository": "jest-community/eslint-plugin-jest",
6
6
  "license": "MIT",
@@ -14,6 +14,15 @@ ruleTester.run('expect-expect', rule, {
14
14
  'it("should pass", () => expect(true).toBeDefined())',
15
15
  'test("should pass", () => expect(true).toBeDefined())',
16
16
  '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
+ },
17
26
  ],
18
27
 
19
28
  invalid: [
@@ -44,5 +53,25 @@ ruleTester.run('expect-expect', rule, {
44
53
  },
45
54
  ],
46
55
  },
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
+ },
47
76
  ],
48
77
  });
@@ -12,10 +12,26 @@ module.exports = {
12
12
  docs: {
13
13
  url: getDocsUrl(__filename),
14
14
  },
15
+ schema: [
16
+ {
17
+ type: 'object',
18
+ properties: {
19
+ assertFunctionNames: {
20
+ type: 'array',
21
+ items: [{ type: 'string' }],
22
+ },
23
+ },
24
+ additionalProperties: false,
25
+ },
26
+ ],
15
27
  },
16
28
  create(context) {
17
29
  // variables should be defined here
18
30
  const unchecked = [];
31
+ const assertFunctionNames =
32
+ context.options[0] && context.options[0].assertFunctionNames
33
+ ? context.options[0].assertFunctionNames
34
+ : ['expect'];
19
35
 
20
36
  //----------------------------------------------------------------------
21
37
  // Helpers
@@ -23,8 +39,8 @@ module.exports = {
23
39
  const isExpectCall = node =>
24
40
  // if we're not calling a function, ignore
25
41
  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);
28
44
  //----------------------------------------------------------------------
29
45
  // Public
30
46
  //----------------------------------------------------------------------