eslint-plugin-jasmine icon indicating copy to clipboard operation
eslint-plugin-jasmine copied to clipboard

Enforce one assertion per test

Open alecxe opened this issue 9 years ago • 6 comments

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.

alecxe avatar Jun 21 '15 00:06 alecxe

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

tlvince avatar Jul 12 '15 20:07 tlvince

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?

alecxe avatar Jul 13 '15 01:07 alecxe

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

tlvince avatar Jul 30 '15 19:07 tlvince

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

ArnaudRinquin avatar Aug 10 '15 15:08 ArnaudRinquin

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.

burabure avatar Nov 05 '16 19:11 burabure

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.

bmingles avatar Feb 01 '17 13:02 bmingles