testing-library / testing-library/testing-library-docs

Best practice while querying translated strings

Open
#738 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
479
Forks
740
PR merge metrics
No merged PRs in 30d

Description

I am having a discussion with my team about what is the best practice when it comes to querying translated texts.

As those texts can be modified by non-developers (translators) we don't want our tests to be broken on each translation change. But a part of my teammates thinks we should keep on using plain strings, so our tests resemble user behavior...
In our current code we are doing something like this to find a button:

const buttonToFind = await screen.findByText('My button text');

We are currently using react-intl, so I proposed a solution that would go like this to find the same button:

const buttonText = getNodeText(<FormattedMessage id="buttonText" defaultMessage="Hello Button" />);
const buttonToFind = await screen.findByText(buttonText);

I think this would make the test more resilient, without breaking the rule of making tests the most similar as possible to user behavior, after all, we would be querying the same text as the user while avoiding unmaintainable strings. What do you think?

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

No file, test, or entry point is named. Start with the issue's react-intl and Testing Library query examples, then determine whether the project should document a recommendation; done means a clear, maintainable testing guideline resolves the question.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
documentation, testing-qa
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.