eslint-plugin-jasmine
eslint-plugin-jasmine copied to clipboard
Enforce one assertion per test
Would it be a good idea to have eslint-plugin-jasmine
report if there are more than one expect()
calls per it()
block? (http://blog.jayfields.com/2007/06/testing-one-assertion-per-test.html)
At least, we can have the rule disabled by default..
Thanks.
Nice idea. I've added the "help wanted" tag in case someone likes to get started on a pull request.
Thanks! Do you think it makes sense to make the number of expect()
per it()
configurable, or just force it to no more than 1?
I wouldn't personally use such a configuration, but it seems trivial to include if it's useful for people.
I could quite easily do that. The maximum count and the watched function names could be part of the configuration.
It could be useful in some very specific cases I think, but there's a lot of cases where asserting the setup of the test is very useful, or maybe we're expecting a series of things to happen.
It might be nice to have a way to assert that a particular number of expect calls were made. This is particularly useful for testing callback code.
eg. something like
foo.subscribe(function(value) {
expect(value).toBe(bar);
});
foo.subscribe(function(value) {
expect(value).toBe(bar);
});
// some way to ensure both of our expects actually got called
expect.wasCalled(2);
Could also have something like
afterEach(function() {
expect.toHaveBeenCalled();
});
I'm not too opinionated about the syntax, but having a way to verify the number of calls to expect(...) similar to how we can check for expected calls to spies would provide a lot of options here.