testing-library / testing-library/dom-testing-library

Support for sharing matching details between getByRole & queryByRole

Open
#1,227 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
JavaScript
Stars
3.3k
Forks
474
PR merge metrics
No merged PRs in 30d

Description

Describe the feature you'd like:

When writing tests we'll often write a positive test i.e. something appears when we expect it to with a corresponding negative test i.e. it doesn't appear when it shouldn't.

For various reasons we want to keep things DRY (e.g. if the text changes, we don't want false-negatives in the negative test).

At the moment, the best we can come up with is

const warningMatcher: [ByRoleMatcher, ByRoleOptions] = [
  (content, element) => content === "status" && element?.textContent?.includes("The file has warning(s) that may affect how your data will import"),
  {}
];

Then

// Positive case i.e. we're looking for the warning
expect(screen.getByRole(...warningMatcher)).toBeInTheDocument();

// Negative case i.e. we're asserting the warning does not appear
expect(screen.queryByRole(...warningMatcher)).not.toBeInTheDocument();

Wondering if API changes could make this (surely common?) use-case easier.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining the getByRole and queryByRole entry points and how they currently accept matchers and options. Compare the positive and negative examples in the issue, then define an API that lets both calls share the same matching details; done means the same matcher can be reused without duplicating its setup.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
testing-qa
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.