From 98034fbb661bcc6cb882e1ae007a7877a803b3a4 Mon Sep 17 00:00:00 2001 From: grnch Date: Sat, 25 May 2019 07:43:56 -0700 Subject: [PATCH] Make the object printing depth configurable (#2121) Respect Node.js' `util.inspect.defaultOptions.depth` setting when printing objects. --- docs/06-configuration.md | 18 ++++++++++++++++++ lib/concordance-options.js | 12 +++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/docs/06-configuration.md b/docs/06-configuration.md index f99bfbf07..92b295dc2 100644 --- a/docs/06-configuration.md +++ b/docs/06-configuration.md @@ -110,3 +110,21 @@ export default ({projectDir}) => { ``` Note that the final configuration must not be a promise. + +## Object printing depth + +By default, AVA prints nested objects to a depth of `3`. However, when debugging tests with deeply nested objects, it can be useful to print with more detail. This can be done by setting [`util.inspect.defaultOptions.depth`](https://nodejs.org/api/util.html#util_util_inspect_defaultoptions) to the desired depth, before the test is executed: + +```js +import util from 'util'; + +import test from 'ava'; + +util.inspect.defaultOptions.depth = 5; // Increase AVA's printing depth + +test('My test', t => { + t.deepEqual(someDeeplyNestedObject, theExpectedValue); +}); +``` + +AVA has a minimum depth of `3`. diff --git a/lib/concordance-options.js b/lib/concordance-options.js index b3390a4b3..0e6193211 100644 --- a/lib/concordance-options.js +++ b/lib/concordance-options.js @@ -1,4 +1,5 @@ 'use strict'; +const util = require('util'); const ansiStyles = require('ansi-styles'); const stripAnsi = require('strip-ansi'); const cloneDeepWith = require('lodash.clonedeepwith'); @@ -124,6 +125,15 @@ const plainTheme = cloneDeepWith(colorTheme, value => { }); const theme = chalk.enabled ? colorTheme : plainTheme; -exports.default = {maxDepth: 3, plugins, theme}; + +exports.default = { + // Use Node's object inspection depth, clamped to a minimum of 3 + get maxDepth() { + return Math.max(3, util.inspect.defaultOptions.depth); + }, + plugins, + theme +}; + exports.diff = {maxDepth: 1, plugins, theme}; exports.snapshotManager = {plugins, theme: plainTheme};