| 1 | # Disallow using `expect` outside of `it` or `test` blocks (`no-standalone-expect`)
|
|---|
| 2 |
|
|---|
| 3 | Prevents `expect` statements outside of a `test` or `it` block. An `expect`
|
|---|
| 4 | within a helper function (but outside of a `test` or `it` block) will not
|
|---|
| 5 | trigger this rule.
|
|---|
| 6 |
|
|---|
| 7 | ## Rule Details
|
|---|
| 8 |
|
|---|
| 9 | This rule aims to eliminate `expect` statements that will not be executed. An
|
|---|
| 10 | `expect` inside of a `describe` block but outside of a `test` or `it` block or
|
|---|
| 11 | outside a `describe` will not execute and therefore will trigger this rule. It
|
|---|
| 12 | is viable, however, to have an `expect` in a helper function that is called from
|
|---|
| 13 | within a `test` or `it` block so `expect` statements in a function will not
|
|---|
| 14 | trigger this rule.
|
|---|
| 15 |
|
|---|
| 16 | Statements like `expect.hasAssertions()` will NOT trigger this rule since these
|
|---|
| 17 | calls will execute if they are not in a test block.
|
|---|
| 18 |
|
|---|
| 19 | Examples of **incorrect** code for this rule:
|
|---|
| 20 |
|
|---|
| 21 | ```js
|
|---|
| 22 | // in describe
|
|---|
| 23 | describe('a test', () => {
|
|---|
| 24 | expect(1).toBe(1);
|
|---|
| 25 | });
|
|---|
| 26 |
|
|---|
| 27 | // below other tests
|
|---|
| 28 | describe('a test', () => {
|
|---|
| 29 | it('an it', () => {
|
|---|
| 30 | expect(1).toBe(1);
|
|---|
| 31 | });
|
|---|
| 32 |
|
|---|
| 33 | expect(1).toBe(1);
|
|---|
| 34 | });
|
|---|
| 35 | ```
|
|---|
| 36 |
|
|---|
| 37 | Examples of **correct** code for this rule:
|
|---|
| 38 |
|
|---|
| 39 | ```js
|
|---|
| 40 | // in it block
|
|---|
| 41 | describe('a test', () => {
|
|---|
| 42 | it('an it', () => {
|
|---|
| 43 | expect(1).toBe(1);
|
|---|
| 44 | });
|
|---|
| 45 | });
|
|---|
| 46 |
|
|---|
| 47 | // in helper function
|
|---|
| 48 | describe('a test', () => {
|
|---|
| 49 | const helper = () => {
|
|---|
| 50 | expect(1).toBe(1);
|
|---|
| 51 | };
|
|---|
| 52 |
|
|---|
| 53 | it('an it', () => {
|
|---|
| 54 | helper();
|
|---|
| 55 | });
|
|---|
| 56 | });
|
|---|
| 57 |
|
|---|
| 58 | describe('a test', () => {
|
|---|
| 59 | expect.hasAssertions(1);
|
|---|
| 60 | });
|
|---|
| 61 | ```
|
|---|
| 62 |
|
|---|
| 63 | \*Note that this rule will not trigger if the helper function is never used even
|
|---|
| 64 | thought the `expect` will not execute. Rely on a rule like no-unused-vars for
|
|---|
| 65 | this case.
|
|---|
| 66 |
|
|---|
| 67 | ### Options
|
|---|
| 68 |
|
|---|
| 69 | #### `additionalTestBlockFunctions`
|
|---|
| 70 |
|
|---|
| 71 | This array can be used to specify the names of functions that should also be
|
|---|
| 72 | treated as test blocks:
|
|---|
| 73 |
|
|---|
| 74 | ```json
|
|---|
| 75 | {
|
|---|
| 76 | "rules": {
|
|---|
| 77 | "jest/no-standalone-expect": [
|
|---|
| 78 | "error",
|
|---|
| 79 | { "additionalTestBlockFunctions": ["each.test"] }
|
|---|
| 80 | ]
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 | ```
|
|---|
| 84 |
|
|---|
| 85 | The following is _correct_ when using the above configuration:
|
|---|
| 86 |
|
|---|
| 87 | ```js
|
|---|
| 88 | each([
|
|---|
| 89 | [1, 1, 2],
|
|---|
| 90 | [1, 2, 3],
|
|---|
| 91 | [2, 1, 3],
|
|---|
| 92 | ]).test('returns the result of adding %d to %d', (a, b, expected) => {
|
|---|
| 93 | expect(a + b).toBe(expected);
|
|---|
| 94 | });
|
|---|
| 95 | ```
|
|---|
| 96 |
|
|---|
| 97 | ## When Not To Use It
|
|---|
| 98 |
|
|---|
| 99 | Don't use this rule on non-jest test files.
|
|---|