Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[new rule] Disallow usage of unnecessary expectations / matchers #1616

Open
maks-rafalko opened this issue Jun 20, 2024 · 2 comments
Open

[new rule] Disallow usage of unnecessary expectations / matchers #1616

maks-rafalko opened this issue Jun 20, 2024 · 2 comments

Comments

@maks-rafalko
Copy link

maks-rafalko commented Jun 20, 2024

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
Copy link
Author

@G-Rath
Copy link
Collaborator

G-Rath commented Jul 26, 2024

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants