Skip to content

Enforce consistent assertions  #218

Closed
@skovy

Description

@skovy

The prefer-explicit-assert rule enforces an explicit assertion but allows a large number of possible assertions. For example:

expect(getByText('foo')).toBeDefined();
expect(getByText('foo')).toBeTruthy();
expect(getByText('foo')).not.toBeNull();
expect(getByText('foo')).toBeInTheDocument();

Functionally, these are all the same (since getBy* throws) but in a large codebase, it's possible these can all be used interchangeably. It would be nice to enforce a consistent assertion on getBy* queries. Even though these behave the same with getBy* (since it throws or always returns something) it's misleading mixing them since toBeDefined is explicitly only checking that it's not undefined, but toBeTruthy is checking that it's not six values: false, 0, '', null, undefined, and NaN.

One way this could be added is by adding a new configuration option:

"testing-library/prefer-explicit-assert": ["error", {"assertion": "toBeInTheDocument"}],

Or, it could be added as a new rule (eg: prefer-consistent-assert).

Happy to put together a PR that makes these changes, but wanted to get feedback if this aligns with the intent of these linting rules and also the preferred implementation?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions