avajs / eslint-plugin-ava

ESLint rules for AVA
https://avajs.dev
MIT License
230 stars 49 forks source link

Rule proposal: Forbid nested assertions #150

Open jfmengels opened 8 years ago

jfmengels commented 8 years ago

I have just received a PR where the contributor added a test resembling this:

test('should foo...', t => {
  t.is(
     t.throws(() => {
       configFile.readConfig(absentFile);
     }).message,
     'Could not find X'
   );
});

This could obviously be simplified to

t.throws(() => {
  configFile.readConfig(absentFile);
}, 'Could not find X');

I think this could have been prevented by telling the user not to use an assertion inside another assertion, though I'm not sure whether the error message would have helped the user know how else to do this.

novemberborn commented 8 years ago

Could make it specific to t.throws, and say "assign t.throws() result to a variable before combining it with another assertion", or something like that?

jfmengels commented 8 years ago

True, the message would be a lot nicer then (though even with a more generic rule, we can still have different error messages).

Unless someone has other ideas of how assertions could be nested together except with t.throws, I'm leaning towards what @novemberborn said, having one rule for this specific case.

Actually, I do have a different case in mind, but also dealing with t.throws:

test('foo', t => {
  t.throws(() => {
    t.is(1, 2);
  });
});