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

Asynchronous beforeEach / beforeAll #1145

Closed
tonyxiao opened this issue Jul 7, 2016 · 5 comments
Closed

Asynchronous beforeEach / beforeAll #1145

tonyxiao opened this issue Jul 7, 2016 · 5 comments

Comments

@tonyxiao
Copy link

tonyxiao commented Jul 7, 2016

Jasmine's it is able to handle async. However beforeEach, beforeAll, and describe do not seem to handle async functions properly. Sometimes test setup is asynchronous. Suggestions?

@Gerg
Copy link
Contributor

Gerg commented Jul 7, 2016

Can you provide an example of what you mean by "do not seem to handle async functions properly?" beforeEach, afterEach, beforeAll, and afterAll all support async testing via the done callback. Also, can you give an example why you might want describe to be executed asynchronously?

See documentation here: http://jasmine.github.io/2.4/introduction.html#section-Asynchronous_Support

@tonyxiao
Copy link
Author

tonyxiao commented Jul 7, 2016

@Gerg ah ok. Nvm there's actually no reason to use describe asynchronously since we'd want setup code to be done in beforeAll anyways to be more idiomatic rather than directly inside the describe method (which I sometimes do.). Speaking of which, is there any technical reason to use beforeAll rather than just write the setup code directly toward the top of the describe block before all the its?

@tonyxiao
Copy link
Author

tonyxiao commented Jul 7, 2016

And I was talking about es7 async await function support in beforeEach. Turns out I was using jasmine through jest and jest implemented additional async await support on top of its bundled version of jasmine. So never mind that either. (see jestjs/jest#1256)

@Gerg
Copy link
Contributor

Gerg commented Jul 8, 2016

I see. Jasmine doesn't currently have explicit support for many ES* features (largely due to support for legacy browsers).

The code inside your describe is executed at a different time then code inside beforeAll etc (internally called "runnables"). describe callback functions are invoked when Jasmine is building your spec tree. Your runnables run later when Jasmine is executing the tree it built. Putting code inside the runnables also has other benefits like:

  • Access to Jasmine's managed this keyword
  • Errors in the runnables will be linked to the correct specs
  • Spies created with spyOn in runnables are cleaned up
  • Custom matcher created in runnables are cleaned up

Thanks for using Jasmine!

@Gerg Gerg closed this as completed Jul 8, 2016
@tonyxiao
Copy link
Author

tonyxiao commented Jul 8, 2016

Got it. Thanks for the helpful explanation.

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

2 participants