testing-library / dom-testing-library

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

Add option to avoid printing DOM to log on failures #1260

Closed IanVS closed 8 months ago

IanVS commented 10 months ago

Describe the feature you'd like:

I am using this tool within Storybook, which means that it's not important for me to have a dump of the DOM output to the console every time a test fails, I can see the HTML as rendered in the page. And by having a long, long dump in the logs, it makes the test results when I run in CI more difficult to skim and find the actual error messages.

Suggested implementation:

Perhaps an option could be added to the configuration, which defaults to the current behavior, but allows turning off the DOM dump?

Describe alternatives you've considered:

Teachability, Documentation, Adoption, Migration Strategy:

MatanBobi commented 8 months ago

Hi @IanVS, sorry it took us time to get to this. You can override the getElementError in the config to remove that. I believe something like this should work:

  getElementError(message, container) {
    const error = new Error(
      [
        message,
        `Ignored nodes: comments, ${config.defaultIgnore}\n`,
      ]
        .filter(Boolean)
        .join('\n\n'),
    )
    error.name = 'TestingLibraryElementError'
    return error
  }
IanVS commented 8 months ago

Thanks for pointing me in the right direction!