eslint-plugin-jest 27.0.3 → 27.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/docs/rules/consistent-test-it.md +13 -1
- package/docs/rules/expect-expect.md +6 -0
- package/docs/rules/max-expects.md +7 -1
- package/docs/rules/max-nested-describe.md +7 -1
- package/docs/rules/no-alias-methods.md +10 -0
- package/docs/rules/no-commented-out-tests.md +7 -1
- package/docs/rules/no-conditional-expect.md +7 -1
- package/docs/rules/no-conditional-in-test.md +7 -1
- package/docs/rules/no-deprecated-functions.md +10 -0
- package/docs/rules/no-disabled-tests.md +7 -1
- package/docs/rules/no-done-callback.md +10 -0
- package/docs/rules/no-duplicate-hooks.md +7 -1
- package/docs/rules/no-export.md +7 -1
- package/docs/rules/no-focused-tests.md +12 -1
- package/docs/rules/no-hooks.md +7 -1
- package/docs/rules/no-identical-title.md +7 -1
- package/docs/rules/no-if.md +4 -4
- package/docs/rules/no-interpolation-in-snapshots.md +7 -1
- package/docs/rules/no-jasmine-globals.md +11 -1
- package/docs/rules/no-large-snapshots.md +7 -1
- package/docs/rules/no-mocks-import.md +7 -1
- package/docs/rules/no-restricted-jest-methods.md +55 -0
- package/docs/rules/no-restricted-matchers.md +11 -1
- package/docs/rules/no-standalone-expect.md +7 -1
- package/docs/rules/no-test-prefixes.md +10 -0
- package/docs/rules/no-test-return-statement.md +6 -0
- package/docs/rules/prefer-called-with.md +6 -0
- package/docs/rules/prefer-comparison-matcher.md +10 -0
- package/docs/rules/prefer-each.md +6 -0
- package/docs/rules/prefer-equality-matcher.md +10 -0
- package/docs/rules/prefer-expect-assertions.md +10 -0
- package/docs/rules/prefer-expect-resolves.md +13 -1
- package/docs/rules/prefer-hooks-in-order.md +7 -1
- package/docs/rules/prefer-hooks-on-top.md +7 -1
- package/docs/rules/prefer-lowercase-title.md +10 -0
- package/docs/rules/prefer-mock-promise-shorthand.md +11 -1
- package/docs/rules/prefer-snapshot-hint.md +6 -0
- package/docs/rules/prefer-spy-on.md +10 -0
- package/docs/rules/prefer-strict-equal.md +10 -0
- package/docs/rules/prefer-to-be.md +10 -0
- package/docs/rules/prefer-to-contain.md +10 -0
- package/docs/rules/prefer-to-have-length.md +10 -0
- package/docs/rules/prefer-todo.md +10 -0
- package/docs/rules/require-hook.md +6 -0
- package/docs/rules/require-to-throw-message.md +6 -0
- package/docs/rules/require-top-level-describe.md +9 -1
- package/docs/rules/unbound-method.md +9 -2
- package/docs/rules/valid-describe-callback.md +7 -1
- package/docs/rules/valid-expect-in-promise.md +6 -0
- package/docs/rules/valid-expect.md +6 -0
- package/docs/rules/valid-title.md +11 -1
- package/lib/rules/no-restricted-jest-methods.js +66 -0
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -215,10 +215,12 @@ installations requiring long-term consistency.
|
|
|
215
215
|
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ![recommended][] | ![suggest][] |
|
|
216
216
|
| [no-hooks](docs/rules/no-hooks.md) | Disallow setup and teardown hooks | | |
|
|
217
217
|
| [no-identical-title](docs/rules/no-identical-title.md) | Disallow identical titles | ![recommended][] | |
|
|
218
|
+
| [no-if](docs/rules/no-if.md) | Disallow conditional logic | ![deprecated][] | |
|
|
218
219
|
| [no-interpolation-in-snapshots](docs/rules/no-interpolation-in-snapshots.md) | Disallow string interpolation inside snapshots | ![recommended][] | |
|
|
219
220
|
| [no-jasmine-globals](docs/rules/no-jasmine-globals.md) | Disallow Jasmine globals | ![recommended][] | ![fixable][] |
|
|
220
221
|
| [no-large-snapshots](docs/rules/no-large-snapshots.md) | disallow large snapshots | | |
|
|
221
222
|
| [no-mocks-import](docs/rules/no-mocks-import.md) | Disallow manually importing from `__mocks__` | ![recommended][] | |
|
|
223
|
+
| [no-restricted-jest-methods](docs/rules/no-restricted-jest-methods.md) | Disallow specific `jest.` methods | | |
|
|
222
224
|
| [no-restricted-matchers](docs/rules/no-restricted-matchers.md) | Disallow specific matchers & modifiers | | |
|
|
223
225
|
| [no-standalone-expect](docs/rules/no-standalone-expect.md) | Disallow using `expect` outside of `it` or `test` blocks | ![recommended][] | |
|
|
224
226
|
| [no-test-prefixes](docs/rules/no-test-prefixes.md) | Use `.only` and `.skip` over `f` and `x` | ![recommended][] | ![fixable][] |
|
|
@@ -308,4 +310,5 @@ https://github.com/istanbuljs/eslint-plugin-istanbul
|
|
|
308
310
|
[suggest]: https://img.shields.io/badge/-suggest-yellow.svg
|
|
309
311
|
[fixable]: https://img.shields.io/badge/-fixable-green.svg
|
|
310
312
|
[style]: https://img.shields.io/badge/-style-blue.svg
|
|
313
|
+
[deprecated]: https://img.shields.io/badge/-deprecated-red.svg
|
|
311
314
|
[`no-deprecated-functions`]: docs/rules/no-deprecated-functions.md
|
|
@@ -1,14 +1,26 @@
|
|
|
1
1
|
# Have control over `test` and `it` usages (`consistent-test-it`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
🔧 This rule is automatically fixable using the `--fix`
|
|
8
|
+
[option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix)
|
|
9
|
+
on the command line.
|
|
10
|
+
|
|
11
|
+
<!-- end rule header -->
|
|
12
|
+
|
|
3
13
|
Jest allows you to choose how you want to define your tests, using the `it` or
|
|
4
14
|
the `test` keywords, with multiple permutations for each:
|
|
5
15
|
|
|
6
16
|
- **it:** `it`, `xit`, `fit`, `it.only`, `it.skip`.
|
|
7
17
|
- **test:** `test`, `xtest`, `test.only`, `test.skip`.
|
|
8
18
|
|
|
19
|
+
## Rule details
|
|
20
|
+
|
|
9
21
|
This rule gives you control over the usage of these keywords in your codebase.
|
|
10
22
|
|
|
11
|
-
##
|
|
23
|
+
## Options
|
|
12
24
|
|
|
13
25
|
This rule can be configured as follows
|
|
14
26
|
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Enforce assertion to be made in a test body (`expect-expect`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
Ensure that there is at least one `expect` call made in a test.
|
|
4
10
|
|
|
5
11
|
## Rule details
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
# Enforces a maximum number assertion calls in a test body (`max-expects`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
As more assertions are made, there is a possible tendency for the test to be
|
|
4
10
|
more likely to mix multiple objectives. To avoid this, this rule reports when
|
|
5
11
|
the maximum number of assertions is exceeded.
|
|
6
12
|
|
|
7
|
-
## Rule
|
|
13
|
+
## Rule details
|
|
8
14
|
|
|
9
15
|
This rule enforces a maximum number of `expect()` calls.
|
|
10
16
|
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
# Enforces a maximum depth to nested describe calls (`max-nested-describe`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
While it's useful to be able to group your tests together within the same file
|
|
4
10
|
using `describe()`, having too many levels of nesting throughout your tests make
|
|
5
11
|
them difficult to read.
|
|
6
12
|
|
|
7
|
-
## Rule
|
|
13
|
+
## Rule details
|
|
8
14
|
|
|
9
15
|
This rule enforces a maximum depth to nested `describe()` calls to improve code
|
|
10
16
|
clarity in your tests.
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Disallow alias methods (`no-alias-methods`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`, `recommended`.
|
|
6
|
+
|
|
7
|
+
🔧 This rule is automatically fixable using the `--fix`
|
|
8
|
+
[option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix)
|
|
9
|
+
on the command line.
|
|
10
|
+
|
|
11
|
+
<!-- end rule header -->
|
|
12
|
+
|
|
3
13
|
> These aliases are going to be removed in the next major version of Jest - see
|
|
4
14
|
> https://github.com/facebook/jest/issues/13164 for more
|
|
5
15
|
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
# Disallow commented out tests (`no-commented-out-tests`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
This rule raises a warning about commented out tests. It's similar to
|
|
4
10
|
no-disabled-tests rule.
|
|
5
11
|
|
|
6
|
-
## Rule
|
|
12
|
+
## Rule details
|
|
7
13
|
|
|
8
14
|
The rule uses fuzzy matching to do its best to determine what constitutes a
|
|
9
15
|
commented out test, checking for a presence of `it(`, `describe(`, `it.skip(`,
|
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
# Prevent calling `expect` conditionally (`no-conditional-expect`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`, `recommended`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
This rule prevents the use of `expect` in conditional blocks, such as `if`s &
|
|
4
10
|
`catch`s.
|
|
5
11
|
|
|
6
12
|
This includes using `expect` in callbacks to functions named `catch`, which are
|
|
7
13
|
assumed to be promises.
|
|
8
14
|
|
|
9
|
-
## Rule
|
|
15
|
+
## Rule details
|
|
10
16
|
|
|
11
17
|
Jest only considers a test to have failed if it throws an error, meaning if
|
|
12
18
|
calls to assertion functions like `expect` occur in conditional code such as a
|
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
# Disallow conditional logic in tests (`no-conditional-in-test`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
Conditional logic in tests is usually an indication that a test is attempting to
|
|
4
10
|
cover too much, and not testing the logic it intends to. Each branch of code
|
|
5
11
|
executing within a conditional statement will usually be better served by a test
|
|
6
12
|
devoted to it.
|
|
7
13
|
|
|
8
|
-
## Rule
|
|
14
|
+
## Rule details
|
|
9
15
|
|
|
10
16
|
This rule reports on any use of a conditional statement such as `if`, `switch`,
|
|
11
17
|
and ternary expressions.
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Disallow use of deprecated functions (`no-deprecated-functions`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`, `recommended`.
|
|
6
|
+
|
|
7
|
+
🔧 This rule is automatically fixable using the `--fix`
|
|
8
|
+
[option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix)
|
|
9
|
+
on the command line.
|
|
10
|
+
|
|
11
|
+
<!-- end rule header -->
|
|
12
|
+
|
|
3
13
|
Over the years Jest has accrued some debt in the form of functions that have
|
|
4
14
|
either been renamed for clarity, or replaced with more powerful APIs.
|
|
5
15
|
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Disallow disabled tests (`no-disabled-tests`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
Jest has a feature that allows you to temporarily mark tests as disabled. This
|
|
4
10
|
feature is often helpful while debugging or to create placeholders for future
|
|
5
11
|
tests. Before committing changes we may want to check that all tests are
|
|
@@ -7,7 +13,7 @@ running.
|
|
|
7
13
|
|
|
8
14
|
This rule raises a warning about disabled tests.
|
|
9
15
|
|
|
10
|
-
## Rule
|
|
16
|
+
## Rule details
|
|
11
17
|
|
|
12
18
|
There are a number of ways to disable tests in Jest:
|
|
13
19
|
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Avoid using a callback in asynchronous tests and hooks (`no-done-callback`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`, `recommended`.
|
|
6
|
+
|
|
7
|
+
💡 This rule provides
|
|
8
|
+
[suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions)
|
|
9
|
+
that can be applied manually.
|
|
10
|
+
|
|
11
|
+
<!-- end rule header -->
|
|
12
|
+
|
|
3
13
|
When calling asynchronous code in hooks and tests, `jest` needs to know when the
|
|
4
14
|
asynchronous work is complete to progress the current run.
|
|
5
15
|
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
# Disallow duplicate setup and teardown hooks (`no-duplicate-hooks`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
A `describe` block should not contain duplicate hooks.
|
|
4
10
|
|
|
5
|
-
## Rule
|
|
11
|
+
## Rule details
|
|
6
12
|
|
|
7
13
|
Examples of **incorrect** code for this rule
|
|
8
14
|
|
package/docs/rules/no-export.md
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
# Disallow using `exports` in files containing tests (`no-export`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`, `recommended`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
Prevents using `exports` if a file has one or more tests in it.
|
|
4
10
|
|
|
5
|
-
## Rule
|
|
11
|
+
## Rule details
|
|
6
12
|
|
|
7
13
|
This rule aims to eliminate duplicate runs of tests by exporting things from
|
|
8
14
|
test files. If you import from a test file, then all the tests in that file will
|
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# Disallow focused tests (`no-focused-tests`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`, `recommended`.
|
|
6
|
+
|
|
7
|
+
💡 This rule provides
|
|
8
|
+
[suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions)
|
|
9
|
+
that can be applied manually.
|
|
10
|
+
|
|
11
|
+
<!-- end rule header -->
|
|
12
|
+
|
|
13
|
+
<!-- prettier-ignore -->
|
|
3
14
|
Jest has a feature that allows you to focus tests by appending `.only` or
|
|
4
15
|
prepending `f` to a test-suite or a test-case. This feature is really helpful to
|
|
5
16
|
debug a failing test, so you don’t have to execute all of your tests. After you
|
|
@@ -9,7 +20,7 @@ have fixed your test and before committing the changes you have to remove
|
|
|
9
20
|
This rule reminds you to remove `.only` from your tests by raising a warning
|
|
10
21
|
whenever you are using the exclusivity feature.
|
|
11
22
|
|
|
12
|
-
## Rule
|
|
23
|
+
## Rule details
|
|
13
24
|
|
|
14
25
|
This rule looks for every `describe.only`, `it.only`, `test.only`, `fdescribe`,
|
|
15
26
|
and `fit` occurrences within the source code. Of course there are some
|
package/docs/rules/no-hooks.md
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
# Disallow setup and teardown hooks (`no-hooks`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
Jest provides global functions for setup and teardown tasks, which are called
|
|
4
10
|
before/after each test case and each test suite. The use of these hooks promotes
|
|
5
11
|
shared state between tests.
|
|
6
12
|
|
|
7
|
-
## Rule
|
|
13
|
+
## Rule details
|
|
8
14
|
|
|
9
15
|
This rule reports for the following function calls:
|
|
10
16
|
|
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
# Disallow identical titles (`no-identical-title`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`, `recommended`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
Having identical titles for two different tests or test suites may create
|
|
4
10
|
confusion. For example, when a test with the same title as another test in the
|
|
5
11
|
same test suite fails, it is harder to know which one failed and thus harder to
|
|
6
12
|
fix.
|
|
7
13
|
|
|
8
|
-
## Rule
|
|
14
|
+
## Rule details
|
|
9
15
|
|
|
10
16
|
This rule looks at the title of every test and test suite. It will report when
|
|
11
17
|
two test suites or two test cases at the same level of a test suite have the
|
package/docs/rules/no-if.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# Disallow conditional logic (`no-if`)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
❌ This rule is deprecated. It was replaced by
|
|
4
|
+
[no-conditional-in-test](no-conditional-in-test.md).
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
[`no-conditional-in-test`](no-conditional-in-test.md).
|
|
6
|
+
<!-- end rule header -->
|
|
7
7
|
|
|
8
8
|
Conditional logic in tests is usually an indication that a test is attempting to
|
|
9
9
|
cover too much, and not testing the logic it intends to. Each branch of code
|
|
@@ -13,7 +13,7 @@ to it.
|
|
|
13
13
|
Conditionals are often used to satisfy the typescript type checker. In these
|
|
14
14
|
cases, using the non-null assertion operator (!) would be best.
|
|
15
15
|
|
|
16
|
-
## Rule
|
|
16
|
+
## Rule details
|
|
17
17
|
|
|
18
18
|
This rule prevents the use of if/ else statements and conditional (ternary)
|
|
19
19
|
operations in tests.
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
# Disallow string interpolation inside snapshots (`no-interpolation-in-snapshots`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`, `recommended`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
Prevents the use of string interpolations in snapshots.
|
|
4
10
|
|
|
5
|
-
## Rule
|
|
11
|
+
## Rule details
|
|
6
12
|
|
|
7
13
|
Interpolation prevents snapshots from being updated. Instead, properties should
|
|
8
14
|
be overloaded with a matcher by using
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Disallow Jasmine globals (`no-jasmine-globals`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`, `recommended`.
|
|
6
|
+
|
|
7
|
+
🔧 This rule is automatically fixable using the `--fix`
|
|
8
|
+
[option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix)
|
|
9
|
+
on the command line.
|
|
10
|
+
|
|
11
|
+
<!-- end rule header -->
|
|
12
|
+
|
|
3
13
|
`jest` uses `jasmine` as a test runner. A side effect of this is that both a
|
|
4
14
|
`jasmine` object, and some jasmine-specific globals, are exposed to the test
|
|
5
15
|
environment. Most functionality offered by Jasmine has been ported to Jest, and
|
|
@@ -7,7 +17,7 @@ the Jasmine globals will stop working in the future. Developers should therefore
|
|
|
7
17
|
migrate to Jest's documented API instead of relying on the undocumented Jasmine
|
|
8
18
|
API.
|
|
9
19
|
|
|
10
|
-
|
|
20
|
+
## Rule details
|
|
11
21
|
|
|
12
22
|
This rule reports on any usage of Jasmine globals, which is not ported to Jest,
|
|
13
23
|
and suggests alternatives from Jest's own API.
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# disallow large snapshots (`no-large-snapshots`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
When using Jest's snapshot capability one should be mindful of the size of
|
|
4
10
|
created snapshots. As a general best practice snapshots should be limited in
|
|
5
11
|
size in order to be more manageable and reviewable. A stored snapshot is only as
|
|
@@ -22,7 +28,7 @@ module.exports = {
|
|
|
22
28
|
};
|
|
23
29
|
```
|
|
24
30
|
|
|
25
|
-
## Rule
|
|
31
|
+
## Rule details
|
|
26
32
|
|
|
27
33
|
This rule looks at all Jest inline and external snapshots (files with `.snap`
|
|
28
34
|
extension) and validates that each stored snapshot within those files does not
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Disallow manually importing from `__mocks__` (`no-mocks-import`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`, `recommended`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
When using `jest.mock`, your tests (just like the code being tested) should
|
|
4
10
|
import from `./x`, not `./__mocks__/x`. Not following this rule can lead to
|
|
5
11
|
confusion, because you will have multiple instances of the mocked module:
|
|
@@ -14,7 +20,7 @@ test('x', () => {
|
|
|
14
20
|
});
|
|
15
21
|
```
|
|
16
22
|
|
|
17
|
-
|
|
23
|
+
## Rule details
|
|
18
24
|
|
|
19
25
|
This rule reports imports from a path containing a `__mocks__` component.
|
|
20
26
|
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Disallow specific `jest.` methods (`no-restricted-jest-methods`)
|
|
2
|
+
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
9
|
+
You may wish to restrict the use of specific `jest` methods.
|
|
10
|
+
|
|
11
|
+
## Rule details
|
|
12
|
+
|
|
13
|
+
This rule checks for the usage of specific methods on the `jest` object, which
|
|
14
|
+
can be used to disallow curtain patterns such as spies and mocks.
|
|
15
|
+
|
|
16
|
+
## Options
|
|
17
|
+
|
|
18
|
+
Restrictions are expressed in the form of a map, with the value being either a
|
|
19
|
+
string message to be shown, or `null` if a generic default message should be
|
|
20
|
+
used.
|
|
21
|
+
|
|
22
|
+
By default, this map is empty, meaning no `jest` methods are banned.
|
|
23
|
+
|
|
24
|
+
For example:
|
|
25
|
+
|
|
26
|
+
```json
|
|
27
|
+
{
|
|
28
|
+
"jest/no-restricted-jest-methods": [
|
|
29
|
+
"error",
|
|
30
|
+
{
|
|
31
|
+
"advanceTimersByTime": null,
|
|
32
|
+
"spyOn": "Don't use spies"
|
|
33
|
+
}
|
|
34
|
+
]
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Examples of **incorrect** code for this rule with the above configuration
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
jest.useFakeTimers();
|
|
42
|
+
it('calls the callback after 1 second via advanceTimersByTime', () => {
|
|
43
|
+
// ...
|
|
44
|
+
|
|
45
|
+
jest.advanceTimersByTime(1000);
|
|
46
|
+
|
|
47
|
+
// ...
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('plays video', () => {
|
|
51
|
+
const spy = jest.spyOn(video, 'play');
|
|
52
|
+
|
|
53
|
+
// ...
|
|
54
|
+
});
|
|
55
|
+
```
|
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
# Disallow specific matchers & modifiers (`no-restricted-matchers`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
9
|
+
You may want to ban specific matchers & modifiers from being used.
|
|
10
|
+
|
|
11
|
+
## Rule details
|
|
12
|
+
|
|
3
13
|
This rule bans specific matchers & modifiers from being used, and can suggest
|
|
4
14
|
alternatives.
|
|
5
15
|
|
|
6
|
-
##
|
|
16
|
+
## Options
|
|
7
17
|
|
|
8
18
|
Bans are expressed in the form of a map, with the value being either a string
|
|
9
19
|
message to be shown, or `null` if the default rule message should be used.
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
# Disallow using `expect` outside of `it` or `test` blocks (`no-standalone-expect`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`, `recommended`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
Prevents `expect` statements outside of a `test` or `it` block. An `expect`
|
|
4
10
|
within a helper function (but outside of a `test` or `it` block) will not
|
|
5
11
|
trigger this rule.
|
|
6
12
|
|
|
7
|
-
## Rule
|
|
13
|
+
## Rule details
|
|
8
14
|
|
|
9
15
|
This rule aims to eliminate `expect` statements that will not be executed. An
|
|
10
16
|
`expect` inside of a `describe` block but outside of a `test` or `it` block or
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Use `.only` and `.skip` over `f` and `x` (`no-test-prefixes`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`, `recommended`.
|
|
6
|
+
|
|
7
|
+
🔧 This rule is automatically fixable using the `--fix`
|
|
8
|
+
[option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix)
|
|
9
|
+
on the command line.
|
|
10
|
+
|
|
11
|
+
<!-- end rule header -->
|
|
12
|
+
|
|
3
13
|
Jest allows you to choose how you want to define focused and skipped tests, with
|
|
4
14
|
multiple permutations for each:
|
|
5
15
|
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Disallow explicitly returning from tests (`no-test-return-statement`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
Tests in Jest should be void and not return values.
|
|
4
10
|
|
|
5
11
|
If you are returning Promises then you should update the test to use
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()` (`prefer-called-with`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
The `toBeCalled()` matcher is used to assert that a mock function has been
|
|
4
10
|
called one or more times, without checking the arguments passed. The assertion
|
|
5
11
|
is stronger when arguments are also validated using the `toBeCalledWith()`
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Suggest using the built-in comparison matchers (`prefer-comparison-matcher`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
🔧 This rule is automatically fixable using the `--fix`
|
|
8
|
+
[option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix)
|
|
9
|
+
on the command line.
|
|
10
|
+
|
|
11
|
+
<!-- end rule header -->
|
|
12
|
+
|
|
3
13
|
Jest has a number of built-in matchers for comparing numbers, which allow for
|
|
4
14
|
more readable tests and error messages if an expectation fails.
|
|
5
15
|
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Prefer using `.each` rather than manual loops (`prefer-each`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
Reports where you might be able to use `.each` instead of native loops.
|
|
4
10
|
|
|
5
11
|
## Rule details
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Suggest using the built-in equality matchers (`prefer-equality-matcher`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
💡 This rule provides
|
|
8
|
+
[suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions)
|
|
9
|
+
that can be applied manually.
|
|
10
|
+
|
|
11
|
+
<!-- end rule header -->
|
|
12
|
+
|
|
3
13
|
Jest has built-in matchers for expecting equality, which allow for more readable
|
|
4
14
|
tests and error messages if an expectation fails.
|
|
5
15
|
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Suggest using `expect.assertions()` OR `expect.hasAssertions()` (`prefer-expect-assertions`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
💡 This rule provides
|
|
8
|
+
[suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions)
|
|
9
|
+
that can be applied manually.
|
|
10
|
+
|
|
11
|
+
<!-- end rule header -->
|
|
12
|
+
|
|
3
13
|
Ensure every test to have either `expect.assertions(<number of assertions>)` OR
|
|
4
14
|
`expect.hasAssertions()` as its first expression.
|
|
5
15
|
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Prefer `await expect(...).resolves` over `expect(await ...)` syntax (`prefer-expect-resolves`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
🔧 This rule is automatically fixable using the `--fix`
|
|
8
|
+
[option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix)
|
|
9
|
+
on the command line.
|
|
10
|
+
|
|
11
|
+
<!-- end rule header -->
|
|
12
|
+
|
|
3
13
|
When working with promises, there are two primary ways you can test the resolved
|
|
4
14
|
value:
|
|
5
15
|
|
|
@@ -48,6 +58,8 @@ it('is true', async () => {
|
|
|
48
58
|
});
|
|
49
59
|
|
|
50
60
|
it('errors', async () => {
|
|
51
|
-
await expect(Promise.
|
|
61
|
+
await expect(Promise.reject(new Error('oh noes!'))).rejects.toThrowError(
|
|
62
|
+
'oh noes!',
|
|
63
|
+
);
|
|
52
64
|
});
|
|
53
65
|
```
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Prefer having hooks in a consistent order (`prefer-hooks-in-order`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
While hooks can be setup in any order, they're always called by `jest` in this
|
|
4
10
|
specific order:
|
|
5
11
|
|
|
@@ -11,7 +17,7 @@ specific order:
|
|
|
11
17
|
This rule aims to make that more obvious by enforcing grouped hooks be setup in
|
|
12
18
|
that order within tests.
|
|
13
19
|
|
|
14
|
-
## Rule
|
|
20
|
+
## Rule details
|
|
15
21
|
|
|
16
22
|
Examples of **incorrect** code for this rule
|
|
17
23
|
|
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
# Suggest having hooks before any test cases (`prefer-hooks-on-top`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
While hooks can be setup anywhere in a test file, they are always called in a
|
|
4
10
|
specific order, which means it can be confusing if they're intermixed with test
|
|
5
11
|
cases.
|
|
6
12
|
|
|
7
13
|
This rule helps to ensure that hooks are always defined before test cases.
|
|
8
14
|
|
|
9
|
-
## Rule
|
|
15
|
+
## Rule details
|
|
10
16
|
|
|
11
17
|
Examples of **incorrect** code for this rule
|
|
12
18
|
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Enforce lowercase test names (`prefer-lowercase-title`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
🔧 This rule is automatically fixable using the `--fix`
|
|
8
|
+
[option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix)
|
|
9
|
+
on the command line.
|
|
10
|
+
|
|
11
|
+
<!-- end rule header -->
|
|
12
|
+
|
|
3
13
|
## Rule details
|
|
4
14
|
|
|
5
15
|
Enforce `it`, `test` and `describe` to have descriptions that begin with a
|
|
@@ -1,11 +1,21 @@
|
|
|
1
1
|
# Prefer mock resolved/rejected shorthands for promises (`prefer-mock-promise-shorthand`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
🔧 This rule is automatically fixable using the `--fix`
|
|
8
|
+
[option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix)
|
|
9
|
+
on the command line.
|
|
10
|
+
|
|
11
|
+
<!-- end rule header -->
|
|
12
|
+
|
|
3
13
|
When working with mocks of functions that return promises, Jest provides some
|
|
4
14
|
API sugar functions to reduce the amount of boilerplate you have to write.
|
|
5
15
|
|
|
6
16
|
These methods should be preferred when possible.
|
|
7
17
|
|
|
8
|
-
## Rule
|
|
18
|
+
## Rule details
|
|
9
19
|
|
|
10
20
|
The following patterns are warnings:
|
|
11
21
|
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Prefer including a hint with external snapshots (`prefer-snapshot-hint`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
When working with external snapshot matchers it's considered best practice to
|
|
4
10
|
provide a hint (as the last argument to the matcher) describing the expected
|
|
5
11
|
snapshot content that will be included in the snapshots name by Jest.
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Suggest using `jest.spyOn()` (`prefer-spy-on`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
🔧 This rule is automatically fixable using the `--fix`
|
|
8
|
+
[option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix)
|
|
9
|
+
on the command line.
|
|
10
|
+
|
|
11
|
+
<!-- end rule header -->
|
|
12
|
+
|
|
3
13
|
When mocking a function by overwriting a property you have to manually restore
|
|
4
14
|
the original implementation when cleaning up. When using `jest.spyOn()` Jest
|
|
5
15
|
keeps track of changes, and they can be restored with `jest.restoreAllMocks()`,
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Suggest using `toStrictEqual()` (`prefer-strict-equal`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
💡 This rule provides
|
|
8
|
+
[suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions)
|
|
9
|
+
that can be applied manually.
|
|
10
|
+
|
|
11
|
+
<!-- end rule header -->
|
|
12
|
+
|
|
3
13
|
`toStrictEqual` not only checks that two objects contain the same data but also
|
|
4
14
|
that they have the same structure. It is common to expect objects to not only
|
|
5
15
|
have identical values but also to have identical keys. A stricter equality will
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Suggest using `toBe()` for primitive literals (`prefer-to-be`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`, `style`.
|
|
6
|
+
|
|
7
|
+
🔧 This rule is automatically fixable using the `--fix`
|
|
8
|
+
[option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix)
|
|
9
|
+
on the command line.
|
|
10
|
+
|
|
11
|
+
<!-- end rule header -->
|
|
12
|
+
|
|
3
13
|
When asserting against primitive literals such as numbers and strings, the
|
|
4
14
|
equality matchers all operate the same, but read slightly differently in code.
|
|
5
15
|
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Suggest using `toContain()` (`prefer-to-contain`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`, `style`.
|
|
6
|
+
|
|
7
|
+
🔧 This rule is automatically fixable using the `--fix`
|
|
8
|
+
[option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix)
|
|
9
|
+
on the command line.
|
|
10
|
+
|
|
11
|
+
<!-- end rule header -->
|
|
12
|
+
|
|
3
13
|
In order to have a better failure message, `toContain()` should be used upon
|
|
4
14
|
asserting expectations on an array containing an object.
|
|
5
15
|
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Suggest using `toHaveLength()` (`prefer-to-have-length`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`, `style`.
|
|
6
|
+
|
|
7
|
+
🔧 This rule is automatically fixable using the `--fix`
|
|
8
|
+
[option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix)
|
|
9
|
+
on the command line.
|
|
10
|
+
|
|
11
|
+
<!-- end rule header -->
|
|
12
|
+
|
|
3
13
|
In order to have a better failure message, `toHaveLength()` should be used upon
|
|
4
14
|
asserting expectations on objects length property.
|
|
5
15
|
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Suggest using `test.todo` (`prefer-todo`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
🔧 This rule is automatically fixable using the `--fix`
|
|
8
|
+
[option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix)
|
|
9
|
+
on the command line.
|
|
10
|
+
|
|
11
|
+
<!-- end rule header -->
|
|
12
|
+
|
|
3
13
|
When test cases are empty then it is better to mark them as `test.todo` as it
|
|
4
14
|
will be highlighted in the summary output.
|
|
5
15
|
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Require setup and teardown code to be within a hook (`require-hook`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
Often while writing tests you have some setup work that needs to happen before
|
|
4
10
|
tests run, and you have some finishing work that needs to happen after tests
|
|
5
11
|
run. Jest provides helper functions to handle this.
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Require a message for `toThrow()` (`require-to-throw-message`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
`toThrow()` (and its alias `toThrowError()`) is used to check if an error is
|
|
4
10
|
thrown by a function call, such as in `expect(() => a()).toThrow()`. However, if
|
|
5
11
|
no message is defined, then the test will pass for any thrown error. Requiring a
|
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
# Require test cases and hooks to be inside a `describe` block (`require-top-level-describe`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
Jest allows you to organise your test files the way you want it. However, the
|
|
4
10
|
more your codebase grows, the more it becomes hard to navigate in your test
|
|
5
11
|
files. This rule makes sure you provide at least a top-level `describe` block in
|
|
6
12
|
your test file.
|
|
7
13
|
|
|
8
|
-
## Rule
|
|
14
|
+
## Rule details
|
|
9
15
|
|
|
10
16
|
This rule triggers a warning if a test case (`test` and `it`) or a hook
|
|
11
17
|
(`beforeAll`, `beforeEach`, `afterEach`, `afterAll`) is not located in a
|
|
@@ -47,6 +53,8 @@ describe('test suite', () => {
|
|
|
47
53
|
});
|
|
48
54
|
```
|
|
49
55
|
|
|
56
|
+
## Options
|
|
57
|
+
|
|
50
58
|
You can also enforce a limit on the number of describes allowed at the top-level
|
|
51
59
|
using the `maxNumberOfTopLevelDescribes` option:
|
|
52
60
|
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
# Enforce unbound methods are called with their expected scope (`unbound-method`)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
9
|
+
## Rule details
|
|
4
10
|
|
|
5
11
|
This rule extends the base [`@typescript-eslint/unbound-method`][original-rule]
|
|
6
12
|
rule, meaning you must depend on `@typescript-eslint/eslint-plugin` for it to
|
|
@@ -46,7 +52,8 @@ which should be applied to the rest of your codebase.
|
|
|
46
52
|
|
|
47
53
|
## Options
|
|
48
54
|
|
|
49
|
-
See [`@typescript-eslint/unbound-method`][original-rule] options.
|
|
55
|
+
See [`@typescript-eslint/unbound-method`][original-rule] options (e.g.
|
|
56
|
+
`ignoreStatic`).
|
|
50
57
|
|
|
51
58
|
<sup>Taken with ❤️ [from `@typescript-eslint` core][original-rule]</sup>
|
|
52
59
|
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
# Enforce valid `describe()` callback (`valid-describe-callback`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`, `recommended`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
Using an improper `describe()` callback function can lead to unexpected test
|
|
4
10
|
errors.
|
|
5
11
|
|
|
6
|
-
## Rule
|
|
12
|
+
## Rule details
|
|
7
13
|
|
|
8
14
|
This rule validates that the second parameter of a `describe()` function is a
|
|
9
15
|
callback function. This callback function:
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Ensure promises that have expectations in their chain are valid (`valid-expect-in-promise`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`, `recommended`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
Ensure promises that include expectations are returned or awaited.
|
|
4
10
|
|
|
5
11
|
## Rule details
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Enforce valid `expect()` usage (`valid-expect`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`, `recommended`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
3
9
|
Ensure `expect()` is called with a single argument and there is an actual
|
|
4
10
|
expectation made.
|
|
5
11
|
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Enforce valid titles (`valid-title`)
|
|
2
2
|
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`, `recommended`.
|
|
6
|
+
|
|
7
|
+
🔧 This rule is automatically fixable using the `--fix`
|
|
8
|
+
[option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix)
|
|
9
|
+
on the command line.
|
|
10
|
+
|
|
11
|
+
<!-- end rule header -->
|
|
12
|
+
|
|
3
13
|
Checks that the title of Jest blocks are valid by ensuring that titles are:
|
|
4
14
|
|
|
5
15
|
- not empty,
|
|
@@ -7,7 +17,7 @@ Checks that the title of Jest blocks are valid by ensuring that titles are:
|
|
|
7
17
|
- not prefixed with their block name,
|
|
8
18
|
- have no leading or trailing spaces
|
|
9
19
|
|
|
10
|
-
## Rule
|
|
20
|
+
## Rule details
|
|
11
21
|
|
|
12
22
|
**emptyTitle**
|
|
13
23
|
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _utils = require("./utils");
|
|
9
|
+
|
|
10
|
+
const messages = {
|
|
11
|
+
restrictedJestMethod: 'Use of `{{ restriction }}` is disallowed',
|
|
12
|
+
restrictedJestMethodWithMessage: '{{ message }}'
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
var _default = (0, _utils.createRule)({
|
|
16
|
+
name: __filename,
|
|
17
|
+
meta: {
|
|
18
|
+
docs: {
|
|
19
|
+
category: 'Best Practices',
|
|
20
|
+
description: 'Disallow specific `jest.` methods',
|
|
21
|
+
recommended: false
|
|
22
|
+
},
|
|
23
|
+
type: 'suggestion',
|
|
24
|
+
schema: [{
|
|
25
|
+
type: 'object',
|
|
26
|
+
additionalProperties: {
|
|
27
|
+
type: ['string', 'null']
|
|
28
|
+
}
|
|
29
|
+
}],
|
|
30
|
+
messages
|
|
31
|
+
},
|
|
32
|
+
defaultOptions: [{}],
|
|
33
|
+
|
|
34
|
+
create(context, [restrictedMethods]) {
|
|
35
|
+
return {
|
|
36
|
+
CallExpression(node) {
|
|
37
|
+
const jestFnCall = (0, _utils.parseJestFnCall)(node, context);
|
|
38
|
+
|
|
39
|
+
if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'jest') {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const method = (0, _utils.getAccessorValue)(jestFnCall.members[0]);
|
|
44
|
+
|
|
45
|
+
if (method in restrictedMethods) {
|
|
46
|
+
const message = restrictedMethods[method];
|
|
47
|
+
context.report({
|
|
48
|
+
messageId: message ? 'restrictedJestMethodWithMessage' : 'restrictedJestMethod',
|
|
49
|
+
data: {
|
|
50
|
+
message,
|
|
51
|
+
restriction: method
|
|
52
|
+
},
|
|
53
|
+
loc: {
|
|
54
|
+
start: jestFnCall.members[0].loc.start,
|
|
55
|
+
end: jestFnCall.members[jestFnCall.members.length - 1].loc.end
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
exports.default = _default;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-jest",
|
|
3
|
-
"version": "27.0
|
|
3
|
+
"version": "27.1.0",
|
|
4
4
|
"description": "ESLint rules for Jest",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -108,7 +108,9 @@
|
|
|
108
108
|
"@schemastore/package": "^0.0.6",
|
|
109
109
|
"@semantic-release/changelog": "^6.0.0",
|
|
110
110
|
"@semantic-release/git": "^10.0.0",
|
|
111
|
+
"@tsconfig/node14": "^1.0.3",
|
|
111
112
|
"@types/dedent": "^0.7.0",
|
|
113
|
+
"@types/eslint": "^8.4.6",
|
|
112
114
|
"@types/jest": "^29.0.0",
|
|
113
115
|
"@types/node": "^14.18.26",
|
|
114
116
|
"@types/prettier": "^2.0.0",
|