Skip to content

Commit

Permalink
Make the object printing depth configurable (#2121)
Browse files Browse the repository at this point in the history
Respect Node.js' `util.inspect.defaultOptions.depth` setting when printing objects.
  • Loading branch information
grnch authored and novemberborn committed May 25, 2019
1 parent f26634b commit 98034fb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
18 changes: 18 additions & 0 deletions docs/06-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
12 changes: 11 additions & 1 deletion lib/concordance-options.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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};

0 comments on commit 98034fb

Please sign in to comment.