Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
Is before() just for semantic benefit? Why not just set up the player var before the specs w/o wrapping it in before()?
I understand how beforeEach() can be useful but what does before() really do for you? In this example, you can take away the before and simply define the player variable above the specs and they will still have access to it and the test passes.
Gabbie Metheny33,778 Points
Guil doesn't go into it in detail, but in the next video he talks about teardown using
afterEach. These methods can be used to clean up after your tests run. That's not needed in this project, but he's probably introducing it as a best practice since a more complicated project might involve interacting with the DOM or modifying a pretend database.
The big reason (in my eyes) is that because all tests are placed in callbacks, by using
before(), you'll allow the testing framework to make sure things run at the right time. Perhaps not as important for
before() in every case, but critical for other hooks.
Steven Parker228,991 Points
It may make no difference to the running of the test, but if you're establishing conditions specifically for the test it would make sense to set them up in a "before" rather than outside of the test code.
I haven't tried it, but I suspect it would also prevent anything from being added to the global namespace.