eslint-plugin-jest 21.20.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.
- package/README.md +4 -0
- package/docs/rules/expect-expect.md +51 -4
- package/docs/rules/prefer-strict-equal.md +24 -0
- package/index.js +4 -0
- package/package.json +1 -1
- package/rules/__tests__/__snapshots__/no-large-snapshots.test.js.snap +22 -0
- package/rules/__tests__/expect-expect.test.js +29 -0
- package/rules/__tests__/no-large-snapshots.test.js +23 -0
- package/rules/__tests__/prefer-strict-equal.test.js +23 -0
- package/rules/expect-expect.js +18 -2
- package/rules/no-large-snapshots.js +6 -2
- package/rules/prefer-strict-equal.js +28 -0
package/README.md
CHANGED
|
@@ -81,6 +81,7 @@ for more information about extending configuration files.
|
|
|
81
81
|
| Rule | Description | Recommended | Fixable |
|
|
82
82
|
| ---------------------------- | ----------------------------------------------------------------- | ---------------- | ------------------- |
|
|
83
83
|
| [consistent-test-it][] | Enforce consistent test or it keyword | | ![fixable-green][] |
|
|
84
|
+
| [expect-expect][] | Enforce assertion to be made in a test body | | |
|
|
84
85
|
| [lowercase-name][] | Disallow capitalized test names | | ![fixable-green][] |
|
|
85
86
|
| [no-disabled-tests][] | Disallow disabled tests | ![recommended][] | |
|
|
86
87
|
| [no-focused-tests][] | Disallow focused tests | ![recommended][] | |
|
|
@@ -92,6 +93,7 @@ for more information about extending configuration files.
|
|
|
92
93
|
| [no-test-prefixes][] | Disallow using `f` & `x` prefixes to define focused/skipped tests | | ![fixable-green][] |
|
|
93
94
|
| [no-test-return-statement][] | Disallow explicitly returning from tests | | |
|
|
94
95
|
| [prefer-expect-assertions][] | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | |
|
|
96
|
+
| [prefer-strict-equal][] | Suggest using `toStrictEqual()` | | ![fixable-green][] |
|
|
95
97
|
| [prefer-to-be-null][] | Suggest using `toBeNull()` | | ![fixable-green][] |
|
|
96
98
|
| [prefer-to-be-undefined][] | Suggest using `toBeUndefined()` | | ![fixable-green][] |
|
|
97
99
|
| [prefer-to-have-length][] | Suggest using `toHaveLength()` | ![recommended][] | ![fixable-green][] |
|
|
@@ -106,6 +108,7 @@ for more information about extending configuration files.
|
|
|
106
108
|
- [eslint-plugin-jasmine](https://github.com/tlvince/eslint-plugin-jasmine)
|
|
107
109
|
|
|
108
110
|
[consistent-test-it]: docs/rules/consistent-test-it.md
|
|
111
|
+
[expect-expect]: docs/rules/expect-expect.md
|
|
109
112
|
[lowercase-name]: docs/rules/lowercase-name.md
|
|
110
113
|
[no-disabled-tests]: docs/rules/no-disabled-tests.md
|
|
111
114
|
[no-focused-tests]: docs/rules/no-focused-tests.md
|
|
@@ -117,6 +120,7 @@ for more information about extending configuration files.
|
|
|
117
120
|
[no-test-prefixes]: docs/rules/no-test-prefixes.md
|
|
118
121
|
[no-test-return-statement]: docs/rules/no-test-return-statement.md
|
|
119
122
|
[prefer-expect-assertions]: docs/rules/prefer-expect-assertions.md
|
|
123
|
+
[prefer-strict-equal]: docs/rules/prefer-strict-equal.md
|
|
120
124
|
[prefer-to-be-null]: docs/rules/prefer-to-be-null.md
|
|
121
125
|
[prefer-to-be-undefined]: docs/rules/prefer-to-be-undefined.md
|
|
122
126
|
[prefer-to-have-length]: docs/rules/prefer-to-have-length.md
|
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
+
```
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Suggest using `toStrictEqual()` (prefer-strict-equal)
|
|
2
|
+
|
|
3
|
+
`toStrictEqual` not only checks that two objects contain the same data but also
|
|
4
|
+
that they have the same structure. It is common to expect objects to not only
|
|
5
|
+
have identical values but also to have identical keys. A stricter equality will
|
|
6
|
+
catch cases where two objects do not have identical keys.
|
|
7
|
+
|
|
8
|
+
## Rule details
|
|
9
|
+
|
|
10
|
+
This rule triggers a warning if `toEqual()` is used to assert equality.
|
|
11
|
+
|
|
12
|
+
### Default configuration
|
|
13
|
+
|
|
14
|
+
The following pattern is considered warning:
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
expect({ a: 'a', b: undefined }).toEqual({ a: 'a' }); // true
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
The following pattern is not warning:
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
expect({ a: 'a', b: undefined }).toStrictEqual({ a: 'a' }); // false
|
|
24
|
+
```
|
package/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const consistentTestIt = require('./rules/consistent-test-it');
|
|
4
|
+
const expectExpect = require('./rules/expect-expect');
|
|
4
5
|
const lowercaseName = require('./rules/lowercase-name');
|
|
5
6
|
const noDisabledTests = require('./rules/no-disabled-tests');
|
|
6
7
|
const noFocusedTests = require('./rules/no-focused-tests');
|
|
@@ -19,6 +20,7 @@ const validExpect = require('./rules/valid-expect');
|
|
|
19
20
|
const preferExpectAssertions = require('./rules/prefer-expect-assertions');
|
|
20
21
|
const validExpectInPromise = require('./rules/valid-expect-in-promise');
|
|
21
22
|
const preferInlineSnapshots = require('./rules/prefer-inline-snapshots');
|
|
23
|
+
const preferStrictEqual = require('./rules/prefer-strict-equal');
|
|
22
24
|
|
|
23
25
|
const snapshotProcessor = require('./processors/snapshot-processor');
|
|
24
26
|
|
|
@@ -67,6 +69,7 @@ module.exports = {
|
|
|
67
69
|
},
|
|
68
70
|
rules: {
|
|
69
71
|
'consistent-test-it': consistentTestIt,
|
|
72
|
+
'expect-expect': expectExpect,
|
|
70
73
|
'lowercase-name': lowercaseName,
|
|
71
74
|
'no-disabled-tests': noDisabledTests,
|
|
72
75
|
'no-focused-tests': noFocusedTests,
|
|
@@ -85,5 +88,6 @@ module.exports = {
|
|
|
85
88
|
'prefer-expect-assertions': preferExpectAssertions,
|
|
86
89
|
'valid-expect-in-promise': validExpectInPromise,
|
|
87
90
|
'prefer-inline-snapshots': preferInlineSnapshots,
|
|
91
|
+
'prefer-strict-equal': preferStrictEqual,
|
|
88
92
|
},
|
|
89
93
|
};
|
package/package.json
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
2
|
|
|
3
|
+
exports[`no-large-snapshots ExpressionStatement function should report if maxSize is zero 1`] = `
|
|
4
|
+
Array [
|
|
5
|
+
Object {
|
|
6
|
+
"data": Object {
|
|
7
|
+
"lineCount": 1,
|
|
8
|
+
"lineLimit": 0,
|
|
9
|
+
},
|
|
10
|
+
"message": "Expected to not encounter a Jest snapshot but was found with {{ lineCount }} lines long",
|
|
11
|
+
"node": Object {
|
|
12
|
+
"loc": Object {
|
|
13
|
+
"end": Object {
|
|
14
|
+
"line": 2,
|
|
15
|
+
},
|
|
16
|
+
"start": Object {
|
|
17
|
+
"line": 1,
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
]
|
|
23
|
+
`;
|
|
24
|
+
|
|
3
25
|
exports[`no-large-snapshots ExpressionStatement function should report if node has more lines of code than number given in sizeThreshold option 1`] = `
|
|
4
26
|
Array [
|
|
5
27
|
Object {
|
|
@@ -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
|
});
|
|
@@ -74,6 +74,29 @@ describe('no-large-snapshots', () => {
|
|
|
74
74
|
expect(mockReport.mock.calls[0]).toMatchSnapshot();
|
|
75
75
|
});
|
|
76
76
|
|
|
77
|
+
it('should report if maxSize is zero', () => {
|
|
78
|
+
const mockReport = jest.fn();
|
|
79
|
+
const mockContext = {
|
|
80
|
+
getFilename: () => 'mock-component.jsx.snap',
|
|
81
|
+
options: [{ maxSize: 0 }],
|
|
82
|
+
report: mockReport,
|
|
83
|
+
};
|
|
84
|
+
const mockNode = {
|
|
85
|
+
loc: {
|
|
86
|
+
start: {
|
|
87
|
+
line: 1,
|
|
88
|
+
},
|
|
89
|
+
end: {
|
|
90
|
+
line: 2,
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
noLargeSnapshots(mockContext).ExpressionStatement(mockNode);
|
|
95
|
+
|
|
96
|
+
expect(mockReport).toHaveBeenCalledTimes(1);
|
|
97
|
+
expect(mockReport.mock.calls[0]).toMatchSnapshot();
|
|
98
|
+
});
|
|
99
|
+
|
|
77
100
|
it('should not report if node has fewer lines of code than limit', () => {
|
|
78
101
|
const mockReport = jest.fn();
|
|
79
102
|
const mockContext = {
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const RuleTester = require('eslint').RuleTester;
|
|
4
|
+
const rule = require('../prefer-strict-equal');
|
|
5
|
+
|
|
6
|
+
const ruleTester = new RuleTester();
|
|
7
|
+
|
|
8
|
+
ruleTester.run('prefer-strict-equal', rule, {
|
|
9
|
+
valid: ['expect(something).toStrictEqual(somethingElse);'],
|
|
10
|
+
invalid: [
|
|
11
|
+
{
|
|
12
|
+
code: 'expect(something).toEqual(somethingElse);',
|
|
13
|
+
errors: [
|
|
14
|
+
{
|
|
15
|
+
message: 'Use toStrictEqual() instead',
|
|
16
|
+
column: 19,
|
|
17
|
+
line: 1,
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
output: 'expect(something).toStrictEqual(somethingElse);',
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
});
|
package/rules/expect-expect.js
CHANGED
|
@@ -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
|
|
27
|
-
node.callee.name
|
|
42
|
+
// if we're not calling allowed assertion
|
|
43
|
+
assertFunctionNames.some(name => name === node.callee.name);
|
|
28
44
|
//----------------------------------------------------------------------
|
|
29
45
|
// Public
|
|
30
46
|
//----------------------------------------------------------------------
|
|
@@ -11,7 +11,9 @@ module.exports = {
|
|
|
11
11
|
create(context) {
|
|
12
12
|
if (context.getFilename().endsWith('.snap')) {
|
|
13
13
|
const lineLimit =
|
|
14
|
-
|
|
14
|
+
context.options[0] && Number.isFinite(context.options[0].maxSize)
|
|
15
|
+
? context.options[0].maxSize
|
|
16
|
+
: 50;
|
|
15
17
|
|
|
16
18
|
return {
|
|
17
19
|
ExpressionStatement(node) {
|
|
@@ -22,7 +24,9 @@ module.exports = {
|
|
|
22
24
|
if (lineCount > lineLimit) {
|
|
23
25
|
context.report({
|
|
24
26
|
message:
|
|
25
|
-
|
|
27
|
+
lineLimit === 0
|
|
28
|
+
? 'Expected to not encounter a Jest snapshot but was found with {{ lineCount }} lines long'
|
|
29
|
+
: 'Expected Jest snapshot to be smaller than {{ lineLimit }} lines but was {{ lineCount }} lines long',
|
|
26
30
|
data: { lineLimit, lineCount },
|
|
27
31
|
node,
|
|
28
32
|
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const getDocsUrl = require('./util').getDocsUrl;
|
|
4
|
+
|
|
5
|
+
module.exports = {
|
|
6
|
+
meta: {
|
|
7
|
+
docs: {
|
|
8
|
+
url: getDocsUrl(__filename),
|
|
9
|
+
},
|
|
10
|
+
fixable: 'code',
|
|
11
|
+
},
|
|
12
|
+
create(context) {
|
|
13
|
+
return {
|
|
14
|
+
CallExpression(node) {
|
|
15
|
+
const propertyName = node.callee.property && node.callee.property.name;
|
|
16
|
+
if (propertyName === 'toEqual') {
|
|
17
|
+
context.report({
|
|
18
|
+
fix(fixer) {
|
|
19
|
+
return [fixer.replaceText(node.callee.property, 'toStrictEqual')];
|
|
20
|
+
},
|
|
21
|
+
message: 'Use toStrictEqual() instead',
|
|
22
|
+
node: node.callee.property,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
},
|
|
28
|
+
};
|