Skip to content
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

Make the object printing depth configurable #2121

Merged
merged 4 commits into from
May 25, 2019

Conversation

grnch
Copy link
Contributor

@grnch grnch commented May 11, 2019

When printing objects or exceptions, Ava restricts the depth of the printed objects to 3 levels. Some libraries that like to throw exceptions with deeply nested data in them (e.g. Axios) would benefit from increasing the printing depth when debugging failing tests, but unfortunately this depth is hardcoded in Ava and it's not possible to change it by the user.

I would propose instead to tie this depth to util.inspect.defaultOptions.depth, which is a setting that NodeJS uses to control the depth of printing for console.log() and similar. Most NodeJS developers who have had to debug deeply nested objects by printing them with console.log() are already familiar with this setting, so it would come natural to use the same setting to control Ava's printing depth.

There are other ways this could be implemented, of course, for example by exposing this option as a command line argument or through some Ava-specific environment variable, but I think tying it to NodeJS's printing depth is the most practical solution. This way a NodeJS developer could kill two birds with one stone, by setting both the NodeJS and Ava printing depth in one go, rather than hunting for Ava-specific docs separately.

@grnch grnch force-pushed the make-printing-depth-configurable branch 2 times, most recently from e462517 to c1bc839 Compare May 11, 2019 03:47
Respect NodeJS's util.inspect.defaultOptions.depth setting when printing objects.
@grnch grnch force-pushed the make-printing-depth-configurable branch from c1bc839 to 4239b10 Compare May 11, 2019 04:12
Copy link
Member

@novemberborn novemberborn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@grnch I like your proposal. Have you tried this PR yourself?

I think we may have to change maxDepth from a property to a getter, so that the configuration can be changed on the fly. We definitely need to ensure you can use AVA's require configuration to load a file that modifies this setting.

lib/concordance-options.js Outdated Show resolved Hide resolved
@grnch
Copy link
Contributor Author

grnch commented May 13, 2019

Good point, @novemberborn!

I had been changing the depth before Ava is loaded so that's why it worked for me, but using a getter like you suggested is clearly superior because then it works even after Ava is loaded.

I have changed the code to use a getter and also clamped the depth to a minimum of 3, as you suggested.

Thanks!

P.S.
You may be wondering how I was changing the depth before Ava is loaded, when the first thing the Ava CLI does is require('ava').

On my dev machine I have this env var permanently set:

export NODE_OPTIONS="-r $HOME/.noderc"

This allows me to put whatever Node initialization code I want in ~/.noderc and it always gets loaded by the Node process before anything else, regardless of how the node executable is started.

@grnch
Copy link
Contributor Author

grnch commented May 14, 2019

Is there anything I should be doing about the Windows build failure in Travis? It seems like a random failure not related to the changes in this PR.

I could change the time stamp on the commit and force push it again, to trigger another Travis build.

@novemberborn
Copy link
Member

You may be wondering how I was changing the depth before Ava is loaded, when the first thing the Ava CLI does is require('ava').

On my dev machine I have this env var permanently set:

export NODE_OPTIONS="-r $HOME/.noderc"

This allows me to put whatever Node initialization code I want in ~/.noderc and it always gets loaded by the Node process before anything else, regardless of how the node executable is started.

Oh that's a neat trick!

Is there anything I should be doing about the Windows build failure in Travis? It seems like a random failure not related to the changes in this PR.

Yea they happen. No worries.

@novemberborn
Copy link
Member

@grnch this looks good. But we should document it, too. Perhaps as another section in https://github.com/avajs/ava/blob/master/docs/06-configuration.md? What do you reckon?

@grnch
Copy link
Contributor Author

grnch commented May 19, 2019

Sure thing @novemberborn.

I'll write something up when I'm near a real computer again.

@grnch
Copy link
Contributor Author

grnch commented May 20, 2019

Added some docs, please let me know if the wording or location needs to be changed.

@novemberborn novemberborn merged commit 98034fb into avajs:master May 25, 2019
@novemberborn
Copy link
Member

Thanks @grnch!

@grnch grnch deleted the make-printing-depth-configurable branch May 30, 2019 03:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants