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

[new rule] Disallow usage of unnecessary expectations / matchers

Open maks-rafalko opened this issue 1 year ago • 2 comments

Currently, the following code doesn't produce any errors neither from TS, nor from @typescript/eslint and eslint-plugin-jest:

const getNames = (): string[] => {
  return ['one', 'two'];
};

test('...', () => {
  const names = getNames();

  expect(names).toBeDefined();
});

Note the string[] return type of getNames() function. It's literally always "defined" so .toBeDefined() does not make any sense here, but neither TS nor ESLint complain about it, giving developers a false feeling of good tests.

Actual result: no errors, tests obviously pass while they make no sense.

Expected result: linter error saying Type of "app" variable is "TestingModule", it can not be undefined. It is useless to check it with .toBeDefined() matcher.

Example: image

Do you think eslint-plugin-jest is the right place to have a new rule, checking already known types of variables and checking matchers, disallowing useless usage?

Some expiration for implementation can be found in typescript-eslint's no-unnecessary-type-assertion

maks-rafalko avatar Jun 20 '24 09:06 maks-rafalko

UPD: see https://github.com/maks-rafalko/eslint-plugin-proper-tests?tab=readme-ov-file#rules

maks-rafalko avatar Jul 11 '24 13:07 maks-rafalko

@maks-rafalko sorry for the late reply - I'd be willing to explore this especially since you've already got working rules. Would you be open to porting them over to here?

G-Rath avatar Jul 26 '24 23:07 G-Rath