question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Debug util that would only print displayed text

See original GitHub issue

Describe the Feature

When debugging tests on large components, i.e. pages, snapshots can be very long and may not be convenient for debugging purposes. Very often I’m mostly interested in seing what text / testID is displayed on the screen and don’t really care about the design so I think that a function that would log only the displayed text and maybe also the testIDs could come in pretty handy.

Possible Implementations

We’d have a function to transform the json representation into a string with just text and maybe also testIDs displayed. I implemented such a function on my current project and it looks like this :

export const toText = (node: ReactTestRendererNode | null, textSeparator = '\n'): string | null => {
  if (!node) return '';
  if (typeof node === 'string') return node;
  if (Array.isArray(node)) return toText({ type: 'Fragment', props: {}, children: node });

  const texts = [
    getImageSource(node),
    ...(node.children?.map(child => toText(child, textSeparator)) || []),
  ].filter(truthy);

  return texts.length > 0
    ? texts.join(textSeparator) +
        // Arbitrarily add a separator if more than 1 child, just to add some space in the snap
        (texts.length > 1 ? textSeparator : '')
    : null;
};

Then the api would need to be modified to add this functionality. I’m thinking of two ways to do this :

  • Add a debugText function to RenderResult (not sure about the naming yet, this could be something else)
  • Add options to the current debug function to enable customization of the output

Second option would allow more customization in the future and we may want to combine several options (for instance text and testID are visible or just text or just testID) but it also makes the api more complex and more difficult to use. Also people may not notice that this feature exists. Alternatively both approach could be combined to have an accessible and easy way of printing just text and also the ability to custom output according to one’s needs.

Related Issues

No related issues as far as I know

@mdjastrzebski @AugustinLF what are your thoughts on this ?

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mdjastrzebskicommented, Sep 27, 2022

I’ve submitted #1141 for adding configure

1reaction
AugustinLFcommented, Sep 27, 2022

I love the suggested API by @mdjastrzebski. I feel filterProps should be renamed to something akin mapNode, or whatever, because here we don’t filter a prop, we just rewrite what is printed.

And I think it’d be nice to also expose that as global configuration through an API akin to RTL’s configure. And with of course a priority if passed as an option to debug().

Read more comments on GitHub >

github_iconTop Results From Across the Web

Looking for an easier way to write debugging print statements ...
Looking for an easier way to write debugging print statements in Java - Stack Overflow. Stack Overflow for Teams – Start collaborating and...
Read more >
VBA Debug Print - WallStreetMojo
VBA Debug.Print is the statement helpful in displaying more variables at a time in the immediate window. It is the best and alternative...
Read more >
12. Debugging Makefiles - Managing Projects with GNU Make ...
The first test I perform on a new makefile target is to invoke make with the --just-print ( -n ) option. This causes...
Read more >
4.4: Using println() - Processing Tutorial - YouTube
This video demonstrates a simple " debugging " technique using println() to display variable values in the message console.
Read more >
Write-Debug (Microsoft.PowerShell.Utility) - Microsoft Learn
The Write-Debug cmdlet writes debug messages to the host from a script or command. By default, debug messages are not displayed in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found