| [9af201e] | 1 | # Disallow setup and teardown hooks (`no-hooks`)
|
|---|
| 2 |
|
|---|
| 3 | Jest provides global functions for setup and teardown tasks, which are called
|
|---|
| 4 | before/after each test case and each test suite. The use of these hooks promotes
|
|---|
| 5 | shared state between tests.
|
|---|
| 6 |
|
|---|
| 7 | ## Rule Details
|
|---|
| 8 |
|
|---|
| 9 | This rule reports for the following function calls:
|
|---|
| 10 |
|
|---|
| 11 | - `beforeAll`
|
|---|
| 12 | - `beforeEach`
|
|---|
| 13 | - `afterAll`
|
|---|
| 14 | - `afterEach`
|
|---|
| 15 |
|
|---|
| 16 | Examples of **incorrect** code for this rule:
|
|---|
| 17 |
|
|---|
| 18 | ```js
|
|---|
| 19 | /* eslint jest/no-hooks: "error" */
|
|---|
| 20 |
|
|---|
| 21 | function setupFoo(options) {
|
|---|
| 22 | /* ... */
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | function setupBar(options) {
|
|---|
| 26 | /* ... */
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | describe('foo', () => {
|
|---|
| 30 | let foo;
|
|---|
| 31 |
|
|---|
| 32 | beforeEach(() => {
|
|---|
| 33 | foo = setupFoo();
|
|---|
| 34 | });
|
|---|
| 35 |
|
|---|
| 36 | afterEach(() => {
|
|---|
| 37 | foo = null;
|
|---|
| 38 | });
|
|---|
| 39 |
|
|---|
| 40 | it('does something', () => {
|
|---|
| 41 | expect(foo.doesSomething()).toBe(true);
|
|---|
| 42 | });
|
|---|
| 43 |
|
|---|
| 44 | describe('with bar', () => {
|
|---|
| 45 | let bar;
|
|---|
| 46 |
|
|---|
| 47 | beforeEach(() => {
|
|---|
| 48 | bar = setupBar();
|
|---|
| 49 | });
|
|---|
| 50 |
|
|---|
| 51 | afterEach(() => {
|
|---|
| 52 | bar = null;
|
|---|
| 53 | });
|
|---|
| 54 |
|
|---|
| 55 | it('does something with bar', () => {
|
|---|
| 56 | expect(foo.doesSomething(bar)).toBe(true);
|
|---|
| 57 | });
|
|---|
| 58 | });
|
|---|
| 59 | });
|
|---|
| 60 | ```
|
|---|
| 61 |
|
|---|
| 62 | Examples of **correct** code for this rule:
|
|---|
| 63 |
|
|---|
| 64 | ```js
|
|---|
| 65 | /* eslint jest/no-hooks: "error" */
|
|---|
| 66 |
|
|---|
| 67 | function setupFoo(options) {
|
|---|
| 68 | /* ... */
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | function setupBar(options) {
|
|---|
| 72 | /* ... */
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | describe('foo', () => {
|
|---|
| 76 | it('does something', () => {
|
|---|
| 77 | const foo = setupFoo();
|
|---|
| 78 | expect(foo.doesSomething()).toBe(true);
|
|---|
| 79 | });
|
|---|
| 80 |
|
|---|
| 81 | it('does something with bar', () => {
|
|---|
| 82 | const foo = setupFoo();
|
|---|
| 83 | const bar = setupBar();
|
|---|
| 84 | expect(foo.doesSomething(bar)).toBe(true);
|
|---|
| 85 | });
|
|---|
| 86 | });
|
|---|
| 87 | ```
|
|---|
| 88 |
|
|---|
| 89 | ## Options
|
|---|
| 90 |
|
|---|
| 91 | ```json
|
|---|
| 92 | {
|
|---|
| 93 | "jest/no-hooks": [
|
|---|
| 94 | "error",
|
|---|
| 95 | {
|
|---|
| 96 | "allow": ["afterEach", "afterAll"]
|
|---|
| 97 | }
|
|---|
| 98 | ]
|
|---|
| 99 | }
|
|---|
| 100 | ```
|
|---|
| 101 |
|
|---|
| 102 | ### `allow`
|
|---|
| 103 |
|
|---|
| 104 | This array option controls which Jest hooks are checked by this rule. There are
|
|---|
| 105 | four possible values:
|
|---|
| 106 |
|
|---|
| 107 | - `"beforeAll"`
|
|---|
| 108 | - `"beforeEach"`
|
|---|
| 109 | - `"afterAll"`
|
|---|
| 110 | - `"afterEach"`
|
|---|
| 111 |
|
|---|
| 112 | By default, none of these options are enabled (the equivalent of
|
|---|
| 113 | `{ "allow": [] }`).
|
|---|
| 114 |
|
|---|
| 115 | Examples of **incorrect** code for the `{ "allow": ["afterEach"] }` option:
|
|---|
| 116 |
|
|---|
| 117 | ```js
|
|---|
| 118 | /* eslint jest/no-hooks: ["error", { "allow": ["afterEach"] }] */
|
|---|
| 119 |
|
|---|
| 120 | function setupFoo(options) {
|
|---|
| 121 | /* ... */
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | let foo;
|
|---|
| 125 |
|
|---|
| 126 | beforeEach(() => {
|
|---|
| 127 | foo = setupFoo();
|
|---|
| 128 | });
|
|---|
| 129 |
|
|---|
| 130 | afterEach(() => {
|
|---|
| 131 | jest.resetModules();
|
|---|
| 132 | });
|
|---|
| 133 |
|
|---|
| 134 | test('foo does this', () => {
|
|---|
| 135 | // ...
|
|---|
| 136 | });
|
|---|
| 137 |
|
|---|
| 138 | test('foo does that', () => {
|
|---|
| 139 | // ...
|
|---|
| 140 | });
|
|---|
| 141 | ```
|
|---|
| 142 |
|
|---|
| 143 | Examples of **correct** code for the `{ "allow": ["afterEach"] }` option:
|
|---|
| 144 |
|
|---|
| 145 | ```js
|
|---|
| 146 | /* eslint jest/no-hooks: ["error", { "allow": ["afterEach"] }] */
|
|---|
| 147 |
|
|---|
| 148 | function setupFoo(options) {
|
|---|
| 149 | /* ... */
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | afterEach(() => {
|
|---|
| 153 | jest.resetModules();
|
|---|
| 154 | });
|
|---|
| 155 |
|
|---|
| 156 | test('foo does this', () => {
|
|---|
| 157 | const foo = setupFoo();
|
|---|
| 158 | // ...
|
|---|
| 159 | });
|
|---|
| 160 |
|
|---|
| 161 | test('foo does that', () => {
|
|---|
| 162 | const foo = setupFoo();
|
|---|
| 163 | // ...
|
|---|
| 164 | });
|
|---|
| 165 | ```
|
|---|
| 166 |
|
|---|
| 167 | ## When Not To Use It
|
|---|
| 168 |
|
|---|
| 169 | If you prefer using the setup and teardown hooks provided by Jest, you can
|
|---|
| 170 | safely disable this rule.
|
|---|
| 171 |
|
|---|
| 172 | ## Further Reading
|
|---|
| 173 |
|
|---|
| 174 | - [Jest docs - Setup and Teardown](https://facebook.github.io/jest/docs/en/setup-teardown.html)
|
|---|