LoginSignup
23
8

More than 5 years have passed since last update.

Jest のDescribe, Before, Afterの実行順序

Posted at

jestのdescribe, it, beforeEachの実行順序がよく分からなかったので確認した

version

jest: 23.1.0

まとめ

describeに書かれたコードが最初に実行される。
before, afterはitの実行前後に呼ばれるみたい。

確認


beforeAll(() => {
  console.log('outer before all')
})

beforeEach(() => {
  console.log('outer before each')
})

afterEach(() => {
  console.log('outer after each')
})

afterAll(() => {
  console.log('outer after all')
})

describe('some class', () => {
  console.log('describe some class')
  beforeAll(() => {
    console.log('inner before all')
  })

  beforeEach(() => {
    console.log('inner before each')
  })

  afterEach(() => {
    console.log('inner after each')
  })

  afterAll(() => {
    console.log('inner after all')
  })

  describe('some function', () => {
    console.log('describe some function')
    it('some expectation', () => {
      console.log('execute')
    })
  })
})

コンソール


 PASS  ./index.test.js
  some class
    some function
      √ some expectation (8ms)

  console.log index.test.js:18
    describe some class

  console.log index.test.js:36
    describe some function

  console.log index.test.js:2
    outer before all

  console.log index.test.js:20
    inner before all

  console.log index.test.js:6
    outer before each

  console.log index.test.js:24
    inner before each

  console.log index.test.js:38
    execute

  console.log index.test.js:28
    inner after each

  console.log index.test.js:10
    outer after each

  console.log index.test.js:32
    inner after all

  console.log index.test.js:14
    outer after all

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        3.106s
23
8
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
23
8