eslint-plugin-jest 24.6.0 → 24.7.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/CHANGELOG.md +9 -0
- package/README.md +1 -2
- package/docs/rules/prefer-to-be-null.md +4 -0
- package/docs/rules/prefer-to-be-undefined.md +4 -0
- package/docs/rules/require-hook.md +149 -0
- package/lib/rules/prefer-to-be-null.js +2 -0
- package/lib/rules/prefer-to-be-undefined.js +2 -0
- package/lib/rules/require-hook.js +87 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
# [24.7.0](https://github.com/jest-community/eslint-plugin-jest/compare/v24.6.0...v24.7.0) (2021-10-10)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* create `require-hook` rule ([#929](https://github.com/jest-community/eslint-plugin-jest/issues/929)) ([6204b31](https://github.com/jest-community/eslint-plugin-jest/commit/6204b311e849b51a0e4705015575139f590ae7a4))
|
|
7
|
+
* deprecate `prefer-to-be-null` rule ([4db9161](https://github.com/jest-community/eslint-plugin-jest/commit/4db91612e988e84ac2facbfe466331b22eeccec9))
|
|
8
|
+
* deprecate `prefer-to-be-undefined` rule ([fa08f09](https://github.com/jest-community/eslint-plugin-jest/commit/fa08f0944e89915fb215bbeff970f12459121cb8))
|
|
9
|
+
|
|
1
10
|
# [24.6.0](https://github.com/jest-community/eslint-plugin-jest/compare/v24.5.2...v24.6.0) (2021-10-09)
|
|
2
11
|
|
|
3
12
|
|
package/README.md
CHANGED
|
@@ -184,11 +184,10 @@ installations requiring long-term consistency.
|
|
|
184
184
|
| [prefer-spy-on](docs/rules/prefer-spy-on.md) | Suggest using `jest.spyOn()` | | ![fixable][] |
|
|
185
185
|
| [prefer-strict-equal](docs/rules/prefer-strict-equal.md) | Suggest using `toStrictEqual()` | | ![suggest][] |
|
|
186
186
|
| [prefer-to-be](docs/rules/prefer-to-be.md) | Suggest using `toBe()` for primitive literals | | ![fixable][] |
|
|
187
|
-
| [prefer-to-be-null](docs/rules/prefer-to-be-null.md) | Suggest using `toBeNull()` | ![style][] | ![fixable][] |
|
|
188
|
-
| [prefer-to-be-undefined](docs/rules/prefer-to-be-undefined.md) | Suggest using `toBeUndefined()` | ![style][] | ![fixable][] |
|
|
189
187
|
| [prefer-to-contain](docs/rules/prefer-to-contain.md) | Suggest using `toContain()` | ![style][] | ![fixable][] |
|
|
190
188
|
| [prefer-to-have-length](docs/rules/prefer-to-have-length.md) | Suggest using `toHaveLength()` | ![style][] | ![fixable][] |
|
|
191
189
|
| [prefer-todo](docs/rules/prefer-todo.md) | Suggest using `test.todo` | | ![fixable][] |
|
|
190
|
+
| [require-hook](docs/rules/require-hook.md) | Require setup and teardown code to be within a hook | | |
|
|
192
191
|
| [require-to-throw-message](docs/rules/require-to-throw-message.md) | Require a message for `toThrow()` | | |
|
|
193
192
|
| [require-top-level-describe](docs/rules/require-top-level-describe.md) | Require test cases and hooks to be inside a `describe` block | | |
|
|
194
193
|
| [valid-describe](docs/rules/valid-describe.md) | Enforce valid `describe()` callback | ![recommended][] | |
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# Suggest using `toBeNull()` (`prefer-to-be-null`)
|
|
2
2
|
|
|
3
|
+
## Deprecated
|
|
4
|
+
|
|
5
|
+
This rule has been deprecated in favor of [`prefer-to-be`](prefer-to-be.md).
|
|
6
|
+
|
|
3
7
|
In order to have a better failure message, `toBeNull()` should be used upon
|
|
4
8
|
asserting expectations on null value.
|
|
5
9
|
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# Suggest using `toBeUndefined()` (`prefer-to-be-undefined`)
|
|
2
2
|
|
|
3
|
+
## Deprecated
|
|
4
|
+
|
|
5
|
+
This rule has been deprecated in favor of [`prefer-to-be`](prefer-to-be.md).
|
|
6
|
+
|
|
3
7
|
In order to have a better failure message, `toBeUndefined()` should be used upon
|
|
4
8
|
asserting expectations on undefined value.
|
|
5
9
|
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# Require setup and teardown code to be within a hook (`require-hook`)
|
|
2
|
+
|
|
3
|
+
Often while writing tests you have some setup work that needs to happen before
|
|
4
|
+
tests run, and you have some finishing work that needs to happen after tests
|
|
5
|
+
run. Jest provides helper functions to handle this.
|
|
6
|
+
|
|
7
|
+
It's common when writing tests to need to perform setup work that needs to
|
|
8
|
+
happen before tests run, and finishing work after tests run.
|
|
9
|
+
|
|
10
|
+
Because Jest executes all `describe` handlers in a test file _before_ it
|
|
11
|
+
executes any of the actual tests, it's important to ensure setup and teardown
|
|
12
|
+
work is done inside `before*` and `after*` handlers respectively, rather than
|
|
13
|
+
inside the `describe` blocks.
|
|
14
|
+
|
|
15
|
+
## Rule details
|
|
16
|
+
|
|
17
|
+
This rule flags any expression that is either at the toplevel of a test file or
|
|
18
|
+
directly within the body of a `describe`, _except_ for the following:
|
|
19
|
+
|
|
20
|
+
- `import` statements
|
|
21
|
+
- `const` variables
|
|
22
|
+
- `let` _declarations_
|
|
23
|
+
- Types
|
|
24
|
+
- Calls to the standard Jest globals
|
|
25
|
+
|
|
26
|
+
This rule flags any function calls within test files that are directly within
|
|
27
|
+
the body of a `describe`, and suggests wrapping them in one of the four
|
|
28
|
+
lifecycle hooks.
|
|
29
|
+
|
|
30
|
+
Here is a slightly contrived test file showcasing some common cases that would
|
|
31
|
+
be flagged:
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
import { database, isCity } from '../database';
|
|
35
|
+
import { Logger } from '../../../src/Logger';
|
|
36
|
+
import { loadCities } from '../api';
|
|
37
|
+
|
|
38
|
+
jest.mock('../api');
|
|
39
|
+
|
|
40
|
+
const initializeCityDatabase = () => {
|
|
41
|
+
database.addCity('Vienna');
|
|
42
|
+
database.addCity('San Juan');
|
|
43
|
+
database.addCity('Wellington');
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const clearCityDatabase = () => {
|
|
47
|
+
database.clear();
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
initializeCityDatabase();
|
|
51
|
+
|
|
52
|
+
test('that persists cities', () => {
|
|
53
|
+
expect(database.cities.length).toHaveLength(3);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test('city database has Vienna', () => {
|
|
57
|
+
expect(isCity('Vienna')).toBeTruthy();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('city database has San Juan', () => {
|
|
61
|
+
expect(isCity('San Juan')).toBeTruthy();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe('when loading cities from the api', () => {
|
|
65
|
+
let consoleWarnSpy = jest.spyOn(console, 'warn');
|
|
66
|
+
|
|
67
|
+
loadCities.mockResolvedValue(['Wellington', 'London']);
|
|
68
|
+
|
|
69
|
+
it('does not duplicate cities', async () => {
|
|
70
|
+
await database.loadCities();
|
|
71
|
+
|
|
72
|
+
expect(database.cities).toHaveLength(4);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('logs any duplicates', async () => {
|
|
76
|
+
await database.loadCities();
|
|
77
|
+
|
|
78
|
+
expect(consoleWarnSpy).toHaveBeenCalledWith(
|
|
79
|
+
'Ignored duplicate cities: Wellington',
|
|
80
|
+
);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
clearCityDatabase();
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Here is the same slightly contrived test file showcasing the same common cases
|
|
88
|
+
but in ways that would be **not** flagged:
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
import { database, isCity } from '../database';
|
|
92
|
+
import { Logger } from '../../../src/Logger';
|
|
93
|
+
import { loadCities } from '../api';
|
|
94
|
+
|
|
95
|
+
jest.mock('../api');
|
|
96
|
+
|
|
97
|
+
const initializeCityDatabase = () => {
|
|
98
|
+
database.addCity('Vienna');
|
|
99
|
+
database.addCity('San Juan');
|
|
100
|
+
database.addCity('Wellington');
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const clearCityDatabase = () => {
|
|
104
|
+
database.clear();
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
beforeEach(() => {
|
|
108
|
+
initializeCityDatabase();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('that persists cities', () => {
|
|
112
|
+
expect(database.cities.length).toHaveLength(3);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test('city database has Vienna', () => {
|
|
116
|
+
expect(isCity('Vienna')).toBeTruthy();
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('city database has San Juan', () => {
|
|
120
|
+
expect(isCity('San Juan')).toBeTruthy();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe('when loading cities from the api', () => {
|
|
124
|
+
let consoleWarnSpy;
|
|
125
|
+
|
|
126
|
+
beforeEach(() => {
|
|
127
|
+
consoleWarnSpy = jest.spyOn(console, 'warn');
|
|
128
|
+
loadCities.mockResolvedValue(['Wellington', 'London']);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('does not duplicate cities', async () => {
|
|
132
|
+
await database.loadCities();
|
|
133
|
+
|
|
134
|
+
expect(database.cities).toHaveLength(4);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('logs any duplicates', async () => {
|
|
138
|
+
await database.loadCities();
|
|
139
|
+
|
|
140
|
+
expect(consoleWarnSpy).toHaveBeenCalledWith(
|
|
141
|
+
'Ignored duplicate cities: Wellington',
|
|
142
|
+
);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
afterEach(() => {
|
|
147
|
+
clearCityDatabase();
|
|
148
|
+
});
|
|
149
|
+
```
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _experimentalUtils = require("@typescript-eslint/experimental-utils");
|
|
9
|
+
|
|
10
|
+
var _utils = require("./utils");
|
|
11
|
+
|
|
12
|
+
const isJestFnCall = node => {
|
|
13
|
+
var _getNodeName;
|
|
14
|
+
|
|
15
|
+
if ((0, _utils.isDescribeCall)(node) || (0, _utils.isTestCaseCall)(node) || (0, _utils.isHook)(node)) {
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return !!((_getNodeName = (0, _utils.getNodeName)(node)) !== null && _getNodeName !== void 0 && _getNodeName.startsWith('jest.'));
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const shouldBeInHook = node => {
|
|
23
|
+
switch (node.type) {
|
|
24
|
+
case _experimentalUtils.AST_NODE_TYPES.ExpressionStatement:
|
|
25
|
+
return shouldBeInHook(node.expression);
|
|
26
|
+
|
|
27
|
+
case _experimentalUtils.AST_NODE_TYPES.CallExpression:
|
|
28
|
+
return !isJestFnCall(node);
|
|
29
|
+
|
|
30
|
+
default:
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
var _default = (0, _utils.createRule)({
|
|
36
|
+
name: __filename,
|
|
37
|
+
meta: {
|
|
38
|
+
docs: {
|
|
39
|
+
category: 'Best Practices',
|
|
40
|
+
description: 'Require setup and teardown code to be within a hook',
|
|
41
|
+
recommended: false
|
|
42
|
+
},
|
|
43
|
+
messages: {
|
|
44
|
+
useHook: 'This should be done within a hook'
|
|
45
|
+
},
|
|
46
|
+
type: 'suggestion',
|
|
47
|
+
schema: []
|
|
48
|
+
},
|
|
49
|
+
defaultOptions: [],
|
|
50
|
+
|
|
51
|
+
create(context) {
|
|
52
|
+
const checkBlockBody = body => {
|
|
53
|
+
for (const statement of body) {
|
|
54
|
+
if (shouldBeInHook(statement)) {
|
|
55
|
+
context.report({
|
|
56
|
+
node: statement,
|
|
57
|
+
messageId: 'useHook'
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
Program(program) {
|
|
65
|
+
checkBlockBody(program.body);
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
CallExpression(node) {
|
|
69
|
+
if (!(0, _utils.isDescribeCall)(node) || node.arguments.length < 2) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const [, testFn] = node.arguments;
|
|
74
|
+
|
|
75
|
+
if (!(0, _utils.isFunction)(testFn) || testFn.body.type !== _experimentalUtils.AST_NODE_TYPES.BlockStatement) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
checkBlockBody(testFn.body.body);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
exports.default = _default;
|