|
| 1 | +# Prefer having hooks in a consistent order (`prefer-hooks-in-order`) |
| 2 | + |
| 3 | +While hooks can be setup in any order, they're always called by `jest` in this |
| 4 | +specific order: |
| 5 | + |
| 6 | +1. `beforeAll` |
| 7 | +1. `beforeEach` |
| 8 | +1. `afterEach` |
| 9 | +1. `afterAll` |
| 10 | + |
| 11 | +This rule aims to make that more obvious by enforcing grouped hooks be setup in |
| 12 | +that order within tests. |
| 13 | + |
| 14 | +## Rule Details |
| 15 | + |
| 16 | +Examples of **incorrect** code for this rule |
| 17 | + |
| 18 | +```js |
| 19 | +/* eslint jest/prefer-hooks-in-order: "error" */ |
| 20 | + |
| 21 | +describe('foo', () => { |
| 22 | + beforeEach(() => { |
| 23 | + seedMyDatabase(); |
| 24 | + }); |
| 25 | + |
| 26 | + beforeAll(() => { |
| 27 | + createMyDatabase(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('accepts this input', () => { |
| 31 | + // ... |
| 32 | + }); |
| 33 | + |
| 34 | + it('returns that value', () => { |
| 35 | + // ... |
| 36 | + }); |
| 37 | + |
| 38 | + describe('when the database has specific values', () => { |
| 39 | + const specificValue = '...'; |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + seedMyDatabase(specificValue); |
| 43 | + }); |
| 44 | + |
| 45 | + it('accepts that input', () => { |
| 46 | + // ... |
| 47 | + }); |
| 48 | + |
| 49 | + it('throws an error', () => { |
| 50 | + // ... |
| 51 | + }); |
| 52 | + |
| 53 | + afterEach(() => { |
| 54 | + clearLogger(); |
| 55 | + }); |
| 56 | + beforeEach(() => { |
| 57 | + mockLogger(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('logs a message', () => { |
| 61 | + // ... |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + afterAll(() => { |
| 66 | + removeMyDatabase(); |
| 67 | + }); |
| 68 | +}); |
| 69 | +``` |
| 70 | + |
| 71 | +Examples of **correct** code for this rule |
| 72 | + |
| 73 | +```js |
| 74 | +/* eslint jest/prefer-hooks-in-order: "error" */ |
| 75 | + |
| 76 | +describe('foo', () => { |
| 77 | + beforeAll(() => { |
| 78 | + createMyDatabase(); |
| 79 | + }); |
| 80 | + |
| 81 | + beforeEach(() => { |
| 82 | + seedMyDatabase(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('accepts this input', () => { |
| 86 | + // ... |
| 87 | + }); |
| 88 | + |
| 89 | + it('returns that value', () => { |
| 90 | + // ... |
| 91 | + }); |
| 92 | + |
| 93 | + describe('when the database has specific values', () => { |
| 94 | + const specificValue = '...'; |
| 95 | + |
| 96 | + beforeEach(() => { |
| 97 | + seedMyDatabase(specificValue); |
| 98 | + }); |
| 99 | + |
| 100 | + it('accepts that input', () => { |
| 101 | + // ... |
| 102 | + }); |
| 103 | + |
| 104 | + it('throws an error', () => { |
| 105 | + // ... |
| 106 | + }); |
| 107 | + |
| 108 | + beforeEach(() => { |
| 109 | + mockLogger(); |
| 110 | + }); |
| 111 | + |
| 112 | + afterEach(() => { |
| 113 | + clearLogger(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('logs a message', () => { |
| 117 | + // ... |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + afterAll(() => { |
| 122 | + removeMyDatabase(); |
| 123 | + }); |
| 124 | +}); |
| 125 | +``` |
| 126 | + |
| 127 | +## Also See |
| 128 | + |
| 129 | +- [`prefer-hooks-on-top`](prefer-hooks-on-top.md) |
| 130 | + |
| 131 | +## Further Reading |
| 132 | + |
| 133 | +- [Order of execution of describe and test blocks](https://jestjs.io/docs/setup-teardown#order-of-execution-of-describe-and-test-blocks) |
0 commit comments