Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stub inside beforeAll #67

Open
DanielGluskin opened this issue Jan 24, 2021 · 3 comments
Open

Stub inside beforeAll #67

DanielGluskin opened this issue Jan 24, 2021 · 3 comments

Comments

@DanielGluskin
Copy link
Collaborator

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?

@mikicho
Copy link
Contributor

mikicho commented Feb 3, 2021

Not sure I understand, can you please elaborate?

@goldbergyoni
Copy link
Contributor

@DanielGluskin I think we do this now, right?

@DanielGluskin
Copy link
Collaborator Author

DanielGluskin commented Feb 5, 2021

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())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants