-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Cross test pollution through the runnable context #587
Comments
that's the point of the context, is to persist. You can easily re-define these things in beforeEach hooks if that's the desired effect |
This behavior of the context, though, is strange in that it encourages I do think that this persistent context should be re-thought a bit, and if That way, this possibly polluted space could at least be namespaced and |
I dont use the context at all personal, I just use closures, the context is helpful for the "exports" ui etc though. Plus when would you not be re-defining something in a beforeEach etc? that seems odd, otherwise you wouldn't use the context at all. I dont see why you would use it in a test case like you have there |
We did just use closures in the past, but that also encouraged accidental Now that we have a spec suite that just broke through 2000 tests, and have We are loving mocha, by the way, as it runs through those 2k+ specs in |
I haven't used it for this purpose personally but I know some people are using it to perform teardowns in after() / afterEach(), for before/after each we could easily |
@JamesMaroney is right. Tests should be isolated, and the test runner should make it easy to do the right thing, and let you know when you did something you didn't intend. Consider the following:
I've forgotten the |
Here's my suggestion:
|
I'm +1 on avoiding shared contexts. @visionmedia, to answer your question: yes, beforeEach hook should be setting things up, but if the setup gets complicated, and you're moving code around between suites, it's easy to forget something and get surprising cross-test dependencies. As a use case, I love to add some helper funcs to the context along the lines of Mocha actually has a test for cross-test pollution in test/acceptance/context.js. What we're proposing is to replace this behavior with the opposite one, creating a test-local context for each test (possibly using the global context as its prototype). Old:
New:
|
@andreyvit, @jfirebaugh , and @visionmedia It basically makes each suite's context prototypically inherit from the parent suite's context. This does not guarentee context not to be shared between tests but it does at least limit cross pollution between test suites. |
@park9140 I think it's a really good idea. Why each suite, though, and not each test? I would expect the context of each test to be separate. Do other people have use cases for a shared context? |
@andreyvit the shared test context is maintained because it is a useful pattern to be able to easily share one before setup when you are doing complex bundles of testing against a single setup. This allows us to do integration style tests against a class that has been set up in a single state without having to waist execution time in setup for each test. With this fix in place you could easily wrap each set of tests you want to have a shared setup in a new suite describing the setup being done and then execute tests. At work we try to ensure that absolutely no setup ever happens in a test which helps keep the tests themselves nice and clean, so I never really need individual tests to have a separate context. |
When building specs, we tend to build up our code under test and attach it to the running context. Sadly, we've noticed that this context is shared across test runs. Here's a minimal failing spec that shows the issue:
As a stop-gap fix, I added an event handler to the 'test' event which cleans out the test context (keeping your internal keys). I'll put up a pull request with this just in case you like that approach.
The text was updated successfully, but these errors were encountered: