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

prefer-in-document: toHaveLength with no args causing crash and commas arent removed if multiple args were there #277

Closed
berkinanik opened this issue Nov 8, 2022 · 1 comment · Fixed by #276

Comments

@berkinanik
Copy link
Contributor

  • eslint-plugin-jest-dom version: 4.0.2
  • node version: 16.17.0
  • npm version: 8.19.0

Relevant code or config

test for following component will fail since jest matcher toHaveLength doesn't have its required expected:number parameter:

export default function Home() {
  const items = [
    'item',
    'item1',
    'item3',
    'item2',
    'item4',
  ];

  return (
    <div className={styles.container}>
      <ul>
        {items.map((item, index) => (
          <li key={item}>
            {' '}
            {`${item}: #${index}`}
            {' '}
          </li>
        ))}
      </ul>
    </div>
  );
}

but eslint will crash because: prefer-in-document rules makes it crash while parsing the code. since no argument is in the matcherArguments, matcherArguments[0].value is .value on undefined

describe('Home', () => {
  it('renders the list items', () => {
    render(<Home />);

    const list = screen.getByRole('list');

    expect(list).toBeInTheDocument();

    const listItems = screen.queryAllByRole('listitem');

    expect(listItems).toHaveLength();
  });
});

Also, following lines will be replaced by prefer-in-document falsely:

const listItems = screen.queryAllByRole('listitem');
expect(listItems).toHaveLength(1, 2, 3);

fixed to:

const listItems = screen.queryByRole('listitem');
expect(listItems).toBeInTheDocument(, , );

commas are still there.

What I did and Suggested solution:
Fix (#276)

Reproduction repository:
https://github.com/berkinanik/with-jest-app

@G-Rath
Copy link
Collaborator

G-Rath commented Nov 10, 2022

Resolved by #276

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