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

Is it really necessary to use isDone()/done()? #1830

Closed
mcometa opened this issue Dec 7, 2019 · 4 comments
Closed

Is it really necessary to use isDone()/done()? #1830

mcometa opened this issue Dec 7, 2019 · 4 comments

Comments

@mcometa
Copy link

mcometa commented Dec 7, 2019

I use nock a lot in my React testing and one thing I noticed is that there's no real convention in mock or best practices as to how you properly nock mock an API request. So, I need some help regarding this simple question - is using scope.done() or nock.isDone() or .done() really necessary? Is there a real benefit as to using it while in test aside from making sure that your mock requests get resolved mocked (and finished/"done") by nock? What am I not seeing that nock is doing behind the scenes?

Thanks~

@paulmelnikow
Copy link
Member

Is there a real benefit as to using it while in test aside from making sure that your mock requests get resolved mocked (and finished/"done") by nock?

That's it exactly!

By the way, there's some discussion on improving the "lifecycle methods" in #1753. I'd like to add a function that checks that all the mocks have been fulfilled.

@mastermatt
Copy link
Member

In test suites that have zero live-outbound traffic, I personally don't find myself using the isDone/done in isolated tests. Instead I use a global afterEach hook that calls pendingMocks and fails the test with verbiage if any mocks are returned.
It's the same effect, ensuring all declared mocks are used, but reduces boilerplate in the individual tests.

@mcometa
Copy link
Author

mcometa commented Dec 9, 2019

Thank you guys for the responses. Both are helpful!

@mcometa mcometa closed this as completed Dec 9, 2019
@nestor-lobo-work
Copy link

ou guys for the responses. Both

Is there a real benefit as to using it while in test aside from making sure that your mock requests get resolved mocked (and finished/"done") by nock?

That's it exactly!

By the way, there's some discussion on improving the "lifecycle methods" in #1753. I'd like to add a function that checks that all the mocks have been fulfilled.
Kinda of late, but trying to shed some light...

As per docs, done method will throw an exception if it has an interceptor for that scope was not called, basically if any pending mock not used, so it validates that expected interceptions were called.
I call done as a good practice on the tear down of each test, to be sure that ALL interceptors defined as expectation were called and throw an exception, avoiding hidden issues because something was not called and it was expected

On the other hand, isDone property will just return true or false depending if all interceptors defined were used (always in the context of each scope nock),
If there is a postcondition in the test case, I add a isDone assertion check before the tear down test case stage

Hope this helps others

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

4 participants