react-testing-library: some portion of debug's output is not visible
Asked Answered
M

12

185

I am using react jest with react testing library to test my component. I am facing a weird issue. I am using debug, returned by render from testing-library.

test('component should work', async () => {
  const { findByText, debug } = render(<MyComponent />);
  const myElement = await findByText(/someText/i);
  debug();

});

enter image description here

As you can see in the screenshot there are incomplete divs and closing tags for parents are missing.

Mainis answered 16/1, 2020 at 5:54 Comment(3)
have you tried increasing the DEBUG_PRINT_LIMIT as mentioned in hereTureen
@Tureen no luck with DEBUG_PRINT_LIMIT, still same issue.Mainis
You should be able to do so by doing: screen.debug(myComponent, Infinity); note: you can specify "undefined" instead of "myComponent" to debug the whole document.Cockup
W
165

You need to change the value of DEBUG_PRINT_LIMIT env variable (default is 7000).

For example, run your tests with: DEBUG_PRINT_LIMIT=100000 yarn test

Source: https://github.com/testing-library/dom-testing-library/blob/2653fd934f33ce19377c98029efe3e983a1c602b/src/pretty-dom.js#L50

Wang answered 8/7, 2020 at 11:48 Comment(1)
If this isn't working for others, note that this limit is characters to print and not linesSubreption
T
154

I am using this version: "@testing-library/react": "^11.0.4"

able to do just like below, we can change 300000 as the max length of output.

debug(undefined, 300000)  
Twelfthtide answered 14/10, 2020 at 3:9 Comment(5)
debug(undefined, 300000)Dimity
@J.Munson why undefined?Balloon
To make typescript happy, basically. But in general, undefined is the standard way of omitting a parameter in Javascript.Dimity
where (file) do you call this?Allseed
answered below by @labsElementary
T
85

Another option

screen.debug(undefined, Infinity);

Taranto answered 22/11, 2021 at 12:21 Comment(0)
E
39

The second argument of the debug() function can be used to set maxLengthToPrint.

E.g. to print everything in myElement using the recommended screen approach:

import {render, screen} from '@testing-library/react'

render(<MyComponent />);
const myElement = await screen.findByText(/someText/i);

const maxLengthToPrint = 100000
screen.debug(myElement, maxLengthToPrint);

See:

Eldwon answered 1/9, 2020 at 20:51 Comment(1)
logged in just to upvote this answer... the others didn't explain how or where to call screen.debug(undefined, Infinity);, I thought we needed to pass undefined there since there was no explanation.Telegraphese
A
19

You can use Jest Preview (https://github.com/nvh95/jest-preview) to view your app UI when testing in a browser, instead of HTML in the terminal, just by 2 lines of code:

import { debug } from 'jest-preview';

describe('App', () => {
  it('should work as expected', () => {
    render(<App />);
    debug();
  });
});

Jest Preview debug react testing library

It works great with jest and react-testing-library.

Annotation answered 10/6, 2022 at 4:14 Comment(3)
very interestingGinzburg
it does not work for react native, uses jsdom and documentSenghor
Wow neat✨. For those getting started, note you must also run jest-preview server for this to work.Heirdom
T
10

Since the DOM size can get really large, you can set the limit of DOM content to be printed via environment variable DEBUG_PRINT_LIMIT. The default value is 7000. You will see ... in the console, when the DOM content is stripped off, because of the length you have set or due to default size limit. Here's how you might increase this limit when running tests:

DEBUG_PRINT_LIMIT=10000 npm test

Here more about debuggin on documentation

Taggart answered 10/11, 2022 at 13:53 Comment(1)
This worked out for me, just put this code before my test DEBUG_PRINT_LIMIT=10000 pnpm test "file path"Football
C
6

If none of the other answers work for you, make sure it's not your terminal limit. For example VS Code only keeps 5000 lines in buffer. Try Mac OS terminal.

Criseyde answered 24/9, 2021 at 18:51 Comment(0)
I
6

This worked for me:

debug(undefined, 300000);

It will give you the markeup of whatever you passed into render() as:

import {render, screen} from '@testing-library/react'

render(<MyComponent />);

You can read about more ways to help you with printing out the results, including prettifying the resulting markup at:

API doc for debug

Identify answered 17/1, 2022 at 0:44 Comment(1)
I added screen for this to work as I needed: screen.debug(undefined, 300000);Football
R
3

This worked for me

const history = createMemoryHistory()
const { debug } = renderWithRedux(
    <Router history={history}>
        <SideBar />
    </Router>
, state);

screen.debug(debug(), 20000);
Radiator answered 8/10, 2020 at 11:27 Comment(0)
S
2

By default RTL doesn't show comments, <script /> and <style /> tags. In my case I needed to test for a commented node in the DOM.

If you want your tests to include all the nodes, you can use prettyDOM like this:

// render DOM with a commented node
const html = {__html: '<!--this is a commented DOM element-->'};
const element = <div dangerouslySetInnerHTML={html} />;
const { container } = render(element);

// This is what tells RLT to print all nodes!
const prettyfiedDOM = prettyDOM(container, undefined, { filterNode: () => true}) || '';

expect(prettyfiedDOM.includes('<!--this is a commented DOM element-->')).toBeTruthy();

Notice that the filterNode function always returns true, which tells RTL to print all DOM nodes, and hence you can test comments, styles, tags, etc. You can read more on prettyDOM source code and configure your desired behavior of DOM filtering.

View the live demo here

Hope that helps!

Swearword answered 30/8, 2022 at 0:14 Comment(0)
H
1

Adding on top of answer by @Haryono

Also make sure you don't have silent flag set in scripts

"test": "jest --config jest.config.js --silent";

Removing silent flag should work.

Note: I think silent flag supresses warnings and debug outputs

Hinkel answered 11/12, 2020 at 6:2 Comment(0)
S
1

Also be sure your terminal allows you to scroll back that far. In iTerm2, I had my "Scrollback lines" set to 1000. Changed it to "Unlimited scrollback" and now life is goodiTerm2 scrollback lines iTerm2:

Sheff answered 26/10, 2021 at 21:6 Comment(1)
So glad I don't have a macLimes

© 2022 - 2024 — McMap. All rights reserved.