Skip to content

Commit 98034fb

Browse files
grnchnovemberborn
authored andcommitted
Make the object printing depth configurable (#2121)
Respect Node.js' `util.inspect.defaultOptions.depth` setting when printing objects.
1 parent f26634b commit 98034fb

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

docs/06-configuration.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,21 @@ export default ({projectDir}) => {
110110
```
111111

112112
Note that the final configuration must not be a promise.
113+
114+
## Object printing depth
115+
116+
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:
117+
118+
```js
119+
import util from 'util';
120+
121+
import test from 'ava';
122+
123+
util.inspect.defaultOptions.depth = 5; // Increase AVA's printing depth
124+
125+
test('My test', t => {
126+
t.deepEqual(someDeeplyNestedObject, theExpectedValue);
127+
});
128+
```
129+
130+
AVA has a minimum depth of `3`.

lib/concordance-options.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
const util = require('util');
23
const ansiStyles = require('ansi-styles');
34
const stripAnsi = require('strip-ansi');
45
const cloneDeepWith = require('lodash.clonedeepwith');
@@ -124,6 +125,15 @@ const plainTheme = cloneDeepWith(colorTheme, value => {
124125
});
125126

126127
const theme = chalk.enabled ? colorTheme : plainTheme;
127-
exports.default = {maxDepth: 3, plugins, theme};
128+
129+
exports.default = {
130+
// Use Node's object inspection depth, clamped to a minimum of 3
131+
get maxDepth() {
132+
return Math.max(3, util.inspect.defaultOptions.depth);
133+
},
134+
plugins,
135+
theme
136+
};
137+
128138
exports.diff = {maxDepth: 1, plugins, theme};
129139
exports.snapshotManager = {plugins, theme: plainTheme};

0 commit comments

Comments
 (0)