| [9af201e] | 1 | # Enforce valid `describe()` callback (`valid-describe-callback`)
|
|---|
| 2 |
|
|---|
| 3 | Using an improper `describe()` callback function can lead to unexpected test
|
|---|
| 4 | errors.
|
|---|
| 5 |
|
|---|
| 6 | ## Rule Details
|
|---|
| 7 |
|
|---|
| 8 | This rule validates that the second parameter of a `describe()` function is a
|
|---|
| 9 | callback function. This callback function:
|
|---|
| 10 |
|
|---|
| 11 | - should not be
|
|---|
| 12 | [async](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)
|
|---|
| 13 | - should not contain any parameters
|
|---|
| 14 | - should not contain any `return` statements
|
|---|
| 15 |
|
|---|
| 16 | The following `describe` function aliases are also validated:
|
|---|
| 17 |
|
|---|
| 18 | - `describe`
|
|---|
| 19 | - `describe.only`
|
|---|
| 20 | - `describe.skip`
|
|---|
| 21 | - `fdescribe`
|
|---|
| 22 | - `xdescribe`
|
|---|
| 23 |
|
|---|
| 24 | The following patterns are considered warnings:
|
|---|
| 25 |
|
|---|
| 26 | ```js
|
|---|
| 27 | // Async callback functions are not allowed
|
|---|
| 28 | describe('myFunction()', async () => {
|
|---|
| 29 | // ...
|
|---|
| 30 | });
|
|---|
| 31 |
|
|---|
| 32 | // Callback function parameters are not allowed
|
|---|
| 33 | describe('myFunction()', done => {
|
|---|
| 34 | // ...
|
|---|
| 35 | });
|
|---|
| 36 |
|
|---|
| 37 | //
|
|---|
| 38 | describe('myFunction', () => {
|
|---|
| 39 | // No return statements are allowed in block of a callback function
|
|---|
| 40 | return Promise.resolve().then(() => {
|
|---|
| 41 | it('breaks', () => {
|
|---|
| 42 | throw new Error('Fail');
|
|---|
| 43 | });
|
|---|
| 44 | });
|
|---|
| 45 | });
|
|---|
| 46 |
|
|---|
| 47 | // Returning a value from a describe block is not allowed
|
|---|
| 48 | describe('myFunction', () =>
|
|---|
| 49 | it('returns a truthy value', () => {
|
|---|
| 50 | expect(myFunction()).toBeTruthy();
|
|---|
| 51 | }));
|
|---|
| 52 | ```
|
|---|
| 53 |
|
|---|
| 54 | The following patterns are not considered warnings:
|
|---|
| 55 |
|
|---|
| 56 | ```js
|
|---|
| 57 | describe('myFunction()', () => {
|
|---|
| 58 | it('returns a truthy value', () => {
|
|---|
| 59 | expect(myFunction()).toBeTruthy();
|
|---|
| 60 | });
|
|---|
| 61 | });
|
|---|
| 62 | ```
|
|---|