| 1 | # Enforces a maximum depth to nested describe calls (`max-nested-describe`)
|
|---|
| 2 |
|
|---|
| 3 | While it's useful to be able to group your tests together within the same file
|
|---|
| 4 | using `describe()`, having too many levels of nesting throughout your tests make
|
|---|
| 5 | them difficult to read.
|
|---|
| 6 |
|
|---|
| 7 | ## Rule Details
|
|---|
| 8 |
|
|---|
| 9 | This rule enforces a maximum depth to nested `describe()` calls to improve code
|
|---|
| 10 | clarity in your tests.
|
|---|
| 11 |
|
|---|
| 12 | The following patterns are considered warnings (with the default option of
|
|---|
| 13 | `{ "max": 5 } `):
|
|---|
| 14 |
|
|---|
| 15 | ```js
|
|---|
| 16 | describe('foo', () => {
|
|---|
| 17 | describe('bar', () => {
|
|---|
| 18 | describe('baz', () => {
|
|---|
| 19 | describe('qux', () => {
|
|---|
| 20 | describe('quxx', () => {
|
|---|
| 21 | describe('too many', () => {
|
|---|
| 22 | it('should get something', () => {
|
|---|
| 23 | expect(getSomething()).toBe('Something');
|
|---|
| 24 | });
|
|---|
| 25 | });
|
|---|
| 26 | });
|
|---|
| 27 | });
|
|---|
| 28 | });
|
|---|
| 29 | });
|
|---|
| 30 | });
|
|---|
| 31 |
|
|---|
| 32 | describe('foo', function () {
|
|---|
| 33 | describe('bar', function () {
|
|---|
| 34 | describe('baz', function () {
|
|---|
| 35 | describe('qux', function () {
|
|---|
| 36 | describe('quxx', function () {
|
|---|
| 37 | describe('too many', function () {
|
|---|
| 38 | it('should get something', () => {
|
|---|
| 39 | expect(getSomething()).toBe('Something');
|
|---|
| 40 | });
|
|---|
| 41 | });
|
|---|
| 42 | });
|
|---|
| 43 | });
|
|---|
| 44 | });
|
|---|
| 45 | });
|
|---|
| 46 | });
|
|---|
| 47 | ```
|
|---|
| 48 |
|
|---|
| 49 | The following patterns are **not** considered warnings (with the default option
|
|---|
| 50 | of `{ "max": 5 } `):
|
|---|
| 51 |
|
|---|
| 52 | ```js
|
|---|
| 53 | describe('foo', () => {
|
|---|
| 54 | describe('bar', () => {
|
|---|
| 55 | it('should get something', () => {
|
|---|
| 56 | expect(getSomething()).toBe('Something');
|
|---|
| 57 | });
|
|---|
| 58 | });
|
|---|
| 59 |
|
|---|
| 60 | describe('qux', () => {
|
|---|
| 61 | it('should get something', () => {
|
|---|
| 62 | expect(getSomething()).toBe('Something');
|
|---|
| 63 | });
|
|---|
| 64 | });
|
|---|
| 65 | });
|
|---|
| 66 |
|
|---|
| 67 | describe('foo2', function () {
|
|---|
| 68 | it('should get something', () => {
|
|---|
| 69 | expect(getSomething()).toBe('Something');
|
|---|
| 70 | });
|
|---|
| 71 | });
|
|---|
| 72 |
|
|---|
| 73 | describe('foo', function () {
|
|---|
| 74 | describe('bar', function () {
|
|---|
| 75 | describe('baz', function () {
|
|---|
| 76 | describe('qux', function () {
|
|---|
| 77 | describe('this is the limit', function () {
|
|---|
| 78 | it('should get something', () => {
|
|---|
| 79 | expect(getSomething()).toBe('Something');
|
|---|
| 80 | });
|
|---|
| 81 | });
|
|---|
| 82 | });
|
|---|
| 83 | });
|
|---|
| 84 | });
|
|---|
| 85 | });
|
|---|
| 86 | ```
|
|---|
| 87 |
|
|---|
| 88 | ## Options
|
|---|
| 89 |
|
|---|
| 90 | ```json
|
|---|
| 91 | {
|
|---|
| 92 | "jest/max-nested-describe": [
|
|---|
| 93 | "error",
|
|---|
| 94 | {
|
|---|
| 95 | "max": 5
|
|---|
| 96 | }
|
|---|
| 97 | ]
|
|---|
| 98 | }
|
|---|
| 99 | ```
|
|---|
| 100 |
|
|---|
| 101 | ### `max`
|
|---|
| 102 |
|
|---|
| 103 | Enforces a maximum depth for nested `describe()`.
|
|---|
| 104 |
|
|---|
| 105 | This has a default value of `5`.
|
|---|
| 106 |
|
|---|
| 107 | Examples of patterns **not** considered warnings with options set to
|
|---|
| 108 | `{ "max": 2 }`:
|
|---|
| 109 |
|
|---|
| 110 | ```js
|
|---|
| 111 | describe('foo', () => {
|
|---|
| 112 | describe('bar', () => {
|
|---|
| 113 | it('should get something', () => {
|
|---|
| 114 | expect(getSomething()).toBe('Something');
|
|---|
| 115 | });
|
|---|
| 116 | });
|
|---|
| 117 | });
|
|---|
| 118 |
|
|---|
| 119 | describe('foo2', function () {
|
|---|
| 120 | describe('bar2', function () {
|
|---|
| 121 | it('should get something', function () {
|
|---|
| 122 | expect(getSomething()).toBe('Something');
|
|---|
| 123 | });
|
|---|
| 124 |
|
|---|
| 125 | it('should get else', function () {
|
|---|
| 126 | expect(getSomething()).toBe('Something');
|
|---|
| 127 | });
|
|---|
| 128 | });
|
|---|
| 129 | });
|
|---|
| 130 | ```
|
|---|