-
Notifications
You must be signed in to change notification settings - Fork 718
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
Best practice while querying translated strings #738
Comments
Some options:
|
@alexkrolick very interesting benchmark of all options available! Thanks for sharing! |
You're still using the same strings as the user to select the elements, it's just a matter of how you generate those strings and whether there is a single source of truth or not. Another consideration is you don't necessarily want test code to rely too much on app code, because if something goes wrong, the test may stop asserting on the right thing. That's why I suggested adding an inline snapshot of any text you generate using messageIDs, so that you know what strings are being used in the test, and as a way to debug unexpected results from the translation framework. I find a test that only consists of messageIDs is really hard to debug in 3 months when I forget what the IDs map to and have to keep opening up the message definitions. |
Querying by the actual text has this clear CON: The second approach gives me reliability about the code that is being shipped and resembles more to what the user will see in the screen. So that's why I prefer querying by the actual text. |
The sample code in
Executing the code results in this error:
|
I've run into the same issue, how is this working for you? @pedrosimao |
Fwiw I got it working with a combination of |
Doesn't that require the |
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: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?
The text was updated successfully, but these errors were encountered: