eslint-plugin-jest 23.18.2 → 24.0.1
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 +61 -0
- package/README.md +45 -41
- package/docs/rules/consistent-test-it.md +4 -4
- package/docs/rules/expect-expect.md +5 -12
- package/docs/rules/no-done-callback.md +90 -0
- package/docs/rules/no-export.md +1 -1
- package/docs/rules/no-interpolation-in-snapshots.md +60 -0
- package/docs/rules/no-large-snapshots.md +4 -4
- package/docs/rules/no-test-return-statement.md +3 -3
- package/docs/rules/valid-title.md +2 -2
- package/lib/globals.json +0 -5
- package/lib/index.js +4 -10
- package/lib/rules/no-conditional-expect.js +1 -1
- package/lib/rules/no-deprecated-functions.js +22 -26
- package/lib/rules/{no-test-callback.js → no-done-callback.js} +18 -16
- package/lib/rules/no-focused-tests.js +1 -1
- package/lib/rules/no-interpolation-in-snapshots.js +63 -0
- package/lib/rules/no-jasmine-globals.js +1 -1
- package/lib/rules/no-large-snapshots.js +35 -31
- package/lib/rules/no-test-return-statement.js +1 -1
- package/lib/rules/prefer-to-have-length.js +1 -1
- package/lib/rules/prefer-todo.js +1 -7
- package/lib/rules/require-to-throw-message.js +3 -1
- package/lib/rules/utils.js +4 -9
- package/lib/rules/valid-describe.js +1 -1
- package/lib/rules/valid-expect-in-promise.js +2 -7
- package/lib/rules/valid-expect.js +2 -2
- package/lib/rules/valid-title.js +1 -1
- package/package.json +27 -34
- package/docs/rules/no-test-callback.md +0 -76
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,64 @@
|
|
|
1
|
+
## [24.0.1](https://github.com/jest-community/eslint-plugin-jest/compare/v24.0.0...v24.0.1) (2020-09-12)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* don't include deprecated rules in `all` config ([#664](https://github.com/jest-community/eslint-plugin-jest/issues/664)) ([f636021](https://github.com/jest-community/eslint-plugin-jest/commit/f636021c16215a713845c699858a2978211df49d)), closes [#663](https://github.com/jest-community/eslint-plugin-jest/issues/663)
|
|
7
|
+
|
|
8
|
+
# [24.0.0](https://github.com/jest-community/eslint-plugin-jest/compare/v23.20.0...v24.0.0) (2020-09-04)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* **no-large-snapshots:** run on all files regardless of type ([#637](https://github.com/jest-community/eslint-plugin-jest/issues/637)) ([22113db](https://github.com/jest-community/eslint-plugin-jest/commit/22113db4cdc2dab42a8e7fdb236d23e7e089741d)), closes [#370](https://github.com/jest-community/eslint-plugin-jest/issues/370)
|
|
14
|
+
* remove Jasmine globals ([#596](https://github.com/jest-community/eslint-plugin-jest/issues/596)) ([a0e2bc5](https://github.com/jest-community/eslint-plugin-jest/commit/a0e2bc526c5c22bcf4d60160242b55d03edb571d))
|
|
15
|
+
* update to typescript-eslint@4 ([1755965](https://github.com/jest-community/eslint-plugin-jest/commit/175596582b3643f36363ff444f987fac08ee0f61)), closes [#590](https://github.com/jest-community/eslint-plugin-jest/issues/590)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Code Refactoring
|
|
19
|
+
|
|
20
|
+
* **no-test-callback:** rename rule to `no-done-callback` ([#653](https://github.com/jest-community/eslint-plugin-jest/issues/653)) ([e15a8d1](https://github.com/jest-community/eslint-plugin-jest/commit/e15a8d19234b267784f87fc7acd318dc4cfcdeae))
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
### Features
|
|
24
|
+
|
|
25
|
+
* **no-done-callback:** support hooks ([#656](https://github.com/jest-community/eslint-plugin-jest/issues/656)) ([3e6cb44](https://github.com/jest-community/eslint-plugin-jest/commit/3e6cb442a20b9aea710d30f81bf2eb192d193823)), closes [#649](https://github.com/jest-community/eslint-plugin-jest/issues/649) [#651](https://github.com/jest-community/eslint-plugin-jest/issues/651)
|
|
26
|
+
* add `no-conditional-expect` to the recommended ruleset ([40cd89d](https://github.com/jest-community/eslint-plugin-jest/commit/40cd89ddf1d6ebbde8ad455f333dda7b61878ffe))
|
|
27
|
+
* add `no-deprecated-functions` to the recommended ruleset ([5b2af00](https://github.com/jest-community/eslint-plugin-jest/commit/5b2af001b50059e4e7b6ababe0355d664e039046))
|
|
28
|
+
* add `no-interpolation-in-snapshots` to the recommended ruleset ([3705dff](https://github.com/jest-community/eslint-plugin-jest/commit/3705dff9d4f77d21013e263478d8a374d9325acb))
|
|
29
|
+
* add `valid-title` to recommended ruleset ([41f7873](https://github.com/jest-community/eslint-plugin-jest/commit/41f7873f734e0122264ace42f6d99733e7e25089))
|
|
30
|
+
* drop support for node 8 ([#570](https://github.com/jest-community/eslint-plugin-jest/issues/570)) ([6788e72](https://github.com/jest-community/eslint-plugin-jest/commit/6788e72d842751400a970e72b115360ad0b12d2e))
|
|
31
|
+
* set `no-jasmine-globals` to `error` in recommended ruleset ([7080952](https://github.com/jest-community/eslint-plugin-jest/commit/7080952a6baaae7a02c78f60016ee21693121416))
|
|
32
|
+
* **no-large-snapshots:** remove `whitelistedSnapshots` option ([8c1c0c9](https://github.com/jest-community/eslint-plugin-jest/commit/8c1c0c9a3e858757b38225ccb4a624e0621b5ca2))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
### BREAKING CHANGES
|
|
36
|
+
|
|
37
|
+
* **no-done-callback:** `no-done-callback` will now report hooks using callbacks as well, not just tests
|
|
38
|
+
* **no-test-callback:** rename `no-test-callback` to `no-done-callback`
|
|
39
|
+
* recommend `no-conditional-expect` rule
|
|
40
|
+
* recommend `no-interpolation-in-snapshots` rule
|
|
41
|
+
* recommend `no-deprecated-functions` rule
|
|
42
|
+
* recommend `valid-title` rule
|
|
43
|
+
* recommend erroring for `no-jasmine-globals` rule
|
|
44
|
+
* **no-large-snapshots:** `no-large-snapshots` runs on all files regardless of type
|
|
45
|
+
* Jasmine globals are no marked as such
|
|
46
|
+
* Node 10+ required
|
|
47
|
+
|
|
48
|
+
# [23.20.0](https://github.com/jest-community/eslint-plugin-jest/compare/v23.19.0...v23.20.0) (2020-07-30)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
### Features
|
|
52
|
+
|
|
53
|
+
* **no-large-snapshots:** deprecate `whitelistedSnapshots` for new name ([#632](https://github.com/jest-community/eslint-plugin-jest/issues/632)) ([706f5c2](https://github.com/jest-community/eslint-plugin-jest/commit/706f5c2bc54797f0f32178fab1d194d9a4309f70))
|
|
54
|
+
|
|
55
|
+
# [23.19.0](https://github.com/jest-community/eslint-plugin-jest/compare/v23.18.2...v23.19.0) (2020-07-27)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
### Features
|
|
59
|
+
|
|
60
|
+
* create `no-interpolation-in-snapshots` rule ([#553](https://github.com/jest-community/eslint-plugin-jest/issues/553)) ([8d2c17c](https://github.com/jest-community/eslint-plugin-jest/commit/8d2c17c449841465630bea5269de677455ef9a8d))
|
|
61
|
+
|
|
1
62
|
## [23.18.2](https://github.com/jest-community/eslint-plugin-jest/compare/v23.18.1...v23.18.2) (2020-07-26)
|
|
2
63
|
|
|
3
64
|
|
package/README.md
CHANGED
|
@@ -57,6 +57,9 @@ doing:
|
|
|
57
57
|
}
|
|
58
58
|
```
|
|
59
59
|
|
|
60
|
+
This is included in all configs shared by this plugin, so can be omitted if
|
|
61
|
+
extending them.
|
|
62
|
+
|
|
60
63
|
The behaviour of some rules (specifically `no-deprecated-functions`) change
|
|
61
64
|
depending on the version of `jest` being used.
|
|
62
65
|
|
|
@@ -128,47 +131,48 @@ installations requiring long-term consistency.
|
|
|
128
131
|
|
|
129
132
|
<!-- begin rules list -->
|
|
130
133
|
|
|
131
|
-
| Rule
|
|
132
|
-
|
|
|
133
|
-
| [consistent-test-it](docs/rules/consistent-test-it.md)
|
|
134
|
-
| [expect-expect](docs/rules/expect-expect.md)
|
|
135
|
-
| [lowercase-name](docs/rules/lowercase-name.md)
|
|
136
|
-
| [no-alias-methods](docs/rules/no-alias-methods.md)
|
|
137
|
-
| [no-commented-out-tests](docs/rules/no-commented-out-tests.md)
|
|
138
|
-
| [no-conditional-expect](docs/rules/no-conditional-expect.md)
|
|
139
|
-
| [no-deprecated-functions](docs/rules/no-deprecated-functions.md)
|
|
140
|
-
| [no-disabled-tests](docs/rules/no-disabled-tests.md)
|
|
141
|
-
| [no-
|
|
142
|
-
| [no-
|
|
143
|
-
| [no-
|
|
144
|
-
| [no-
|
|
145
|
-
| [no-
|
|
146
|
-
| [no-
|
|
147
|
-
| [no-
|
|
148
|
-
| [no-
|
|
149
|
-
| [no-
|
|
150
|
-
| [no-
|
|
151
|
-
| [no-
|
|
152
|
-
| [no-
|
|
153
|
-
| [no-
|
|
154
|
-
| [no-
|
|
155
|
-
| [no-test-
|
|
156
|
-
| [
|
|
157
|
-
| [prefer-
|
|
158
|
-
| [prefer-
|
|
159
|
-
| [prefer-
|
|
160
|
-
| [prefer-
|
|
161
|
-
| [prefer-
|
|
162
|
-
| [prefer-to-be-
|
|
163
|
-
| [prefer-to-
|
|
164
|
-
| [prefer-to-
|
|
165
|
-
| [prefer-
|
|
166
|
-
| [
|
|
167
|
-
| [require-
|
|
168
|
-
| [
|
|
169
|
-
| [valid-
|
|
170
|
-
| [valid-expect
|
|
171
|
-
| [valid-
|
|
134
|
+
| Rule | Description | Configurations | Fixable |
|
|
135
|
+
| ---------------------------------------------------------------------------- | --------------------------------------------------------------- | ---------------- | ------------ |
|
|
136
|
+
| [consistent-test-it](docs/rules/consistent-test-it.md) | Have control over `test` and `it` usages | | ![fixable][] |
|
|
137
|
+
| [expect-expect](docs/rules/expect-expect.md) | Enforce assertion to be made in a test body | ![recommended][] | |
|
|
138
|
+
| [lowercase-name](docs/rules/lowercase-name.md) | Enforce lowercase test names | | ![fixable][] |
|
|
139
|
+
| [no-alias-methods](docs/rules/no-alias-methods.md) | Disallow alias methods | ![style][] | ![fixable][] |
|
|
140
|
+
| [no-commented-out-tests](docs/rules/no-commented-out-tests.md) | Disallow commented out tests | ![recommended][] | |
|
|
141
|
+
| [no-conditional-expect](docs/rules/no-conditional-expect.md) | Prevent calling `expect` conditionally | ![recommended][] | |
|
|
142
|
+
| [no-deprecated-functions](docs/rules/no-deprecated-functions.md) | Disallow use of deprecated functions | ![recommended][] | ![fixable][] |
|
|
143
|
+
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended][] | |
|
|
144
|
+
| [no-done-callback](docs/rules/no-done-callback.md) | Avoid using a callback in asynchronous tests and hooks | ![recommended][] | ![suggest][] |
|
|
145
|
+
| [no-duplicate-hooks](docs/rules/no-duplicate-hooks.md) | Disallow duplicate setup and teardown hooks | | |
|
|
146
|
+
| [no-export](docs/rules/no-export.md) | Disallow using `exports` in files containing tests | ![recommended][] | |
|
|
147
|
+
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ![recommended][] | ![fixable][] |
|
|
148
|
+
| [no-hooks](docs/rules/no-hooks.md) | Disallow setup and teardown hooks | | |
|
|
149
|
+
| [no-identical-title](docs/rules/no-identical-title.md) | Disallow identical titles | ![recommended][] | |
|
|
150
|
+
| [no-if](docs/rules/no-if.md) | Disallow conditional logic | | |
|
|
151
|
+
| [no-interpolation-in-snapshots](docs/rules/no-interpolation-in-snapshots.md) | Disallow string interpolation inside snapshots | ![recommended][] | |
|
|
152
|
+
| [no-jasmine-globals](docs/rules/no-jasmine-globals.md) | Disallow Jasmine globals | ![recommended][] | ![fixable][] |
|
|
153
|
+
| [no-jest-import](docs/rules/no-jest-import.md) | Disallow importing Jest | ![recommended][] | |
|
|
154
|
+
| [no-large-snapshots](docs/rules/no-large-snapshots.md) | disallow large snapshots | | |
|
|
155
|
+
| [no-mocks-import](docs/rules/no-mocks-import.md) | Disallow manually importing from `__mocks__` | ![recommended][] | |
|
|
156
|
+
| [no-restricted-matchers](docs/rules/no-restricted-matchers.md) | Disallow specific matchers & modifiers | | |
|
|
157
|
+
| [no-standalone-expect](docs/rules/no-standalone-expect.md) | Disallow using `expect` outside of `it` or `test` blocks | ![recommended][] | |
|
|
158
|
+
| [no-test-prefixes](docs/rules/no-test-prefixes.md) | Use `.only` and `.skip` over `f` and `x` | ![recommended][] | ![fixable][] |
|
|
159
|
+
| [no-test-return-statement](docs/rules/no-test-return-statement.md) | Disallow explicitly returning from tests | | |
|
|
160
|
+
| [prefer-called-with](docs/rules/prefer-called-with.md) | Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()` | | |
|
|
161
|
+
| [prefer-expect-assertions](docs/rules/prefer-expect-assertions.md) | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | ![suggest][] |
|
|
162
|
+
| [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases | | |
|
|
163
|
+
| [prefer-spy-on](docs/rules/prefer-spy-on.md) | Suggest using `jest.spyOn()` | | ![fixable][] |
|
|
164
|
+
| [prefer-strict-equal](docs/rules/prefer-strict-equal.md) | Suggest using `toStrictEqual()` | | ![suggest][] |
|
|
165
|
+
| [prefer-to-be-null](docs/rules/prefer-to-be-null.md) | Suggest using `toBeNull()` | ![style][] | ![fixable][] |
|
|
166
|
+
| [prefer-to-be-undefined](docs/rules/prefer-to-be-undefined.md) | Suggest using `toBeUndefined()` | ![style][] | ![fixable][] |
|
|
167
|
+
| [prefer-to-contain](docs/rules/prefer-to-contain.md) | Suggest using `toContain()` | ![style][] | ![fixable][] |
|
|
168
|
+
| [prefer-to-have-length](docs/rules/prefer-to-have-length.md) | Suggest using `toHaveLength()` | ![style][] | ![fixable][] |
|
|
169
|
+
| [prefer-todo](docs/rules/prefer-todo.md) | Suggest using `test.todo` | | ![fixable][] |
|
|
170
|
+
| [require-to-throw-message](docs/rules/require-to-throw-message.md) | Require a message for `toThrow()` | | |
|
|
171
|
+
| [require-top-level-describe](docs/rules/require-top-level-describe.md) | Require test cases and hooks to be inside a `describe` block | | |
|
|
172
|
+
| [valid-describe](docs/rules/valid-describe.md) | Enforce valid `describe()` callback | ![recommended][] | |
|
|
173
|
+
| [valid-expect](docs/rules/valid-expect.md) | Enforce valid `expect()` usage | ![recommended][] | |
|
|
174
|
+
| [valid-expect-in-promise](docs/rules/valid-expect-in-promise.md) | Enforce having return statement when testing with promises | ![recommended][] | |
|
|
175
|
+
| [valid-title](docs/rules/valid-title.md) | Enforce valid titles | ![recommended][] | ![fixable][] |
|
|
172
176
|
|
|
173
177
|
<!-- end rules list -->
|
|
174
178
|
|
|
@@ -59,12 +59,12 @@ test.only('foo'); // invalid
|
|
|
59
59
|
/*eslint jest/consistent-test-it: ["error", {"fn": "it", "withinDescribe": "test"}]*/
|
|
60
60
|
|
|
61
61
|
it('foo'); // valid
|
|
62
|
-
describe('foo', function() {
|
|
62
|
+
describe('foo', function () {
|
|
63
63
|
test('bar'); // valid
|
|
64
64
|
});
|
|
65
65
|
|
|
66
66
|
test('foo'); // invalid
|
|
67
|
-
describe('foo', function() {
|
|
67
|
+
describe('foo', function () {
|
|
68
68
|
it('bar'); // invalid
|
|
69
69
|
});
|
|
70
70
|
```
|
|
@@ -78,12 +78,12 @@ nested within `describe` to use `it`.
|
|
|
78
78
|
/*eslint jest/consistent-test-it: ["error"]*/
|
|
79
79
|
|
|
80
80
|
test('foo'); // valid
|
|
81
|
-
describe('foo', function() {
|
|
81
|
+
describe('foo', function () {
|
|
82
82
|
it('bar'); // valid
|
|
83
83
|
});
|
|
84
84
|
|
|
85
85
|
it('foo'); // invalid
|
|
86
|
-
describe('foo', function() {
|
|
86
|
+
describe('foo', function () {
|
|
87
87
|
test('bar'); // invalid
|
|
88
88
|
});
|
|
89
89
|
```
|
|
@@ -56,9 +56,7 @@ import { expectSaga } from 'redux-saga-test-plan';
|
|
|
56
56
|
import { addSaga } from '../src/sagas';
|
|
57
57
|
|
|
58
58
|
test('returns sum', () => {
|
|
59
|
-
expectSaga(addSaga, 1, 1)
|
|
60
|
-
.returns(2)
|
|
61
|
-
.run();
|
|
59
|
+
expectSaga(addSaga, 1, 1).returns(2).run();
|
|
62
60
|
});
|
|
63
61
|
```
|
|
64
62
|
|
|
@@ -72,9 +70,7 @@ import { expectSaga } from 'redux-saga-test-plan';
|
|
|
72
70
|
import { addSaga } from '../src/sagas';
|
|
73
71
|
|
|
74
72
|
test('returns sum', () => {
|
|
75
|
-
expectSaga(addSaga, 1, 1)
|
|
76
|
-
.returns(2)
|
|
77
|
-
.run();
|
|
73
|
+
expectSaga(addSaga, 1, 1).returns(2).run();
|
|
78
74
|
});
|
|
79
75
|
```
|
|
80
76
|
|
|
@@ -100,12 +96,9 @@ const express = require('express');
|
|
|
100
96
|
|
|
101
97
|
const app = express();
|
|
102
98
|
|
|
103
|
-
describe('GET /user', function() {
|
|
104
|
-
it('responds with json', function(done) {
|
|
105
|
-
request(app)
|
|
106
|
-
.get('/user')
|
|
107
|
-
.expect('Content-Type', /json/)
|
|
108
|
-
.expect(200, done);
|
|
99
|
+
describe('GET /user', function () {
|
|
100
|
+
it('responds with json', function (done) {
|
|
101
|
+
request(app).get('/user').expect('Content-Type', /json/).expect(200, done);
|
|
109
102
|
});
|
|
110
103
|
});
|
|
111
104
|
```
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# Avoid using a callback in asynchronous tests and hooks (`no-done-callback`)
|
|
2
|
+
|
|
3
|
+
When calling asynchronous code in hooks and tests, `jest` needs to know when the
|
|
4
|
+
asynchronous work is complete to progress the current run.
|
|
5
|
+
|
|
6
|
+
Originally the most common pattern to archive this was to use callbacks:
|
|
7
|
+
|
|
8
|
+
```js
|
|
9
|
+
test('the data is peanut butter', done => {
|
|
10
|
+
function callback(data) {
|
|
11
|
+
try {
|
|
12
|
+
expect(data).toBe('peanut butter');
|
|
13
|
+
done();
|
|
14
|
+
} catch (error) {
|
|
15
|
+
done(error);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
fetchData(callback);
|
|
20
|
+
});
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
This can be very error prone however, as it requires careful understanding of
|
|
24
|
+
how assertions work in tests or otherwise tests won't behave as expected.
|
|
25
|
+
|
|
26
|
+
For example, if the `try/catch` was left out of the above code, the test would
|
|
27
|
+
timeout rather than fail. Even with the `try/catch`, forgetting to pass the
|
|
28
|
+
caught error to `done` will result in `jest` believing the test has passed.
|
|
29
|
+
|
|
30
|
+
A more straightforward way to handle asynchronous code is to use Promises:
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
test('the data is peanut butter', () => {
|
|
34
|
+
return fetchData().then(data => {
|
|
35
|
+
expect(data).toBe('peanut butter');
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
When a test or hook returns a promise, `jest` waits for that promise to resolve,
|
|
41
|
+
as well as automatically failing should the promise reject.
|
|
42
|
+
|
|
43
|
+
If your environment supports `async/await`, this becomes even simpler:
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
test('the data is peanut butter', async () => {
|
|
47
|
+
const data = await fetchData();
|
|
48
|
+
expect(data).toBe('peanut butter');
|
|
49
|
+
});
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Rule details
|
|
53
|
+
|
|
54
|
+
This rule checks the function parameter of hooks & tests for use of the `done`
|
|
55
|
+
argument, suggesting you return a promise instead.
|
|
56
|
+
|
|
57
|
+
The following patterns are considered warnings:
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
beforeEach(done => {
|
|
61
|
+
// ...
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('myFunction()', done => {
|
|
65
|
+
// ...
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('myFunction()', function (done) {
|
|
69
|
+
// ...
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The following patterns are not considered warnings:
|
|
74
|
+
|
|
75
|
+
```js
|
|
76
|
+
beforeEach(async () => {
|
|
77
|
+
await setupUsTheBomb();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('myFunction()', () => {
|
|
81
|
+
expect(myFunction()).toBeTruthy();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test('myFunction()', () => {
|
|
85
|
+
return new Promise(done => {
|
|
86
|
+
expect(myFunction()).toBeTruthy();
|
|
87
|
+
done();
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
```
|
package/docs/rules/no-export.md
CHANGED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Disallow string interpolation inside snapshots (`no-interpolation-in-snapshots`)
|
|
2
|
+
|
|
3
|
+
Prevents the use of string interpolations in snapshots.
|
|
4
|
+
|
|
5
|
+
## Rule Details
|
|
6
|
+
|
|
7
|
+
Interpolation prevents snapshots from being updated. Instead, properties should
|
|
8
|
+
be overloaded with a matcher by using
|
|
9
|
+
[property matchers](https://jestjs.io/docs/en/snapshot-testing#property-matchers).
|
|
10
|
+
|
|
11
|
+
Examples of **incorrect** code for this rule:
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
expect(something).toMatchInlineSnapshot(
|
|
15
|
+
`Object {
|
|
16
|
+
property: ${interpolated}
|
|
17
|
+
}`,
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
expect(something).toMatchInlineSnapshot(
|
|
21
|
+
{ other: expect.any(Number) },
|
|
22
|
+
`Object {
|
|
23
|
+
other: Any<Number>,
|
|
24
|
+
property: ${interpolated}
|
|
25
|
+
}`,
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
expect(errorThrowingFunction).toThrowErrorMatchingInlineSnapshot(
|
|
29
|
+
`${interpolated}`,
|
|
30
|
+
);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Examples of **correct** code for this rule:
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
expect(something).toMatchInlineSnapshot();
|
|
37
|
+
|
|
38
|
+
expect(something).toMatchInlineSnapshot(
|
|
39
|
+
`Object {
|
|
40
|
+
property: 1
|
|
41
|
+
}`,
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
expect(something).toMatchInlineSnapshot(
|
|
45
|
+
{ property: expect.any(Date) },
|
|
46
|
+
`Object {
|
|
47
|
+
property: Any<Date>
|
|
48
|
+
}`,
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
expect(errorThrowingFunction).toThrowErrorMatchingInlineSnapshot();
|
|
52
|
+
|
|
53
|
+
expect(errorThrowingFunction).toThrowErrorMatchingInlineSnapshot(
|
|
54
|
+
`Error Message`,
|
|
55
|
+
);
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## When Not To Use It
|
|
59
|
+
|
|
60
|
+
Don't use this rule on non-jest test files.
|
|
@@ -122,8 +122,8 @@ If only `maxSize` is provided on options, the value of `maxSize` will be used to
|
|
|
122
122
|
both snapshot types (Inline and External).
|
|
123
123
|
|
|
124
124
|
Since `eslint-disable` comments are not preserved by Jest when updating
|
|
125
|
-
snapshots, you can use the `
|
|
126
|
-
|
|
125
|
+
snapshots, you can use the `allowedSnapshots` option to have specific snapshots
|
|
126
|
+
allowed regardless of their size.
|
|
127
127
|
|
|
128
128
|
This option takes a map, with the key being the absolute filepath to a snapshot
|
|
129
129
|
file, and the value an array of values made up of strings and regular
|
|
@@ -141,7 +141,7 @@ module.exports = {
|
|
|
141
141
|
'jest/no-large-snapshots': [
|
|
142
142
|
'error',
|
|
143
143
|
{
|
|
144
|
-
|
|
144
|
+
allowedSnapshots: {
|
|
145
145
|
'/path/to/file.js.snap': ['snapshot name 1', /a big snapshot \d+/],
|
|
146
146
|
},
|
|
147
147
|
},
|
|
@@ -161,7 +161,7 @@ module.exports = {
|
|
|
161
161
|
'jest/no-large-snapshots': [
|
|
162
162
|
'error',
|
|
163
163
|
{
|
|
164
|
-
|
|
164
|
+
allowedSnapshots: {
|
|
165
165
|
[path.resolve('test/__snapshots__/get.js.snap')]: ['full request'],
|
|
166
166
|
[path.resolve('test/__snapshots__/put.js.snap')]: ['full request'],
|
|
167
167
|
},
|
|
@@ -15,7 +15,7 @@ body.
|
|
|
15
15
|
|
|
16
16
|
// valid:
|
|
17
17
|
|
|
18
|
-
it('noop', function() {});
|
|
18
|
+
it('noop', function () {});
|
|
19
19
|
|
|
20
20
|
test('noop', () => {});
|
|
21
21
|
|
|
@@ -27,7 +27,7 @@ test('one', () => {
|
|
|
27
27
|
expect(1).toBe(1);
|
|
28
28
|
});
|
|
29
29
|
|
|
30
|
-
it('one', function() {
|
|
30
|
+
it('one', function () {
|
|
31
31
|
expect(1).toBe(1);
|
|
32
32
|
});
|
|
33
33
|
|
|
@@ -41,7 +41,7 @@ test('return an expect', () => {
|
|
|
41
41
|
return expect(1).toBe(1);
|
|
42
42
|
});
|
|
43
43
|
|
|
44
|
-
it('returning a promise', function() {
|
|
44
|
+
it('returning a promise', function () {
|
|
45
45
|
return new Promise(res => setTimeout(res, 100)).then(() => expect(1).toBe(1));
|
|
46
46
|
});
|
|
47
47
|
```
|
|
@@ -55,7 +55,7 @@ it(123, () => {});
|
|
|
55
55
|
describe(String(/.+/), () => {});
|
|
56
56
|
describe(myFunction, () => {});
|
|
57
57
|
xdescribe(myFunction, () => {});
|
|
58
|
-
describe(6, function() {});
|
|
58
|
+
describe(6, function () {});
|
|
59
59
|
```
|
|
60
60
|
|
|
61
61
|
Examples of **correct** code for this rule:
|
|
@@ -82,7 +82,7 @@ fdescribe('is a string', () => {});
|
|
|
82
82
|
describe(String(/.+/), () => {});
|
|
83
83
|
describe(myFunction, () => {});
|
|
84
84
|
xdescribe(myFunction, () => {});
|
|
85
|
-
describe(6, function() {});
|
|
85
|
+
describe(6, function () {});
|
|
86
86
|
```
|
|
87
87
|
|
|
88
88
|
**duplicatePrefix**
|
package/lib/globals.json
CHANGED
|
@@ -5,14 +5,9 @@
|
|
|
5
5
|
"beforeEach": false,
|
|
6
6
|
"describe": false,
|
|
7
7
|
"expect": false,
|
|
8
|
-
"fail": false,
|
|
9
8
|
"fit": false,
|
|
10
9
|
"it": false,
|
|
11
|
-
"jasmine": false,
|
|
12
10
|
"jest": false,
|
|
13
|
-
"pending": false,
|
|
14
|
-
"pit": false,
|
|
15
|
-
"require": false,
|
|
16
11
|
"test": false,
|
|
17
12
|
"xdescribe": false,
|
|
18
13
|
"xit": false,
|
package/lib/index.js
CHANGED
|
@@ -14,12 +14,6 @@ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj;
|
|
|
14
14
|
|
|
15
15
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
16
16
|
|
|
17
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
|
18
|
-
|
|
19
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
20
|
-
|
|
21
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
22
|
-
|
|
23
17
|
// copied from https://github.com/babel/babel/blob/d8da63c929f2d28c401571e2a43166678c555bc4/packages/babel-helpers/src/helpers.js#L602-L606
|
|
24
18
|
|
|
25
19
|
/* istanbul ignore next */
|
|
@@ -32,14 +26,14 @@ interopRequireDefault(require(moduleName)).default;
|
|
|
32
26
|
|
|
33
27
|
const rulesDir = (0, _path.join)(__dirname, 'rules');
|
|
34
28
|
const excludedFiles = ['__tests__', 'utils'];
|
|
35
|
-
const rules = (0, _fs.readdirSync)(rulesDir).map(rule => (0, _path.parse)(rule).name).filter(rule => !excludedFiles.includes(rule)).reduce((acc, curr) =>
|
|
29
|
+
const rules = (0, _fs.readdirSync)(rulesDir).map(rule => (0, _path.parse)(rule).name).filter(rule => !excludedFiles.includes(rule)).reduce((acc, curr) => ({ ...acc,
|
|
36
30
|
[curr]: importDefault((0, _path.join)(rulesDir, curr))
|
|
37
31
|
}), {});
|
|
38
|
-
const recommendedRules = Object.entries(rules).filter(([, rule]) => rule.meta.docs.recommended).reduce((acc, [name, rule]) =>
|
|
32
|
+
const recommendedRules = Object.entries(rules).filter(([, rule]) => rule.meta.docs.recommended).reduce((acc, [name, rule]) => ({ ...acc,
|
|
39
33
|
[`jest/${name}`]: rule.meta.docs.recommended
|
|
40
34
|
}), {});
|
|
41
|
-
const allRules = Object.
|
|
42
|
-
[`jest/${
|
|
35
|
+
const allRules = Object.entries(rules).filter(([, rule]) => !rule.meta.deprecated).reduce((acc, [name]) => ({ ...acc,
|
|
36
|
+
[`jest/${name}`]: 'error'
|
|
43
37
|
}), {});
|
|
44
38
|
|
|
45
39
|
const createConfig = rules => ({
|
|
@@ -13,7 +13,7 @@ var _default = (0, _utils.createRule)({
|
|
|
13
13
|
docs: {
|
|
14
14
|
description: 'Prevent calling `expect` conditionally',
|
|
15
15
|
category: 'Best Practices',
|
|
16
|
-
recommended:
|
|
16
|
+
recommended: 'error'
|
|
17
17
|
},
|
|
18
18
|
messages: {
|
|
19
19
|
conditionalExpect: 'Avoid calling `expect` conditionally`'
|
|
@@ -9,12 +9,6 @@ var _experimentalUtils = require("@typescript-eslint/experimental-utils");
|
|
|
9
9
|
|
|
10
10
|
var _utils = require("./utils");
|
|
11
11
|
|
|
12
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
|
13
|
-
|
|
14
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
15
|
-
|
|
16
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
17
|
-
|
|
18
12
|
let cachedJestVersion = null;
|
|
19
13
|
/** @internal */
|
|
20
14
|
|
|
@@ -39,7 +33,7 @@ const detectJestVersion = () => {
|
|
|
39
33
|
const [majorVersion] = jestPackageJson.version.split('.');
|
|
40
34
|
return cachedJestVersion = parseInt(majorVersion, 10);
|
|
41
35
|
}
|
|
42
|
-
} catch
|
|
36
|
+
} catch {}
|
|
43
37
|
|
|
44
38
|
throw new Error('Unable to detect Jest version - please ensure jest package is installed, or otherwise set version explicitly');
|
|
45
39
|
};
|
|
@@ -50,7 +44,7 @@ var _default = (0, _utils.createRule)({
|
|
|
50
44
|
docs: {
|
|
51
45
|
category: 'Best Practices',
|
|
52
46
|
description: 'Disallow use of deprecated functions',
|
|
53
|
-
recommended:
|
|
47
|
+
recommended: 'error'
|
|
54
48
|
},
|
|
55
49
|
messages: {
|
|
56
50
|
deprecatedFunction: '`{{ deprecation }}` has been deprecated in favor of `{{ replacement }}`'
|
|
@@ -62,23 +56,26 @@ var _default = (0, _utils.createRule)({
|
|
|
62
56
|
defaultOptions: [],
|
|
63
57
|
|
|
64
58
|
create(context) {
|
|
65
|
-
var
|
|
66
|
-
|
|
67
|
-
const jestVersion = ((
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
59
|
+
var _context$settings, _context$settings$jes;
|
|
60
|
+
|
|
61
|
+
const jestVersion = ((_context$settings = context.settings) === null || _context$settings === void 0 ? void 0 : (_context$settings$jes = _context$settings.jest) === null || _context$settings$jes === void 0 ? void 0 : _context$settings$jes.version) || detectJestVersion();
|
|
62
|
+
const deprecations = { ...(jestVersion >= 15 && {
|
|
63
|
+
'jest.resetModuleRegistry': 'jest.resetModules'
|
|
64
|
+
}),
|
|
65
|
+
...(jestVersion >= 17 && {
|
|
66
|
+
'jest.addMatchers': 'expect.extend'
|
|
67
|
+
}),
|
|
68
|
+
...(jestVersion >= 21 && {
|
|
69
|
+
'require.requireMock': 'jest.requireMock',
|
|
70
|
+
'require.requireActual': 'jest.requireActual'
|
|
71
|
+
}),
|
|
72
|
+
...(jestVersion >= 22 && {
|
|
73
|
+
'jest.runTimersToTime': 'jest.advanceTimersByTime'
|
|
74
|
+
}),
|
|
75
|
+
...(jestVersion >= 26 && {
|
|
76
|
+
'jest.genMockFromModule': 'jest.createMockFromModule'
|
|
77
|
+
})
|
|
78
|
+
};
|
|
82
79
|
return {
|
|
83
80
|
CallExpression(node) {
|
|
84
81
|
if (node.callee.type !== _experimentalUtils.AST_NODE_TYPES.MemberExpression) {
|
|
@@ -104,7 +101,6 @@ var _default = (0, _utils.createRule)({
|
|
|
104
101
|
node,
|
|
105
102
|
|
|
106
103
|
fix(fixer) {
|
|
107
|
-
// eslint-disable-next-line prefer-const
|
|
108
104
|
let [name, func] = replacement.split('.');
|
|
109
105
|
|
|
110
106
|
if (callee.property.type === _experimentalUtils.AST_NODE_TYPES.Literal) {
|
|
@@ -9,17 +9,29 @@ var _experimentalUtils = require("@typescript-eslint/experimental-utils");
|
|
|
9
9
|
|
|
10
10
|
var _utils = require("./utils");
|
|
11
11
|
|
|
12
|
+
const findCallbackArg = node => {
|
|
13
|
+
if ((0, _utils.isHook)(node) && node.arguments.length >= 1) {
|
|
14
|
+
return node.arguments[0];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if ((0, _utils.isTestCase)(node) && node.arguments.length >= 2) {
|
|
18
|
+
return node.arguments[1];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return null;
|
|
22
|
+
};
|
|
23
|
+
|
|
12
24
|
var _default = (0, _utils.createRule)({
|
|
13
25
|
name: __filename,
|
|
14
26
|
meta: {
|
|
15
27
|
docs: {
|
|
16
28
|
category: 'Best Practices',
|
|
17
|
-
description: 'Avoid using a callback in asynchronous tests',
|
|
29
|
+
description: 'Avoid using a callback in asynchronous tests and hooks',
|
|
18
30
|
recommended: 'error',
|
|
19
31
|
suggestion: true
|
|
20
32
|
},
|
|
21
33
|
messages: {
|
|
22
|
-
|
|
34
|
+
noDoneCallback: 'Return a Promise instead of relying on callback parameter',
|
|
23
35
|
suggestWrappingInPromise: 'Wrap in `new Promise({{ callback }} => ...`',
|
|
24
36
|
useAwaitInsteadOfCallback: 'Use await instead of callback in async functions'
|
|
25
37
|
},
|
|
@@ -31,13 +43,9 @@ var _default = (0, _utils.createRule)({
|
|
|
31
43
|
create(context) {
|
|
32
44
|
return {
|
|
33
45
|
CallExpression(node) {
|
|
34
|
-
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const [, callback] = node.arguments;
|
|
46
|
+
const callback = findCallbackArg(node);
|
|
39
47
|
|
|
40
|
-
if (!(0, _utils.isFunction)(callback) || callback.params.length !== 1) {
|
|
48
|
+
if (!callback || !(0, _utils.isFunction)(callback) || callback.params.length !== 1) {
|
|
41
49
|
return;
|
|
42
50
|
}
|
|
43
51
|
|
|
@@ -46,7 +54,7 @@ var _default = (0, _utils.createRule)({
|
|
|
46
54
|
if (argument.type !== _experimentalUtils.AST_NODE_TYPES.Identifier) {
|
|
47
55
|
context.report({
|
|
48
56
|
node: argument,
|
|
49
|
-
messageId: '
|
|
57
|
+
messageId: 'noDoneCallback'
|
|
50
58
|
});
|
|
51
59
|
return;
|
|
52
60
|
}
|
|
@@ -61,7 +69,7 @@ var _default = (0, _utils.createRule)({
|
|
|
61
69
|
|
|
62
70
|
context.report({
|
|
63
71
|
node: argument,
|
|
64
|
-
messageId: '
|
|
72
|
+
messageId: 'noDoneCallback',
|
|
65
73
|
suggest: [{
|
|
66
74
|
messageId: 'suggestWrappingInPromise',
|
|
67
75
|
data: {
|
|
@@ -72,12 +80,6 @@ var _default = (0, _utils.createRule)({
|
|
|
72
80
|
const {
|
|
73
81
|
body
|
|
74
82
|
} = callback;
|
|
75
|
-
/* istanbul ignore if https://github.com/typescript-eslint/typescript-eslint/issues/734 */
|
|
76
|
-
|
|
77
|
-
if (!body) {
|
|
78
|
-
throw new Error(`Unexpected null when attempting to fix ${context.getFilename()} - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
83
|
const sourceCode = context.getSourceCode();
|
|
82
84
|
const firstBodyToken = sourceCode.getFirstToken(body);
|
|
83
85
|
const lastBodyToken = sourceCode.getLastToken(body);
|
|
@@ -14,7 +14,7 @@ const testFunctions = new Set([_utils.DescribeAlias.describe, ...validTestCaseNa
|
|
|
14
14
|
|
|
15
15
|
const isConcurrentExpression = expression => (0, _utils.isSupportedAccessor)(expression.property, _utils.TestCaseProperty.concurrent) && !!expression.parent && expression.parent.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression;
|
|
16
16
|
|
|
17
|
-
const matchesTestFunction = object => 'name' in object && (object.name in _utils.TestCaseName || object.name in _utils.DescribeAlias);
|
|
17
|
+
const matchesTestFunction = object => 'name' in object && typeof object.name === 'string' && (object.name in _utils.TestCaseName || object.name in _utils.DescribeAlias);
|
|
18
18
|
|
|
19
19
|
const isCallToFocusedTestFunction = object => object.name.startsWith('f') && testFunctions.has(object.name.substring(1));
|
|
20
20
|
|
|
@@ -0,0 +1,63 @@
|
|
|
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
|
+
var _default = (0, _utils.createRule)({
|
|
13
|
+
name: __filename,
|
|
14
|
+
meta: {
|
|
15
|
+
docs: {
|
|
16
|
+
category: 'Best Practices',
|
|
17
|
+
description: 'Disallow string interpolation inside snapshots',
|
|
18
|
+
recommended: 'error'
|
|
19
|
+
},
|
|
20
|
+
messages: {
|
|
21
|
+
noInterpolation: 'Do not use string interpolation inside of snapshots'
|
|
22
|
+
},
|
|
23
|
+
schema: [],
|
|
24
|
+
type: 'problem'
|
|
25
|
+
},
|
|
26
|
+
defaultOptions: [],
|
|
27
|
+
|
|
28
|
+
create(context) {
|
|
29
|
+
return {
|
|
30
|
+
CallExpression(node) {
|
|
31
|
+
if (!(0, _utils.isExpectCall)(node)) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const {
|
|
36
|
+
matcher
|
|
37
|
+
} = (0, _utils.parseExpectCall)(node);
|
|
38
|
+
|
|
39
|
+
if (!matcher) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (['toMatchInlineSnapshot', 'toThrowErrorMatchingInlineSnapshot'].includes(matcher.name)) {
|
|
44
|
+
var _matcher$arguments;
|
|
45
|
+
|
|
46
|
+
// Check all since the optional 'propertyMatchers' argument might be present
|
|
47
|
+
(_matcher$arguments = matcher.arguments) === null || _matcher$arguments === void 0 ? void 0 : _matcher$arguments.forEach(argument => {
|
|
48
|
+
if (argument.type === _experimentalUtils.AST_NODE_TYPES.TemplateLiteral && argument.expressions.length > 0) {
|
|
49
|
+
context.report({
|
|
50
|
+
messageId: 'noInterpolation',
|
|
51
|
+
node: argument
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
exports.default = _default;
|
|
@@ -15,7 +15,7 @@ var _default = (0, _utils.createRule)({
|
|
|
15
15
|
docs: {
|
|
16
16
|
category: 'Best Practices',
|
|
17
17
|
description: 'Disallow Jasmine globals',
|
|
18
|
-
recommended: '
|
|
18
|
+
recommended: 'error'
|
|
19
19
|
},
|
|
20
20
|
messages: {
|
|
21
21
|
illegalGlobal: 'Illegal usage of global `{{ global }}`, prefer `{{ replacement }}`',
|
|
@@ -11,34 +11,28 @@ var _experimentalUtils = require("@typescript-eslint/experimental-utils");
|
|
|
11
11
|
|
|
12
12
|
var _utils = require("./utils");
|
|
13
13
|
|
|
14
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
|
15
|
-
|
|
16
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
17
|
-
|
|
18
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
19
|
-
|
|
20
14
|
const reportOnViolation = (context, node, {
|
|
21
15
|
maxSize: lineLimit = 50,
|
|
22
|
-
|
|
16
|
+
allowedSnapshots = {}
|
|
23
17
|
}) => {
|
|
24
18
|
const startLine = node.loc.start.line;
|
|
25
19
|
const endLine = node.loc.end.line;
|
|
26
20
|
const lineCount = endLine - startLine;
|
|
27
|
-
const allPathsAreAbsolute = Object.keys(
|
|
21
|
+
const allPathsAreAbsolute = Object.keys(allowedSnapshots).every(_path.isAbsolute);
|
|
28
22
|
|
|
29
23
|
if (!allPathsAreAbsolute) {
|
|
30
|
-
throw new Error('All paths for
|
|
24
|
+
throw new Error('All paths for allowedSnapshots must be absolute. You can use JS config and `path.resolve`');
|
|
31
25
|
}
|
|
32
26
|
|
|
33
|
-
let
|
|
27
|
+
let isAllowed = false;
|
|
34
28
|
|
|
35
29
|
if (node.type === _experimentalUtils.AST_NODE_TYPES.ExpressionStatement && 'left' in node.expression && (0, _utils.isExpectMember)(node.expression.left)) {
|
|
36
30
|
const fileName = context.getFilename();
|
|
37
|
-
const
|
|
31
|
+
const allowedSnapshotsInFile = allowedSnapshots[fileName];
|
|
38
32
|
|
|
39
|
-
if (
|
|
33
|
+
if (allowedSnapshotsInFile) {
|
|
40
34
|
const snapshotName = (0, _utils.getAccessorValue)(node.expression.left.property);
|
|
41
|
-
|
|
35
|
+
isAllowed = allowedSnapshotsInFile.some(name => {
|
|
42
36
|
if (name instanceof RegExp) {
|
|
43
37
|
return name.test(snapshotName);
|
|
44
38
|
}
|
|
@@ -48,7 +42,7 @@ const reportOnViolation = (context, node, {
|
|
|
48
42
|
}
|
|
49
43
|
}
|
|
50
44
|
|
|
51
|
-
if (!
|
|
45
|
+
if (!isAllowed && lineCount > lineLimit) {
|
|
52
46
|
context.report({
|
|
53
47
|
messageId: lineLimit === 0 ? 'noSnapshot' : 'tooLongSnapshots',
|
|
54
48
|
data: {
|
|
@@ -82,12 +76,10 @@ var _default = (0, _utils.createRule)({
|
|
|
82
76
|
inlineMaxSize: {
|
|
83
77
|
type: 'number'
|
|
84
78
|
},
|
|
85
|
-
|
|
79
|
+
allowedSnapshots: {
|
|
86
80
|
type: 'object',
|
|
87
|
-
|
|
88
|
-
'
|
|
89
|
-
type: 'array'
|
|
90
|
-
}
|
|
81
|
+
additionalProperties: {
|
|
82
|
+
type: 'array'
|
|
91
83
|
}
|
|
92
84
|
}
|
|
93
85
|
},
|
|
@@ -104,22 +96,34 @@ var _default = (0, _utils.createRule)({
|
|
|
104
96
|
}
|
|
105
97
|
|
|
106
98
|
};
|
|
107
|
-
}
|
|
108
|
-
return {
|
|
109
|
-
CallExpression(node) {
|
|
110
|
-
if ('property' in node.callee && ((0, _utils.isSupportedAccessor)(node.callee.property, 'toMatchInlineSnapshot') || (0, _utils.isSupportedAccessor)(node.callee.property, 'toThrowErrorMatchingInlineSnapshot'))) {
|
|
111
|
-
var _options$inlineMaxSiz;
|
|
99
|
+
}
|
|
112
100
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
101
|
+
return {
|
|
102
|
+
CallExpression(node) {
|
|
103
|
+
var _matcher$node$parent;
|
|
104
|
+
|
|
105
|
+
if (!(0, _utils.isExpectCall)(node)) {
|
|
106
|
+
return;
|
|
117
107
|
}
|
|
118
108
|
|
|
119
|
-
|
|
120
|
-
|
|
109
|
+
const {
|
|
110
|
+
matcher
|
|
111
|
+
} = (0, _utils.parseExpectCall)(node);
|
|
112
|
+
|
|
113
|
+
if ((matcher === null || matcher === void 0 ? void 0 : (_matcher$node$parent = matcher.node.parent) === null || _matcher$node$parent === void 0 ? void 0 : _matcher$node$parent.type) !== _experimentalUtils.AST_NODE_TYPES.CallExpression) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (['toMatchInlineSnapshot', 'toThrowErrorMatchingInlineSnapshot'].includes(matcher.name)) {
|
|
118
|
+
var _options$inlineMaxSiz;
|
|
119
|
+
|
|
120
|
+
reportOnViolation(context, matcher.node.parent, { ...options,
|
|
121
|
+
maxSize: (_options$inlineMaxSiz = options.inlineMaxSize) !== null && _options$inlineMaxSiz !== void 0 ? _options$inlineMaxSiz : options.maxSize
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
121
125
|
|
|
122
|
-
|
|
126
|
+
};
|
|
123
127
|
}
|
|
124
128
|
|
|
125
129
|
});
|
|
@@ -12,7 +12,7 @@ var _utils = require("./utils");
|
|
|
12
12
|
const getBody = args => {
|
|
13
13
|
const [, secondArg] = args;
|
|
14
14
|
|
|
15
|
-
if (secondArg && (0, _utils.isFunction)(secondArg) && secondArg.body
|
|
15
|
+
if (secondArg && (0, _utils.isFunction)(secondArg) && secondArg.body.type === _experimentalUtils.AST_NODE_TYPES.BlockStatement) {
|
|
16
16
|
return secondArg.body.body;
|
|
17
17
|
}
|
|
18
18
|
|
|
@@ -40,7 +40,7 @@ var _default = (0, _utils.createRule)({
|
|
|
40
40
|
matcher
|
|
41
41
|
} = (0, _utils.parseExpectCall)(node);
|
|
42
42
|
|
|
43
|
-
if (!matcher || !(0, _utils.isParsedEqualityMatcherCall)(matcher) ||
|
|
43
|
+
if (!matcher || !(0, _utils.isParsedEqualityMatcherCall)(matcher) || (argument === null || argument === void 0 ? void 0 : argument.type) !== _experimentalUtils.AST_NODE_TYPES.MemberExpression || !(0, _utils.isSupportedAccessor)(argument.property, 'length') || argument.property.type !== _experimentalUtils.AST_NODE_TYPES.Identifier) {
|
|
44
44
|
return;
|
|
45
45
|
}
|
|
46
46
|
|
package/lib/rules/prefer-todo.js
CHANGED
|
@@ -13,14 +13,8 @@ function isEmptyFunction(node) {
|
|
|
13
13
|
if (!(0, _utils.isFunction)(node)) {
|
|
14
14
|
return false;
|
|
15
15
|
}
|
|
16
|
-
/* istanbul ignore if https://github.com/typescript-eslint/typescript-eslint/issues/734 */
|
|
17
16
|
|
|
18
|
-
|
|
19
|
-
if (!node.body) {
|
|
20
|
-
throw new Error(`Unexpected null while performing prefer-todo - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return node.body.type === _experimentalUtils.AST_NODE_TYPES.BlockStatement && node.body.body && !node.body.body.length;
|
|
17
|
+
return node.body.type === _experimentalUtils.AST_NODE_TYPES.BlockStatement && !node.body.body.length;
|
|
24
18
|
}
|
|
25
19
|
|
|
26
20
|
function createTodoFixer(node, fixer) {
|
|
@@ -26,6 +26,8 @@ var _default = (0, _utils.createRule)({
|
|
|
26
26
|
create(context) {
|
|
27
27
|
return {
|
|
28
28
|
CallExpression(node) {
|
|
29
|
+
var _matcher$arguments;
|
|
30
|
+
|
|
29
31
|
if (!(0, _utils.isExpectCall)(node)) {
|
|
30
32
|
return;
|
|
31
33
|
}
|
|
@@ -35,7 +37,7 @@ var _default = (0, _utils.createRule)({
|
|
|
35
37
|
modifier
|
|
36
38
|
} = (0, _utils.parseExpectCall)(node);
|
|
37
39
|
|
|
38
|
-
if (matcher
|
|
40
|
+
if ((matcher === null || matcher === void 0 ? void 0 : (_matcher$arguments = matcher.arguments) === null || _matcher$arguments === void 0 ? void 0 : _matcher$arguments.length) === 0 && ['toThrow', 'toThrowError'].includes(matcher.name) && (!modifier || !(modifier.name === _utils.ModifierName.not || modifier.negation))) {
|
|
39
41
|
// Look for `toThrow` calls with no arguments.
|
|
40
42
|
context.report({
|
|
41
43
|
messageId: 'addErrorMessage',
|
package/lib/rules/utils.js
CHANGED
|
@@ -12,12 +12,6 @@ var _experimentalUtils = require("@typescript-eslint/experimental-utils");
|
|
|
12
12
|
|
|
13
13
|
var _package = require("../../package.json");
|
|
14
14
|
|
|
15
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
|
16
|
-
|
|
17
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
18
|
-
|
|
19
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
20
|
-
|
|
21
15
|
const REPO_URL = 'https://github.com/jest-community/eslint-plugin-jest';
|
|
22
16
|
|
|
23
17
|
const createRule = _experimentalUtils.ESLintUtils.RuleCreator(name => {
|
|
@@ -231,7 +225,8 @@ const parseExpectMember = expectMember => ({
|
|
|
231
225
|
node: expectMember
|
|
232
226
|
});
|
|
233
227
|
|
|
234
|
-
const reparseAsMatcher = parsedMember =>
|
|
228
|
+
const reparseAsMatcher = parsedMember => ({ ...parsedMember,
|
|
229
|
+
|
|
235
230
|
/**
|
|
236
231
|
* The arguments being passed to this `Matcher`, if any.
|
|
237
232
|
*
|
|
@@ -265,9 +260,9 @@ const reparseMemberAsModifier = parsedMember => {
|
|
|
265
260
|
}
|
|
266
261
|
|
|
267
262
|
const negation = parsedMember.node.parent && isExpectMember(parsedMember.node.parent, ModifierName.not) ? parsedMember.node.parent : undefined;
|
|
268
|
-
return
|
|
263
|
+
return { ...parsedMember,
|
|
269
264
|
negation
|
|
270
|
-
}
|
|
265
|
+
};
|
|
271
266
|
};
|
|
272
267
|
|
|
273
268
|
const isSpecificMember = (member, specific) => member.name === specific;
|
|
@@ -86,7 +86,7 @@ var _default = (0, _utils.createRule)({
|
|
|
86
86
|
});
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
-
if (callback.body
|
|
89
|
+
if (callback.body.type === _experimentalUtils.AST_NODE_TYPES.BlockStatement) {
|
|
90
90
|
callback.body.body.forEach(node => {
|
|
91
91
|
if (node.type === _experimentalUtils.AST_NODE_TYPES.ReturnStatement) {
|
|
92
92
|
context.report({
|
|
@@ -76,8 +76,8 @@ const isParentThenOrPromiseReturned = (node, testFunctionBody) => node.type ===
|
|
|
76
76
|
|
|
77
77
|
const verifyExpectWithReturn = (promiseCallbacks, node, context, testFunctionBody) => {
|
|
78
78
|
promiseCallbacks.some(promiseCallback => {
|
|
79
|
-
if (promiseCallback && (0, _utils.isFunction)(promiseCallback)
|
|
80
|
-
if (isExpectCallPresentInFunction(promiseCallback.body) && node.parent
|
|
79
|
+
if (promiseCallback && (0, _utils.isFunction)(promiseCallback)) {
|
|
80
|
+
if (isExpectCallPresentInFunction(promiseCallback.body) && node.parent.parent && !isParentThenOrPromiseReturned(node.parent.parent, testFunctionBody)) {
|
|
81
81
|
reportReturnRequired(context, node.parent.parent);
|
|
82
82
|
return true;
|
|
83
83
|
}
|
|
@@ -118,11 +118,6 @@ var _default = (0, _utils.createRule)({
|
|
|
118
118
|
const {
|
|
119
119
|
body
|
|
120
120
|
} = testFunction;
|
|
121
|
-
/* istanbul ignore if https://github.com/typescript-eslint/typescript-eslint/issues/734 */
|
|
122
|
-
|
|
123
|
-
if (!body) {
|
|
124
|
-
throw new Error(`Unexpected null when attempting to fix ${context.getFilename()} - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`);
|
|
125
|
-
}
|
|
126
121
|
|
|
127
122
|
if (body.type !== _experimentalUtils.AST_NODE_TYPES.BlockStatement) {
|
|
128
123
|
return;
|
|
@@ -190,7 +190,7 @@ var _default = (0, _utils.createRule)({
|
|
|
190
190
|
return;
|
|
191
191
|
}
|
|
192
192
|
|
|
193
|
-
if (
|
|
193
|
+
if ((0, _utils.isExpectMember)(matcher.node.parent)) {
|
|
194
194
|
context.report({
|
|
195
195
|
messageId: 'modifierUnknown',
|
|
196
196
|
data: {
|
|
@@ -210,7 +210,7 @@ var _default = (0, _utils.createRule)({
|
|
|
210
210
|
|
|
211
211
|
const parentNode = matcher.node.parent;
|
|
212
212
|
|
|
213
|
-
if (!
|
|
213
|
+
if (!parentNode.parent || !modifier || modifier.name === _utils.ModifierName.not) {
|
|
214
214
|
return;
|
|
215
215
|
}
|
|
216
216
|
/**
|
package/lib/rules/valid-title.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-jest",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "24.0.1",
|
|
4
4
|
"description": "Eslint rules for Jest",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -20,11 +20,11 @@
|
|
|
20
20
|
"lib/"
|
|
21
21
|
],
|
|
22
22
|
"scripts": {
|
|
23
|
-
"build": "babel --extensions .js,.ts src --out-dir lib --copy-files",
|
|
24
|
-
"postbuild": "rimraf lib/__tests__ lib/**/__tests__",
|
|
23
|
+
"build": "babel --extensions .js,.ts src --out-dir lib --copy-files && rimraf lib/__tests__ 'lib/**/__tests__'",
|
|
25
24
|
"lint": "eslint . --ignore-pattern '!.eslintrc.js' --ext js,ts",
|
|
26
|
-
"prepack": "yarn build",
|
|
27
|
-
"
|
|
25
|
+
"prepack": "rimraf lib && yarn build",
|
|
26
|
+
"prettier:check": "prettier --check 'docs/**/*.md' README.md '.github/**' package.json tsconfig.json src/globals.json .yarnrc.yml",
|
|
27
|
+
"prettier:write": "prettier --write 'docs/**/*.md' README.md '.github/**' package.json tsconfig.json src/globals.json .yarnrc.yml",
|
|
28
28
|
"test": "jest",
|
|
29
29
|
"tools:regenerate-docs": "ts-node -T tools/regenerate-docs",
|
|
30
30
|
"typecheck": "tsc -p ."
|
|
@@ -41,14 +41,8 @@
|
|
|
41
41
|
]
|
|
42
42
|
},
|
|
43
43
|
"lint-staged": {
|
|
44
|
-
"*.{js,ts}":
|
|
45
|
-
|
|
46
|
-
"git add"
|
|
47
|
-
],
|
|
48
|
-
"*.{md,json}": [
|
|
49
|
-
"prettier --write",
|
|
50
|
-
"git add"
|
|
51
|
-
]
|
|
44
|
+
"*.{js,ts}": "eslint --fix",
|
|
45
|
+
"*.{md,json,yml}": "prettier --write"
|
|
52
46
|
},
|
|
53
47
|
"prettier": {
|
|
54
48
|
"arrowParens": "avoid",
|
|
@@ -87,28 +81,28 @@
|
|
|
87
81
|
]
|
|
88
82
|
},
|
|
89
83
|
"dependencies": {
|
|
90
|
-
"@typescript-eslint/experimental-utils": "^
|
|
84
|
+
"@typescript-eslint/experimental-utils": "^4.0.1"
|
|
91
85
|
},
|
|
92
86
|
"devDependencies": {
|
|
93
87
|
"@babel/cli": "^7.4.4",
|
|
94
88
|
"@babel/core": "^7.4.4",
|
|
95
89
|
"@babel/preset-env": "^7.4.4",
|
|
96
90
|
"@babel/preset-typescript": "^7.3.3",
|
|
97
|
-
"@commitlint/cli": "^
|
|
98
|
-
"@commitlint/config-conventional": "^
|
|
91
|
+
"@commitlint/cli": "^9.1.1",
|
|
92
|
+
"@commitlint/config-conventional": "^9.1.1",
|
|
99
93
|
"@schemastore/package": "^0.0.6",
|
|
100
|
-
"@semantic-release/changelog": "^
|
|
101
|
-
"@semantic-release/git": "^
|
|
94
|
+
"@semantic-release/changelog": "^5.0.1",
|
|
95
|
+
"@semantic-release/git": "^9.0.0",
|
|
102
96
|
"@types/dedent": "^0.7.0",
|
|
103
|
-
"@types/jest": "^
|
|
104
|
-
"@types/node": "^
|
|
105
|
-
"@types/prettier": "^
|
|
106
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
107
|
-
"@typescript-eslint/parser": "^
|
|
108
|
-
"babel-jest": "^
|
|
97
|
+
"@types/jest": "^26.0.0",
|
|
98
|
+
"@types/node": "^14.0.0",
|
|
99
|
+
"@types/prettier": "^2.0.0",
|
|
100
|
+
"@typescript-eslint/eslint-plugin": "^4.0.1",
|
|
101
|
+
"@typescript-eslint/parser": "^4.0.1",
|
|
102
|
+
"babel-jest": "^26.0.1",
|
|
109
103
|
"babel-plugin-replace-ts-export-assignment": "^0.0.2",
|
|
110
104
|
"dedent": "^0.7.0",
|
|
111
|
-
"eslint": "^5.1.0 || ^6.0.0",
|
|
105
|
+
"eslint": "^5.1.0 || ^6.0.0 || ^7.0.0",
|
|
112
106
|
"eslint-config-prettier": "^6.5.0",
|
|
113
107
|
"eslint-plugin-eslint-comments": "^3.1.2",
|
|
114
108
|
"eslint-plugin-eslint-config": "^1.0.2",
|
|
@@ -116,23 +110,22 @@
|
|
|
116
110
|
"eslint-plugin-import": "^2.20.2",
|
|
117
111
|
"eslint-plugin-node": "^11.0.0",
|
|
118
112
|
"eslint-plugin-prettier": "^3.0.0",
|
|
119
|
-
"husky": "^
|
|
120
|
-
"jest": "^
|
|
113
|
+
"husky": "^4.2.5",
|
|
114
|
+
"jest": "^26.0.1",
|
|
121
115
|
"jest-runner-eslint": "^0.10.0",
|
|
122
|
-
"lint-staged": "^
|
|
123
|
-
"prettier": "^
|
|
124
|
-
"prettylint": "^1.0.0",
|
|
116
|
+
"lint-staged": "^10.2.2",
|
|
117
|
+
"prettier": "^2.0.5",
|
|
125
118
|
"resolve-from": "^5.0.0",
|
|
126
119
|
"rimraf": "^3.0.0",
|
|
127
|
-
"semantic-release": "^
|
|
128
|
-
"ts-node": "^
|
|
129
|
-
"typescript": "^
|
|
120
|
+
"semantic-release": "^17.0.7",
|
|
121
|
+
"ts-node": "^9.0.0",
|
|
122
|
+
"typescript": "^4.0.0"
|
|
130
123
|
},
|
|
131
124
|
"peerDependencies": {
|
|
132
125
|
"eslint": ">=5"
|
|
133
126
|
},
|
|
134
127
|
"engines": {
|
|
135
|
-
"node": ">=
|
|
128
|
+
"node": ">=10"
|
|
136
129
|
},
|
|
137
130
|
"release": {
|
|
138
131
|
"plugins": [
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
# Avoid using a callback in asynchronous tests (`no-test-callback`)
|
|
2
|
-
|
|
3
|
-
Jest allows you to pass a callback to test definitions, typically called `done`,
|
|
4
|
-
that is later invoked to indicate that the asynchronous test is complete.
|
|
5
|
-
|
|
6
|
-
However, that means that if your test throws (e.g. because of a failing
|
|
7
|
-
assertion), `done` will never be called unless you manually use `try-catch`.
|
|
8
|
-
|
|
9
|
-
```js
|
|
10
|
-
test('some test', done => {
|
|
11
|
-
expect(false).toBe(true);
|
|
12
|
-
done();
|
|
13
|
-
});
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
The test above will time out instead of failing the assertions, since `done` is
|
|
17
|
-
never called.
|
|
18
|
-
|
|
19
|
-
Correct way of doing the same thing is to wrap it in `try-catch`.
|
|
20
|
-
|
|
21
|
-
```js
|
|
22
|
-
test('some test', done => {
|
|
23
|
-
try {
|
|
24
|
-
expect(false).toBe(true);
|
|
25
|
-
done();
|
|
26
|
-
} catch (e) {
|
|
27
|
-
done(e);
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
However, Jest supports a second way of having asynchronous tests - using
|
|
33
|
-
promises.
|
|
34
|
-
|
|
35
|
-
```js
|
|
36
|
-
test('some test', () => {
|
|
37
|
-
return new Promise(done => {
|
|
38
|
-
expect(false).toBe(true);
|
|
39
|
-
done();
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
Even though `done` is never called here, the Promise will still reject, and Jest
|
|
45
|
-
will report the assertion error correctly.
|
|
46
|
-
|
|
47
|
-
## Rule details
|
|
48
|
-
|
|
49
|
-
This rule triggers a warning if you have a `done` callback in your test.
|
|
50
|
-
|
|
51
|
-
The following patterns are considered warnings:
|
|
52
|
-
|
|
53
|
-
```js
|
|
54
|
-
test('myFunction()', done => {
|
|
55
|
-
// ...
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
test('myFunction()', function(done) {
|
|
59
|
-
// ...
|
|
60
|
-
});
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
The following patterns are not considered warnings:
|
|
64
|
-
|
|
65
|
-
```js
|
|
66
|
-
test('myFunction()', () => {
|
|
67
|
-
expect(myFunction()).toBeTruthy();
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
test('myFunction()', () => {
|
|
71
|
-
return new Promise(done => {
|
|
72
|
-
expect(myFunction()).toBeTruthy();
|
|
73
|
-
done();
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
```
|