-
-
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
this
using inheritance causes problems
#2977
Comments
We have multiple issues already open about Mocha's Personally, I would like it to be more RSpec-like instead (reset before every test automatically, more useful and less confusing nested suite behavior that can't easily be achieved with local variables), but I'm not up for making that happen on my own, so we'd need some team consensus on it (and really, we'd need team consensus on braving the inevitable upset of some users that we changed it even if the change is justified and happens in a semver "major" release). Ultimately, unless the rest of the team ends up convinced by one case or another for a change, what I'm going to say is same thing I said to the last couple of requests about it: To get a full picture of what the pros and cons of a change are, in addition to knowing on the one hand that any change is going to cause a certain amount of backwards-compatibility backlash and on the other hand the various cases to be made for more useful behavior and that the current behavior surprises some users, is useless (or very nearly so) and doesn't match RSpec, the one last piece of info we'd need is what other RSpec-like JavaScript test runners do (especially ones that claim compatibility with Mocha). |
yes, don't use the context is tied to the suite, and it's passed around... I couldn't tell you why it's based on the suite (unless I have a whole lot of time to kill), but as @ScottFreeCode, I don't think we can really monkey with it. given that I have yet to see this particular error before, it seems others are doing a good job of avoiding this gotcha. so, yeah, it's not an ideal architecture. but don't use describe.only('Example', function() {
let value;
beforeEach(function() {
value = 5;
});
it('should have the right value', function() {
value.should.equal(5);
value = 'who cares';
});
it('should still have the right value', function() {
value.should.equal(5);
value = 'who cares';
});
describe('Inner', function() {
it('should have the right value', function() {
value.should.equal(5);
value = 'who cares';
});
it('should still have the right value but won\'t', function() {
value.should.equal(5); // success
});
})
}); (BTW, Karma has no test framework of its own; it's just a runner. You may be thinking of Jasmine.) |
The last
it
will unexpectedly fail.Using console.log, I have verified the
beforeEach
is running before each and every test and it is, so the only way this could occur is if thethis
in the Inner tests is different from the one in the outer tests and thebeforeEach
. Since values are still accessible, I am guessing Inner blocks'this
prototypically inherits from the outer block. If that is the case, why? Suites are never running simultaneously, so how would injecting the exact same object asthis
regardless of nesting cause an error? FWIW, Karma, which it seems like yourthis
is based off of, doesn't use inheritance.It also seems dangerous that you're reusing
this
but that might be a performance thing; to make tests more isolated from each other I would expect a newthis
to be instantiated for each and everyit
but that's not the case given this error.The text was updated successfully, but these errors were encountered: