testing-library / dom-testing-library

🐙 Simple and complete DOM testing utilities that encourage good testing practices.
https://testing-library.com/dom
MIT License
3.26k stars 467 forks source link

feat: Enhance `ByText` error with `selector` #1159

Closed raplemie closed 1 year ago

raplemie commented 2 years ago

What: fixes #1158

Why: The selector option behavior might not be well known by everyone reading the tests, and current test error output does not mention that additional requirement of the element being searched (which might be shown in the prettyDOM, similar reasonning to #450)

How: By adding a conditional to the error message genereated which have access to the used selector, and display it in the case it is not undefined (function called directly) and different from "*" (default selector for getByText)

Checklist:

codesandbox-ci[bot] commented 2 years ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 7054c068bca6d286748bfd7de341a47e94b90c6c:

Sandbox Source
react-testing-library-examples Configuration
codecov[bot] commented 2 years ago

Codecov Report

Merging #1159 (7054c06) into main (a9a8cf2) will not change coverage. The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main     #1159   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           24        24           
  Lines          996       998    +2     
  Branches       327       326    -1     
=========================================
+ Hits           996       998    +2     
Flag Coverage Δ
node-12 100.00% <100.00%> (ø)
node-14 100.00% <100.00%> (ø)
node-16 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/queries/text.ts 100.00% <100.00%> (ø)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

raplemie commented 1 year ago

Thanks. You've checked that docs were added but I don't see a PR to the docs page (https://github.com/testing-library/testing-library-docs/pulls?q=is%3Apr+is%3Aopen+sort%3Aupdated-desc+author%3Araplemie)?

I checked the docs checkmark because it was "handled" as N/A, not added. The error messages are not documented in the docs, so it didnt seem to require a docs change, but since the PR been sleeping for a month I thought that maybe this was automated that it had to be checked for it to process any further.

I unchecked the box, but is there a section where error messages are documented so I can follow the style ? Or do we keep the documentation as is if there is no precedent in documenting the error messages text that are returning by a query ?

eps1lon commented 1 year ago

@all-contributors add @raplemie for code

allcontributors[bot] commented 1 year ago

@eps1lon

I've put up a pull request to add @raplemie! :tada:

github-actions[bot] commented 1 year ago

:tada: This PR is included in version 8.19.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

raplemie commented 1 year ago

Thanks for merging!