beforeAll/beforeEach afterAll/afterEach should be inside `describe`?

Viewed 317

I am bit confused. Should beforeAll/beforeEach/afterAll/afterEach be inside describe or outside?

Example:

describe('unamed', () => {
  beforeAll(async () => {
     //
  })
})

Vs

beforeAll(async () => {
  //
})

describe('unamed', () => {
  //
})

Which is correct? Or doesn't matter?

1 Answers

If you have a before/after function at the top level of the file, it will apply to all the tests in the file. If you have them inside a describe scope, they only apply to the tests under that describe scope.

Related