LoginSignup
5
2

More than 5 years have passed since last update.

JestのbeforeEachの実行順について

Posted at

JestのbeforeEachの実行順についてです。


describe('beforeEachの実行順確認1', () => {
  beforeEach(() => {
    console.log('aaa')
  });
  describe('グループ1', () => {
    beforeEach(() => {
      console.log('bbb')
    });
    it('テスト1', () => {
      console.log('ccc')
    });
    it('テスト2', () => {
      console.log('ddd')
    });
  });
  describe('グループ2', () => {
    it('テスト3', () => {
      console.log('eee')
    });
  });
});
describe('beforeEachの実行順確認2', () => {
  beforeEach(() => {
    console.log('fff')
  });
  it('テスト4', () => {
    console.log('ggg')
  });
});

下記、結果です。親のdescribeから実行されます。

beforeEachの実行順確認1
  グループ1
    ✓ テスト1 (44ms)
    ✓ テスト2 (17ms)
  グループ2
    ✓ テスト3 (6ms)
beforeEachの実行順確認2
  ✓ テスト4 (3ms)

console.log index.test.js:22
  aaa #テスト1
console.log index.test.js:26
  bbb #テスト1
console.log index.test.js:29
  ccc #テスト1
console.log index.test.js:22
  aaa #テスト2
console.log index.test.js:26
  bbb #テスト2
console.log index.test.js:32
  ddd #テスト2
console.log index.test.js:22
  aaa #テスト3
console.log index.test.js:37
  eee #テスト3
console.log index.test.js:43
  fff #テスト4
console.log index.test.js:46
  ggg #テスト4
5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2