|
| 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_ |
| 23 | +- Types |
| 24 | +- Calls to the standard Jest globals |
| 25 | + |
| 26 | +This rule flags any function calls within test files that are directly within |
| 27 | +the body of a `describe`, and suggests wrapping them in one of the four |
| 28 | +lifecycle hooks. |
| 29 | + |
| 30 | +Here is a slightly contrived test file showcasing some common cases that would |
| 31 | +be flagged: |
| 32 | + |
| 33 | +```js |
| 34 | +import { database, isCity } from '../database'; |
| 35 | +import { Logger } from '../../../src/Logger'; |
| 36 | +import { loadCities } from '../api'; |
| 37 | + |
| 38 | +jest.mock('../api'); |
| 39 | + |
| 40 | +const initializeCityDatabase = () => { |
| 41 | + database.addCity('Vienna'); |
| 42 | + database.addCity('San Juan'); |
| 43 | + database.addCity('Wellington'); |
| 44 | +}; |
| 45 | + |
| 46 | +const clearCityDatabase = () => { |
| 47 | + database.clear(); |
| 48 | +}; |
| 49 | + |
| 50 | +initializeCityDatabase(); |
| 51 | + |
| 52 | +test('that persists cities', () => { |
| 53 | + expect(database.cities.length).toHaveLength(3); |
| 54 | +}); |
| 55 | + |
| 56 | +test('city database has Vienna', () => { |
| 57 | + expect(isCity('Vienna')).toBeTruthy(); |
| 58 | +}); |
| 59 | + |
| 60 | +test('city database has San Juan', () => { |
| 61 | + expect(isCity('San Juan')).toBeTruthy(); |
| 62 | +}); |
| 63 | + |
| 64 | +describe('when loading cities from the api', () => { |
| 65 | + let consoleWarnSpy = jest.spyOn(console, 'warn'); |
| 66 | + |
| 67 | + loadCities.mockResolvedValue(['Wellington', 'London']); |
| 68 | + |
| 69 | + it('does not duplicate cities', async () => { |
| 70 | + await database.loadCities(); |
| 71 | + |
| 72 | + expect(database.cities).toHaveLength(4); |
| 73 | + }); |
| 74 | + |
| 75 | + it('logs any duplicates', async () => { |
| 76 | + await database.loadCities(); |
| 77 | + |
| 78 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 79 | + 'Ignored duplicate cities: Wellington', |
| 80 | + ); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +clearCityDatabase(); |
| 85 | +``` |
| 86 | + |
| 87 | +Here is the same slightly contrived test file showcasing the same common cases |
| 88 | +but in ways that would be **not** flagged: |
| 89 | + |
| 90 | +```js |
| 91 | +import { database, isCity } from '../database'; |
| 92 | +import { Logger } from '../../../src/Logger'; |
| 93 | +import { loadCities } from '../api'; |
| 94 | + |
| 95 | +jest.mock('../api'); |
| 96 | + |
| 97 | +const initializeCityDatabase = () => { |
| 98 | + database.addCity('Vienna'); |
| 99 | + database.addCity('San Juan'); |
| 100 | + database.addCity('Wellington'); |
| 101 | +}; |
| 102 | + |
| 103 | +const clearCityDatabase = () => { |
| 104 | + database.clear(); |
| 105 | +}; |
| 106 | + |
| 107 | +beforeEach(() => { |
| 108 | + initializeCityDatabase(); |
| 109 | +}); |
| 110 | + |
| 111 | +test('that persists cities', () => { |
| 112 | + expect(database.cities.length).toHaveLength(3); |
| 113 | +}); |
| 114 | + |
| 115 | +test('city database has Vienna', () => { |
| 116 | + expect(isCity('Vienna')).toBeTruthy(); |
| 117 | +}); |
| 118 | + |
| 119 | +test('city database has San Juan', () => { |
| 120 | + expect(isCity('San Juan')).toBeTruthy(); |
| 121 | +}); |
| 122 | + |
| 123 | +describe('when loading cities from the api', () => { |
| 124 | + let consoleWarnSpy; |
| 125 | + |
| 126 | + beforeEach(() => { |
| 127 | + consoleWarnSpy = jest.spyOn(console, 'warn'); |
| 128 | + loadCities.mockResolvedValue(['Wellington', 'London']); |
| 129 | + }); |
| 130 | + |
| 131 | + it('does not duplicate cities', async () => { |
| 132 | + await database.loadCities(); |
| 133 | + |
| 134 | + expect(database.cities).toHaveLength(4); |
| 135 | + }); |
| 136 | + |
| 137 | + it('logs any duplicates', async () => { |
| 138 | + await database.loadCities(); |
| 139 | + |
| 140 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 141 | + 'Ignored duplicate cities: Wellington', |
| 142 | + ); |
| 143 | + }); |
| 144 | +}); |
| 145 | + |
| 146 | +afterEach(() => { |
| 147 | + clearCityDatabase(); |
| 148 | +}); |
| 149 | +``` |
0 commit comments