How to pass variable from beforeEach hook to tests in jest?

Solution 1:

Just declare sandbox so it is available in the scope of beforeEach and test:

let sandbox;

beforeEach(async () => {
  sandbox = sinon.sandbox.create()
  ...
})

test('/add', () => {
  // sandbox available for use
})