| [9af201e] | 1 | # Require setup and teardown code to be within a hook (`require-hook`)
|
|---|
| 2 |
|
|---|
| 3 | Often while writing tests you have some setup work that needs to happen before
|
|---|
| 4 | tests run, and you have some finishing work that needs to happen after tests
|
|---|
| 5 | run. Jest provides helper functions to handle this.
|
|---|
| 6 |
|
|---|
| 7 | It's common when writing tests to need to perform setup work that needs to
|
|---|
| 8 | happen before tests run, and finishing work after tests run.
|
|---|
| 9 |
|
|---|
| 10 | Because Jest executes all `describe` handlers in a test file _before_ it
|
|---|
| 11 | executes any of the actual tests, it's important to ensure setup and teardown
|
|---|
| 12 | work is done inside `before*` and `after*` handlers respectively, rather than
|
|---|
| 13 | inside the `describe` blocks.
|
|---|
| 14 |
|
|---|
| 15 | ## Rule details
|
|---|
| 16 |
|
|---|
| 17 | This rule flags any expression that is either at the toplevel of a test file or
|
|---|
| 18 | directly within the body of a `describe`, _except_ for the following:
|
|---|
| 19 |
|
|---|
| 20 | - `import` statements
|
|---|
| 21 | - `const` variables
|
|---|
| 22 | - `let` _declarations_, and initializations to `null` or `undefined`
|
|---|
| 23 | - Classes
|
|---|
| 24 | - Types
|
|---|
| 25 | - Calls to the standard Jest globals
|
|---|
| 26 |
|
|---|
| 27 | This rule flags any function calls within test files that are directly within
|
|---|
| 28 | the body of a `describe`, and suggests wrapping them in one of the four
|
|---|
| 29 | lifecycle hooks.
|
|---|
| 30 |
|
|---|
| 31 | Here is a slightly contrived test file showcasing some common cases that would
|
|---|
| 32 | be flagged:
|
|---|
| 33 |
|
|---|
| 34 | ```js
|
|---|
| 35 | import { database, isCity } from '../database';
|
|---|
| 36 | import { Logger } from '../../../src/Logger';
|
|---|
| 37 | import { loadCities } from '../api';
|
|---|
| 38 |
|
|---|
| 39 | jest.mock('../api');
|
|---|
| 40 |
|
|---|
| 41 | const initializeCityDatabase = () => {
|
|---|
| 42 | database.addCity('Vienna');
|
|---|
| 43 | database.addCity('San Juan');
|
|---|
| 44 | database.addCity('Wellington');
|
|---|
| 45 | };
|
|---|
| 46 |
|
|---|
| 47 | const clearCityDatabase = () => {
|
|---|
| 48 | database.clear();
|
|---|
| 49 | };
|
|---|
| 50 |
|
|---|
| 51 | initializeCityDatabase();
|
|---|
| 52 |
|
|---|
| 53 | test('that persists cities', () => {
|
|---|
| 54 | expect(database.cities.length).toHaveLength(3);
|
|---|
| 55 | });
|
|---|
| 56 |
|
|---|
| 57 | test('city database has Vienna', () => {
|
|---|
| 58 | expect(isCity('Vienna')).toBeTruthy();
|
|---|
| 59 | });
|
|---|
| 60 |
|
|---|
| 61 | test('city database has San Juan', () => {
|
|---|
| 62 | expect(isCity('San Juan')).toBeTruthy();
|
|---|
| 63 | });
|
|---|
| 64 |
|
|---|
| 65 | describe('when loading cities from the api', () => {
|
|---|
| 66 | let consoleWarnSpy = jest.spyOn(console, 'warn');
|
|---|
| 67 |
|
|---|
| 68 | loadCities.mockResolvedValue(['Wellington', 'London']);
|
|---|
| 69 |
|
|---|
| 70 | it('does not duplicate cities', async () => {
|
|---|
| 71 | await database.loadCities();
|
|---|
| 72 |
|
|---|
| 73 | expect(database.cities).toHaveLength(4);
|
|---|
| 74 | });
|
|---|
| 75 |
|
|---|
| 76 | it('logs any duplicates', async () => {
|
|---|
| 77 | await database.loadCities();
|
|---|
| 78 |
|
|---|
| 79 | expect(consoleWarnSpy).toHaveBeenCalledWith(
|
|---|
| 80 | 'Ignored duplicate cities: Wellington',
|
|---|
| 81 | );
|
|---|
| 82 | });
|
|---|
| 83 | });
|
|---|
| 84 |
|
|---|
| 85 | clearCityDatabase();
|
|---|
| 86 | ```
|
|---|
| 87 |
|
|---|
| 88 | Here is the same slightly contrived test file showcasing the same common cases
|
|---|
| 89 | but in ways that would be **not** flagged:
|
|---|
| 90 |
|
|---|
| 91 | ```js
|
|---|
| 92 | import { database, isCity } from '../database';
|
|---|
| 93 | import { Logger } from '../../../src/Logger';
|
|---|
| 94 | import { loadCities } from '../api';
|
|---|
| 95 |
|
|---|
| 96 | jest.mock('../api');
|
|---|
| 97 |
|
|---|
| 98 | const initializeCityDatabase = () => {
|
|---|
| 99 | database.addCity('Vienna');
|
|---|
| 100 | database.addCity('San Juan');
|
|---|
| 101 | database.addCity('Wellington');
|
|---|
| 102 | };
|
|---|
| 103 |
|
|---|
| 104 | const clearCityDatabase = () => {
|
|---|
| 105 | database.clear();
|
|---|
| 106 | };
|
|---|
| 107 |
|
|---|
| 108 | beforeEach(() => {
|
|---|
| 109 | initializeCityDatabase();
|
|---|
| 110 | });
|
|---|
| 111 |
|
|---|
| 112 | test('that persists cities', () => {
|
|---|
| 113 | expect(database.cities.length).toHaveLength(3);
|
|---|
| 114 | });
|
|---|
| 115 |
|
|---|
| 116 | test('city database has Vienna', () => {
|
|---|
| 117 | expect(isCity('Vienna')).toBeTruthy();
|
|---|
| 118 | });
|
|---|
| 119 |
|
|---|
| 120 | test('city database has San Juan', () => {
|
|---|
| 121 | expect(isCity('San Juan')).toBeTruthy();
|
|---|
| 122 | });
|
|---|
| 123 |
|
|---|
| 124 | describe('when loading cities from the api', () => {
|
|---|
| 125 | let consoleWarnSpy;
|
|---|
| 126 |
|
|---|
| 127 | beforeEach(() => {
|
|---|
| 128 | consoleWarnSpy = jest.spyOn(console, 'warn');
|
|---|
| 129 | loadCities.mockResolvedValue(['Wellington', 'London']);
|
|---|
| 130 | });
|
|---|
| 131 |
|
|---|
| 132 | it('does not duplicate cities', async () => {
|
|---|
| 133 | await database.loadCities();
|
|---|
| 134 |
|
|---|
| 135 | expect(database.cities).toHaveLength(4);
|
|---|
| 136 | });
|
|---|
| 137 |
|
|---|
| 138 | it('logs any duplicates', async () => {
|
|---|
| 139 | await database.loadCities();
|
|---|
| 140 |
|
|---|
| 141 | expect(consoleWarnSpy).toHaveBeenCalledWith(
|
|---|
| 142 | 'Ignored duplicate cities: Wellington',
|
|---|
| 143 | );
|
|---|
| 144 | });
|
|---|
| 145 | });
|
|---|
| 146 |
|
|---|
| 147 | afterEach(() => {
|
|---|
| 148 | clearCityDatabase();
|
|---|
| 149 | });
|
|---|
| 150 | ```
|
|---|
| 151 |
|
|---|
| 152 | ## Options
|
|---|
| 153 |
|
|---|
| 154 | If there are methods that you want to call outside of hooks and tests, you can
|
|---|
| 155 | mark them as allowed using the `allowedFunctionCalls` option.
|
|---|
| 156 |
|
|---|
| 157 | ```json
|
|---|
| 158 | {
|
|---|
| 159 | "jest/require-hook": [
|
|---|
| 160 | "error",
|
|---|
| 161 | {
|
|---|
| 162 | "allowedFunctionCalls": ["enableAutoDestroy"]
|
|---|
| 163 | }
|
|---|
| 164 | ]
|
|---|
| 165 | }
|
|---|
| 166 | ```
|
|---|
| 167 |
|
|---|
| 168 | Examples of **correct** code when using
|
|---|
| 169 | `{ "allowedFunctionCalls": ["enableAutoDestroy"] }` option:
|
|---|
| 170 |
|
|---|
| 171 | ```js
|
|---|
| 172 | /* eslint jest/require-hook: ["error", { "allowedFunctionCalls": ["enableAutoDestroy"] }] */
|
|---|
| 173 |
|
|---|
| 174 | import { enableAutoDestroy, mount } from '@vue/test-utils';
|
|---|
| 175 | import { initDatabase, tearDownDatabase } from './databaseUtils';
|
|---|
| 176 |
|
|---|
| 177 | enableAutoDestroy(afterEach);
|
|---|
| 178 |
|
|---|
| 179 | beforeEach(initDatabase);
|
|---|
| 180 | afterEach(tearDownDatabase);
|
|---|
| 181 |
|
|---|
| 182 | describe('Foo', () => {
|
|---|
| 183 | test('always returns 42', () => {
|
|---|
| 184 | expect(global.getAnswer()).toBe(42);
|
|---|
| 185 | });
|
|---|
| 186 | });
|
|---|
| 187 | ```
|
|---|