TL:DR
Jestにおいて、describe内外にbeforeAllを複数用意した場合の評価タイミングを確認した。その結果、beforeAllは上から順番に実行されていることが分かった。
モチベーション
beforeAllを上書き指定できれば、綺麗に書けるテストを書いていた。 そのため、beforeAllの優先順位や実行順序などが気になって検証した。
実行内容
コード
beforeAll(()=>{ console.log("out of describe"); }); describe('sample describe 1', () => { beforeAll(()=>{ console.log("inside of describe 1"); }); describe("sample describe 2", () => { beforeAll(() => { console.log('inside of describe 2'); }); test('sample test', () => { expect('hoge').toEqual('hoge'); }); }) });
ターミナル
$ yarn test __tests__/test.spec.ts yarn run v1.3.2 $ yarn build && yarn jest __tests__/test.spec.ts $ yarn tsc $ /path_to_dir/node_modules/.bin/tsc $ /path_to_dir/node_modules/.bin/jest __tests__/test.spec.ts PASS __tests__/test.spec.ts sample describe 1 sample describe 2 ✓ sample test (5ms) console.log __tests__/test.spec.ts:2 out of describe console.log __tests__/test.spec.ts:6 inside of describe 1 console.log __tests__/test.spec.ts:10 inside of describe 2
というように、上から順番に実行されていることが分かった。