eslint-plugin-jest 24.4.3 → 24.6.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 CHANGED
@@ -1,3 +1,40 @@
1
+ # [24.6.0](https://github.com/jest-community/eslint-plugin-jest/compare/v24.5.2...v24.6.0) (2021-10-09)
2
+
3
+
4
+ ### Features
5
+
6
+ * **valid-expect-in-promise:** re-implement rule ([#916](https://github.com/jest-community/eslint-plugin-jest/issues/916)) ([7a49c58](https://github.com/jest-community/eslint-plugin-jest/commit/7a49c5831e3d85a60c11e385203b8f83d98ad580))
7
+
8
+ ## [24.5.2](https://github.com/jest-community/eslint-plugin-jest/compare/v24.5.1...v24.5.2) (2021-10-04)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * **lowercase-name:** consider skip and only prefixes for ignores ([#923](https://github.com/jest-community/eslint-plugin-jest/issues/923)) ([8716c24](https://github.com/jest-community/eslint-plugin-jest/commit/8716c24678ea7dc7c9f692b573d1ea19a67efd84))
14
+
15
+ ## [24.5.1](https://github.com/jest-community/eslint-plugin-jest/compare/v24.5.0...v24.5.1) (2021-10-04)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * **prefer-to-be:** don't consider RegExp literals as `toBe`-able ([#922](https://github.com/jest-community/eslint-plugin-jest/issues/922)) ([99b6d42](https://github.com/jest-community/eslint-plugin-jest/commit/99b6d429e697d60645b4bc64ee4ae34d7016118c))
21
+
22
+ # [24.5.0](https://github.com/jest-community/eslint-plugin-jest/compare/v24.4.3...v24.5.0) (2021-09-29)
23
+
24
+
25
+ ### Bug Fixes
26
+
27
+ * **no-deprecated-functions:** remove `process.cwd` from resolve paths ([#889](https://github.com/jest-community/eslint-plugin-jest/issues/889)) ([6940488](https://github.com/jest-community/eslint-plugin-jest/commit/6940488d7b5a47577e2823e6d4385b511c5becf4))
28
+ * **no-identical-title:** always consider `.each` titles unique ([#910](https://github.com/jest-community/eslint-plugin-jest/issues/910)) ([a41a40e](https://github.com/jest-community/eslint-plugin-jest/commit/a41a40eafaf1db444ba940cccd2014cb0dc41be9))
29
+
30
+
31
+ ### Features
32
+
33
+ * create `prefer-expect-resolves` rule ([#822](https://github.com/jest-community/eslint-plugin-jest/issues/822)) ([2556020](https://github.com/jest-community/eslint-plugin-jest/commit/2556020a777f9daaf1d362a04e3f990415e82db8))
34
+ * create `prefer-to-be` rule ([#864](https://github.com/jest-community/eslint-plugin-jest/issues/864)) ([3a64aea](https://github.com/jest-community/eslint-plugin-jest/commit/3a64aea5bdc55465f1ef34f1426ae626d6c8a230))
35
+ * **require-top-level-describe:** support enforcing max num of describes ([#912](https://github.com/jest-community/eslint-plugin-jest/issues/912)) ([14a2d13](https://github.com/jest-community/eslint-plugin-jest/commit/14a2d1391c9f6f52509316542f45df35853c9b79))
36
+ * **valid-title:** allow custom matcher messages ([#913](https://github.com/jest-community/eslint-plugin-jest/issues/913)) ([ffc9392](https://github.com/jest-community/eslint-plugin-jest/commit/ffc93921348b0d4a394125f665d2bb09148ea37e))
37
+
1
38
  ## [24.4.3](https://github.com/jest-community/eslint-plugin-jest/compare/v24.4.2...v24.4.3) (2021-09-28)
2
39
 
3
40
 
package/README.md CHANGED
@@ -59,23 +59,43 @@ doing:
59
59
  This is included in all configs shared by this plugin, so can be omitted if
60
60
  extending them.
61
61
 
62
- The behaviour of some rules (specifically `no-deprecated-functions`) change
63
- depending on the version of `jest` being used.
62
+ ### Jest `version` setting
64
63
 
65
- This setting is detected automatically based off the version of the `jest`
66
- package installed in `node_modules`, but it can also be provided explicitly if
67
- desired:
64
+ The behaviour of some rules (specifically [`no-deprecated-functions`][]) change
65
+ depending on the version of Jest being used.
66
+
67
+ By default, this plugin will attempt to determine to locate Jest using
68
+ `require.resolve`, meaning it will start looking in the closest `node_modules`
69
+ folder to the file being linted and work its way up.
70
+
71
+ Since we cache the automatically determined version, if you're linting
72
+ sub-folders that have different versions of Jest, you may find that the wrong
73
+ version of Jest is considered when linting. You can work around this by
74
+ providing the Jest version explicitly in nested ESLint configs:
68
75
 
69
76
  ```json
70
77
  {
71
78
  "settings": {
72
79
  "jest": {
73
- "version": 26
80
+ "version": 27
74
81
  }
75
82
  }
76
83
  }
77
84
  ```
78
85
 
86
+ To avoid hard-coding a number, you can also fetch it from the installed version
87
+ of Jest if you use a JavaScript config file such as `.eslintrc.js`:
88
+
89
+ ```js
90
+ module.exports = {
91
+ settings: {
92
+ jest: {
93
+ version: require('jest/package.json').version,
94
+ },
95
+ },
96
+ };
97
+ ```
98
+
79
99
  ## Shareable configurations
80
100
 
81
101
  ### Recommended
@@ -130,49 +150,51 @@ installations requiring long-term consistency.
130
150
 
131
151
  <!-- begin base rules list -->
132
152
 
133
- | Rule | Description | Configurations | Fixable |
134
- | ---------------------------------------------------------------------------- | --------------------------------------------------------------- | ---------------- | ------------ |
135
- | [consistent-test-it](docs/rules/consistent-test-it.md) | Have control over `test` and `it` usages | | ![fixable][] |
136
- | [expect-expect](docs/rules/expect-expect.md) | Enforce assertion to be made in a test body | ![recommended][] | |
137
- | [lowercase-name](docs/rules/lowercase-name.md) | Enforce lowercase test names | | ![fixable][] |
138
- | [max-nested-describe](docs/rules/max-nested-describe.md) | Enforces a maximum depth to nested describe calls | | |
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][] | ![suggest][] |
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][] |
153
+ | Rule | Description | Configurations | Fixable |
154
+ | ---------------------------------------------------------------------------- | ------------------------------------------------------------------- | ---------------- | ------------ |
155
+ | [consistent-test-it](docs/rules/consistent-test-it.md) | Have control over `test` and `it` usages | | ![fixable][] |
156
+ | [expect-expect](docs/rules/expect-expect.md) | Enforce assertion to be made in a test body | ![recommended][] | |
157
+ | [lowercase-name](docs/rules/lowercase-name.md) | Enforce lowercase test names | | ![fixable][] |
158
+ | [max-nested-describe](docs/rules/max-nested-describe.md) | Enforces a maximum depth to nested describe calls | | |
159
+ | [no-alias-methods](docs/rules/no-alias-methods.md) | Disallow alias methods | ![style][] | ![fixable][] |
160
+ | [no-commented-out-tests](docs/rules/no-commented-out-tests.md) | Disallow commented out tests | ![recommended][] | |
161
+ | [no-conditional-expect](docs/rules/no-conditional-expect.md) | Prevent calling `expect` conditionally | ![recommended][] | |
162
+ | [no-deprecated-functions](docs/rules/no-deprecated-functions.md) | Disallow use of deprecated functions | ![recommended][] | ![fixable][] |
163
+ | [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended][] | |
164
+ | [no-done-callback](docs/rules/no-done-callback.md) | Avoid using a callback in asynchronous tests and hooks | ![recommended][] | ![suggest][] |
165
+ | [no-duplicate-hooks](docs/rules/no-duplicate-hooks.md) | Disallow duplicate setup and teardown hooks | | |
166
+ | [no-export](docs/rules/no-export.md) | Disallow using `exports` in files containing tests | ![recommended][] | |
167
+ | [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ![recommended][] | ![suggest][] |
168
+ | [no-hooks](docs/rules/no-hooks.md) | Disallow setup and teardown hooks | | |
169
+ | [no-identical-title](docs/rules/no-identical-title.md) | Disallow identical titles | ![recommended][] | |
170
+ | [no-if](docs/rules/no-if.md) | Disallow conditional logic | | |
171
+ | [no-interpolation-in-snapshots](docs/rules/no-interpolation-in-snapshots.md) | Disallow string interpolation inside snapshots | ![recommended][] | |
172
+ | [no-jasmine-globals](docs/rules/no-jasmine-globals.md) | Disallow Jasmine globals | ![recommended][] | ![fixable][] |
173
+ | [no-jest-import](docs/rules/no-jest-import.md) | Disallow importing Jest | ![recommended][] | |
174
+ | [no-large-snapshots](docs/rules/no-large-snapshots.md) | disallow large snapshots | | |
175
+ | [no-mocks-import](docs/rules/no-mocks-import.md) | Disallow manually importing from `__mocks__` | ![recommended][] | |
176
+ | [no-restricted-matchers](docs/rules/no-restricted-matchers.md) | Disallow specific matchers & modifiers | | |
177
+ | [no-standalone-expect](docs/rules/no-standalone-expect.md) | Disallow using `expect` outside of `it` or `test` blocks | ![recommended][] | |
178
+ | [no-test-prefixes](docs/rules/no-test-prefixes.md) | Use `.only` and `.skip` over `f` and `x` | ![recommended][] | ![fixable][] |
179
+ | [no-test-return-statement](docs/rules/no-test-return-statement.md) | Disallow explicitly returning from tests | | |
180
+ | [prefer-called-with](docs/rules/prefer-called-with.md) | Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()` | | |
181
+ | [prefer-expect-assertions](docs/rules/prefer-expect-assertions.md) | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | ![suggest][] |
182
+ | [prefer-expect-resolves](docs/rules/prefer-expect-resolves.md) | Prefer `await expect(...).resolves` over `expect(await ...)` syntax | | ![fixable][] |
183
+ | [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases | | |
184
+ | [prefer-spy-on](docs/rules/prefer-spy-on.md) | Suggest using `jest.spyOn()` | | ![fixable][] |
185
+ | [prefer-strict-equal](docs/rules/prefer-strict-equal.md) | Suggest using `toStrictEqual()` | | ![suggest][] |
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
+ | [prefer-to-contain](docs/rules/prefer-to-contain.md) | Suggest using `toContain()` | ![style][] | ![fixable][] |
190
+ | [prefer-to-have-length](docs/rules/prefer-to-have-length.md) | Suggest using `toHaveLength()` | ![style][] | ![fixable][] |
191
+ | [prefer-todo](docs/rules/prefer-todo.md) | Suggest using `test.todo` | | ![fixable][] |
192
+ | [require-to-throw-message](docs/rules/require-to-throw-message.md) | Require a message for `toThrow()` | | |
193
+ | [require-top-level-describe](docs/rules/require-top-level-describe.md) | Require test cases and hooks to be inside a `describe` block | | |
194
+ | [valid-describe](docs/rules/valid-describe.md) | Enforce valid `describe()` callback | ![recommended][] | |
195
+ | [valid-expect](docs/rules/valid-expect.md) | Enforce valid `expect()` usage | ![recommended][] | |
196
+ | [valid-expect-in-promise](docs/rules/valid-expect-in-promise.md) | Ensure promises that have expectations in their chain are valid | ![recommended][] | |
197
+ | [valid-title](docs/rules/valid-title.md) | Enforce valid titles | ![recommended][] | ![fixable][] |
176
198
 
177
199
  <!-- end base rules list -->
178
200
 
@@ -226,3 +248,4 @@ https://github.com/istanbuljs/eslint-plugin-istanbul
226
248
  [suggest]: https://img.shields.io/badge/-suggest-yellow.svg
227
249
  [fixable]: https://img.shields.io/badge/-fixable-green.svg
228
250
  [style]: https://img.shields.io/badge/-style-blue.svg
251
+ [`no-deprecated-functions`]: docs/rules/no-deprecated-functions.md
@@ -6,6 +6,11 @@ either been renamed for clarity, or replaced with more powerful APIs.
6
6
  While typically these deprecated functions are kept in the codebase for a number
7
7
  of majors, eventually they are removed completely.
8
8
 
9
+ This rule requires knowing which version of Jest you're using - see
10
+ [this section of the readme](../../README.md#jest-version-setting) for details
11
+ on how that is obtained automatically and how you can explicitly provide a
12
+ version if needed.
13
+
9
14
  ## Rule details
10
15
 
11
16
  This rule warns about calls to deprecated functions, and provides details on
@@ -0,0 +1,53 @@
1
+ # Prefer `await expect(...).resolves` over `expect(await ...)` syntax (`prefer-expect-resolves`)
2
+
3
+ When working with promises, there are two primary ways you can test the resolved
4
+ value:
5
+
6
+ 1. use the `resolve` modifier on `expect`
7
+ (`await expect(...).resolves.<matcher>` style)
8
+ 2. `await` the promise and assert against its result
9
+ (`expect(await ...).<matcher>` style)
10
+
11
+ While the second style is arguably less dependent on `jest`, if the promise
12
+ rejects it will be treated as a general error, resulting in less predictable
13
+ behaviour and output from `jest`.
14
+
15
+ Additionally, favoring the first style ensures consistency with its `rejects`
16
+ counterpart, as there is no way of "awaiting" a rejection.
17
+
18
+ ## Rule details
19
+
20
+ This rule triggers a warning if an `await` is done within an `expect`, and
21
+ recommends using `resolves` instead.
22
+
23
+ Examples of **incorrect** code for this rule
24
+
25
+ ```js
26
+ it('passes', async () => {
27
+ expect(await someValue()).toBe(true);
28
+ });
29
+
30
+ it('is true', async () => {
31
+ const myPromise = Promise.resolve(true);
32
+
33
+ expect(await myPromise).toBe(true);
34
+ });
35
+ ```
36
+
37
+ Examples of **correct** code for this rule
38
+
39
+ ```js
40
+ it('passes', async () => {
41
+ await expect(someValue()).resolves.toBe(true);
42
+ });
43
+
44
+ it('is true', async () => {
45
+ const myPromise = Promise.resolve(true);
46
+
47
+ await expect(myPromise).resolves.toBe(true);
48
+ });
49
+
50
+ it('errors', async () => {
51
+ await expect(Promise.rejects('oh noes!')).rejects.toThrow('oh noes!');
52
+ });
53
+ ```
@@ -0,0 +1,53 @@
1
+ # Suggest using `toBe()` for primitive literals (`prefer-to-be`)
2
+
3
+ When asserting against primitive literals such as numbers and strings, the
4
+ equality matchers all operate the same, but read slightly differently in code.
5
+
6
+ This rule recommends using the `toBe` matcher in these situations, as it forms
7
+ the most grammatically natural sentence. For `null`, `undefined`, and `NaN` this
8
+ rule recommends using their specific `toBe` matchers, as they give better error
9
+ messages as well.
10
+
11
+ ## Rule details
12
+
13
+ This rule triggers a warning if `toEqual()` or `toStrictEqual()` are used to
14
+ assert a primitive literal value such as numbers, strings, and booleans.
15
+
16
+ The following patterns are considered warnings:
17
+
18
+ ```js
19
+ expect(value).not.toEqual(5);
20
+ expect(getMessage()).toStrictEqual('hello world');
21
+ expect(loadMessage()).resolves.toEqual('hello world');
22
+ ```
23
+
24
+ The following pattern is not warning:
25
+
26
+ ```js
27
+ expect(value).not.toBe(5);
28
+ expect(getMessage()).toBe('hello world');
29
+ expect(loadMessage()).resolves.toBe('hello world');
30
+ expect(didError).not.toBe(true);
31
+
32
+ expect(catchError()).toStrictEqual({ message: 'oh noes!' });
33
+ ```
34
+
35
+ For `null`, `undefined`, and `NaN`, this rule triggers a warning if `toBe` is
36
+ used to assert against those literal values instead of their more specific
37
+ `toBe` counterparts:
38
+
39
+ ```js
40
+ expect(value).not.toBe(undefined);
41
+ expect(getMessage()).toBe(null);
42
+ expect(countMessages()).resolves.not.toBe(NaN);
43
+ ```
44
+
45
+ The following pattern is not warning:
46
+
47
+ ```js
48
+ expect(value).toBeDefined();
49
+ expect(getMessage()).toBeNull();
50
+ expect(countMessages()).resolves.not.toBeNaN();
51
+
52
+ expect(catchError()).toStrictEqual({ message: undefined });
53
+ ```
@@ -47,6 +47,34 @@ describe('test suite', () => {
47
47
  });
48
48
  ```
49
49
 
50
+ You can also enforce a limit on the number of describes allowed at the top-level
51
+ using the `maxNumberOfTopLevelDescribes` option:
52
+
53
+ ```json
54
+ {
55
+ "jest/require-top-level-describe": [
56
+ "error",
57
+ {
58
+ "maxNumberOfTopLevelDescribes": 2
59
+ }
60
+ ]
61
+ }
62
+ ```
63
+
64
+ Examples of **incorrect** code with the above config:
65
+
66
+ ```js
67
+ describe('test suite', () => {
68
+ it('test', () => {});
69
+ });
70
+
71
+ describe('test suite', () => {});
72
+
73
+ describe('test suite', () => {});
74
+ ```
75
+
76
+ This option defaults to `Infinity`, allowing any number of top-level describes.
77
+
50
78
  ## When Not To Use It
51
79
 
52
80
  Don't use this rule on non-jest test files.
@@ -1,31 +1,72 @@
1
- # Enforce having return statement when testing with promises (`valid-expect-in-promise`)
1
+ # Ensure promises that have expectations in their chain are valid (`valid-expect-in-promise`)
2
2
 
3
- Ensure to return promise when having assertions in `then` or `catch` block of
4
- promise
3
+ Ensure promises that include expectations are returned or awaited.
5
4
 
6
5
  ## Rule details
7
6
 
8
- This rule looks for tests that have assertions in `then` and `catch` methods on
9
- promises that are not returned by the test.
7
+ This rule flags any promises within the body of a test that include expectations
8
+ that have either not been returned or awaited.
10
9
 
11
- ### Default configuration
12
-
13
- The following pattern is considered warning:
10
+ The following patterns is considered warning:
14
11
 
15
12
  ```js
16
- it('promise test', () => {
17
- somePromise.then(data => {
18
- expect(data).toEqual('foo');
13
+ it('promises a person', () => {
14
+ api.getPersonByName('bob').then(person => {
15
+ expect(person).toHaveProperty('name', 'Bob');
16
+ });
17
+ });
18
+
19
+ it('promises a counted person', () => {
20
+ const promise = api.getPersonByName('bob').then(person => {
21
+ expect(person).toHaveProperty('name', 'Bob');
22
+ });
23
+
24
+ promise.then(() => {
25
+ expect(analytics.gottenPeopleCount).toBe(1);
19
26
  });
20
27
  });
28
+
29
+ it('promises multiple people', () => {
30
+ const firstPromise = api.getPersonByName('bob').then(person => {
31
+ expect(person).toHaveProperty('name', 'Bob');
32
+ });
33
+ const secondPromise = api.getPersonByName('alice').then(person => {
34
+ expect(person).toHaveProperty('name', 'Alice');
35
+ });
36
+
37
+ return Promise.any([firstPromise, secondPromise]);
38
+ });
21
39
  ```
22
40
 
23
41
  The following pattern is not warning:
24
42
 
25
43
  ```js
26
- it('promise test', () => {
27
- return somePromise.then(data => {
28
- expect(data).toEqual('foo');
44
+ it('promises a person', async () => {
45
+ await api.getPersonByName('bob').then(person => {
46
+ expect(person).toHaveProperty('name', 'Bob');
29
47
  });
30
48
  });
49
+
50
+ it('promises a counted person', () => {
51
+ let promise = api.getPersonByName('bob').then(person => {
52
+ expect(person).toHaveProperty('name', 'Bob');
53
+ });
54
+
55
+ promise = promise.then(() => {
56
+ expect(analytics.gottenPeopleCount).toBe(1);
57
+ });
58
+
59
+ return promise;
60
+ });
61
+
62
+ it('promises multiple people', () => {
63
+ const firstPromise = api.getPersonByName('bob').then(person => {
64
+ expect(person).toHaveProperty('name', 'Bob');
65
+ });
66
+ const secondPromise = api.getPersonByName('alice').then(person => {
67
+ expect(person).toHaveProperty('name', 'Alice');
68
+ });
69
+
70
+ return Promise.allSettled([firstPromise, secondPromise]);
71
+ });
31
72
  ```
@@ -198,8 +198,9 @@ describe('the proper way to handle things', () => {});
198
198
  Defaults: `{}`
199
199
 
200
200
  Allows enforcing that titles must match or must not match a given Regular
201
- Expression. An object can be provided to apply different Regular Expressions to
202
- specific Jest test function groups (`describe`, `test`, and `it`).
201
+ Expression, with an optional message. An object can be provided to apply
202
+ different Regular Expressions (with optional messages) to specific Jest test
203
+ function groups (`describe`, `test`, and `it`).
203
204
 
204
205
  Examples of **incorrect** code when using `mustMatch`:
205
206
 
@@ -226,3 +227,30 @@ describe('the tests that will be run', () => {});
226
227
  test('that the stuff works', () => {});
227
228
  xtest('that errors that thrown have messages', () => {});
228
229
  ```
230
+
231
+ Optionally you can provide a custom message to show for a particular matcher by
232
+ using a tuple at any level where you can provide a matcher:
233
+
234
+ ```js
235
+ const prefixes = ['when', 'with', 'without', 'if', 'unless', 'for'];
236
+ const prefixesList = prefixes.join(' - \n');
237
+
238
+ module.exports = {
239
+ rules: {
240
+ 'jest/valid-title': [
241
+ 'error',
242
+ {
243
+ mustNotMatch: ['\\.$', 'Titles should not end with a full-stop'],
244
+ mustMatch: {
245
+ describe: [
246
+ new RegExp(`^(?:[A-Z]|\\b(${prefixes.join('|')})\\b`, 'u').source,
247
+ `Describe titles should either start with a capital letter or one of the following prefixes: ${prefixesList}`,
248
+ ],
249
+ test: [/[^A-Z]/u.source],
250
+ it: /[^A-Z]/u.source,
251
+ },
252
+ },
253
+ ],
254
+ },
255
+ };
256
+ ```
package/lib/index.js CHANGED
@@ -25,7 +25,7 @@ const importDefault = moduleName => // eslint-disable-next-line @typescript-esli
25
25
  interopRequireDefault(require(moduleName)).default;
26
26
 
27
27
  const rulesDir = (0, _path.join)(__dirname, 'rules');
28
- const excludedFiles = ['__tests__', 'utils'];
28
+ const excludedFiles = ['__tests__', 'detectJestVersion', 'utils'];
29
29
  const rules = (0, _fs.readdirSync)(rulesDir).map(rule => (0, _path.parse)(rule).name).filter(rule => !excludedFiles.includes(rule)).reduce((acc, curr) => ({ ...acc,
30
30
  [curr]: importDefault((0, _path.join)(rulesDir, curr))
31
31
  }), {});
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.detectJestVersion = void 0;
7
+ let cachedJestVersion = null;
8
+
9
+ const detectJestVersion = () => {
10
+ if (cachedJestVersion) {
11
+ return cachedJestVersion;
12
+ }
13
+
14
+ try {
15
+ const jestPath = require.resolve('jest/package.json');
16
+
17
+ const jestPackageJson = // eslint-disable-next-line @typescript-eslint/no-require-imports
18
+ require(jestPath);
19
+
20
+ if (jestPackageJson.version) {
21
+ const [majorVersion] = jestPackageJson.version.split('.');
22
+ return cachedJestVersion = parseInt(majorVersion, 10);
23
+ }
24
+ } catch {}
25
+
26
+ throw new Error('Unable to detect Jest version - please ensure jest package is installed, or otherwise set version explicitly');
27
+ };
28
+
29
+ exports.detectJestVersion = detectJestVersion;
@@ -21,6 +21,24 @@ const findNodeNameAndArgument = node => {
21
21
  return [(0, _utils.getNodeName)(node).split('.')[0], node.arguments[0]];
22
22
  };
23
23
 
24
+ const populateIgnores = ignore => {
25
+ const ignores = [];
26
+
27
+ if (ignore.includes(_utils.DescribeAlias.describe)) {
28
+ ignores.push(...Object.keys(_utils.DescribeAlias));
29
+ }
30
+
31
+ if (ignore.includes(_utils.TestCaseName.test)) {
32
+ ignores.push(...Object.keys(_utils.TestCaseName));
33
+ }
34
+
35
+ if (ignore.includes(_utils.TestCaseName.it)) {
36
+ ignores.push(...Object.keys(_utils.TestCaseName));
37
+ }
38
+
39
+ return ignores;
40
+ };
41
+
24
42
  var _default = (0, _utils.createRule)({
25
43
  name: __filename,
26
44
  meta: {
@@ -70,6 +88,7 @@ var _default = (0, _utils.createRule)({
70
88
  allowedPrefixes = [],
71
89
  ignoreTopLevelDescribe
72
90
  }]) {
91
+ const ignores = populateIgnores(ignore);
73
92
  let numberOfDescribeBlocks = 0;
74
93
  return {
75
94
  CallExpression(node) {
@@ -96,7 +115,7 @@ var _default = (0, _utils.createRule)({
96
115
 
97
116
  const firstCharacter = description.charAt(0);
98
117
 
99
- if (!firstCharacter || firstCharacter === firstCharacter.toLowerCase() || ignore.includes(name)) {
118
+ if (!firstCharacter || firstCharacter === firstCharacter.toLowerCase() || ignores.includes(name)) {
100
119
  return;
101
120
  }
102
121
 
@@ -3,39 +3,21 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = exports._clearCachedJestVersion = void 0;
6
+ exports.default = void 0;
7
7
 
8
8
  var _experimentalUtils = require("@typescript-eslint/experimental-utils");
9
9
 
10
- var _utils = require("./utils");
11
-
12
- let cachedJestVersion = null;
13
- /** @internal */
10
+ var _detectJestVersion = require("./detectJestVersion");
14
11
 
15
- const _clearCachedJestVersion = () => cachedJestVersion = null;
16
-
17
- exports._clearCachedJestVersion = _clearCachedJestVersion;
12
+ var _utils = require("./utils");
18
13
 
19
- const detectJestVersion = () => {
20
- if (cachedJestVersion) {
21
- return cachedJestVersion;
14
+ const parseJestVersion = rawVersion => {
15
+ if (typeof rawVersion === 'number') {
16
+ return rawVersion;
22
17
  }
23
18
 
24
- try {
25
- const jestPath = require.resolve('jest/package.json', {
26
- paths: [process.cwd()]
27
- });
28
-
29
- const jestPackageJson = // eslint-disable-next-line @typescript-eslint/no-require-imports
30
- require(jestPath);
31
-
32
- if (jestPackageJson.version) {
33
- const [majorVersion] = jestPackageJson.version.split('.');
34
- return cachedJestVersion = parseInt(majorVersion, 10);
35
- }
36
- } catch {}
37
-
38
- throw new Error('Unable to detect Jest version - please ensure jest package is installed, or otherwise set version explicitly');
19
+ const [majorVersion] = rawVersion.split('.');
20
+ return parseInt(majorVersion, 10);
39
21
  };
40
22
 
41
23
  var _default = (0, _utils.createRule)({
@@ -58,7 +40,7 @@ var _default = (0, _utils.createRule)({
58
40
  create(context) {
59
41
  var _context$settings, _context$settings$jes;
60
42
 
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();
43
+ const jestVersion = parseJestVersion(((_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) || (0, _detectJestVersion.detectJestVersion)());
62
44
  const deprecations = { ...(jestVersion >= 15 && {
63
45
  'jest.resetModuleRegistry': 'jest.resetModules'
64
46
  }),