tlvince / eslint-plugin-jasmine

ESLint rules for Jasmine
https://www.npmjs.com/package/eslint-plugin-jasmine
MIT License
95 stars 58 forks source link

Enforce one assertion per test #5

Open alecxe opened 9 years ago

alecxe commented 9 years ago

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.

tlvince commented 9 years ago

Nice idea. I've added the "help wanted" tag in case someone likes to get started on a pull request.

alecxe commented 9 years ago

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?

tlvince commented 9 years ago

I wouldn't personally use such a configuration, but it seems trivial to include if it's useful for people.

ArnaudRinquin commented 9 years ago

I could quite easily do that. The maximum count and the watched function names could be part of the configuration.

burabure commented 7 years ago

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.

bmingles commented 7 years ago

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.