nodejs-integration-tests-best-practices icon indicating copy to clipboard operation
nodejs-integration-tests-best-practices copied to clipboard

Stub inside beforeAll

Open DanielGluskin opened this issue 4 years ago • 3 comments

Here we use sinon.stub in beforeAll. This way we provide only one stub for the whole suite, which cannot be restored later.

Should we always stub inside beforeEach or the arrange phase but not in beforeAll?

DanielGluskin avatar Jan 24 '21 22:01 DanielGluskin

Not sure I understand, can you please elaborate?

mikicho avatar Feb 03 '21 20:02 mikicho

@DanielGluskin I think we do this now, right?

goldbergyoni avatar Feb 05 '21 08:02 goldbergyoni

please refer here, what are your thoughts on the setup (beforeAll)? I think it is a bit problematic, as

  • we must initialize the stub before we initialize the app itself.
  • the stub is permanent, if we use sinon.restore() it won't be cleaned up (the same app instance will still be using it).
  • we might want to stub differently for each test.
  • we stub in beforeAll, not beforeEach.

I think a solution like this will be cleaner -

test('When user is not authenticated, Then ... ', () => {
   // arrange
   sinon.stub(authenticationMiddleware, 'authenticationMiddleware').callsFake((req, res, next) => res.status(401).end())

test('When user is authenticated, Then ... ', () => {
   // arrange
   sinon.stub(authenticationMiddleware, 'authenticationMiddleware').callsFake((req, res, next) => next())

DanielGluskin avatar Feb 05 '21 11:02 DanielGluskin