Skip to content

Commit

Permalink
Core: Make QUnit.dump.maxDepth live alias to QUnit.config.maxDepth
Browse files Browse the repository at this point in the history
  • Loading branch information
Krinkle committed Jul 14, 2024
1 parent 4a2100d commit 5509595
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
1 change: 1 addition & 0 deletions docs/api/config/maxDepth.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ This is used by [`QUnit.dump.parse()`](../extension/QUnit.dump.parse.md).

## Changelog

| UNRELEASED | Make `QUnit.dump.maxDepth` an alias for `QUnit.config.maxDepth`, allowing either to be read or changed at runtime.
| [QUnit 1.18](https://github.com/qunitjs/qunit/releases/tag/1.18.0) | Introduce `QUnit.config.maxDepth` to enable setting via [preconfig](./index.md). Temporary changes at runtime must change `QUnit.dump.maxDepth` instead.
| [QUnit 1.16](https://github.com/qunitjs/qunit/releases/tag/1.16.0) | Introduce `QUnit.dump.maxDepth`.
6 changes: 3 additions & 3 deletions docs/api/extension/QUnit.dump.parse.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Extensible data dumping and string serialization.

This method does string serialization by parsing data structures and objects. It parses DOM elements to a string representation of their outer HTML. By default, nested structures will be displayed up to five levels deep. Anything beyond that is replaced by `[object Object]` and `[object Array]` placeholders.

If you need more or less output, change the value of `QUnit.dump.maxDepth`, representing how deep the elements should be parsed.
If you need more or less output, change the value of `QUnit.config.maxDepth`, representing how deep the elements should be parsed.

## Changelog

Expand Down Expand Up @@ -74,11 +74,11 @@ var input = {
back: []
}
};
QUnit.dump.maxDepth = 1;
QUnit.config.maxDepth = 1;
console.log(QUnit.dump.parse(input));
// Logs: { "parts": [object Object] }

QUnit.dump.maxDepth = 2;
QUnit.config.maxDepth = 2;
console.log(QUnit.dump.parse(input));
// Logs: { "parts": { "back": [object Array], "front": [object Array] } }
```
15 changes: 12 additions & 3 deletions src/dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default (function () {
return [pre, inner + arr, base + post].join(separator);
}
function array (arr, stack) {
if (dump.maxDepth && dump.depth > dump.maxDepth) {
if (config.maxDepth && dump.depth > config.maxDepth) {
return '[object Array]';
}

Expand Down Expand Up @@ -156,7 +156,16 @@ export default (function () {
literal: literal,
join: join,
depth: 1,
maxDepth: config.maxDepth,

/**
* @deprecated since 3.0.0, use QUnit.config.maxDepth instead.
*/
get maxDepth () {
return config.maxDepth;
},
set maxDepth (value) {
config.maxDepth = value;
},

// This is the list of parsers, to modify them, use dump.setParser
parsers: {
Expand Down Expand Up @@ -191,7 +200,7 @@ export default (function () {
object: function (map, stack) {
const ret = [];

if (dump.maxDepth && dump.depth > dump.maxDepth) {
if (config.maxDepth && dump.depth > config.maxDepth) {
return '[object Object]';
}

Expand Down
42 changes: 31 additions & 11 deletions test/main/dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
QUnit.module('dump', {
beforeEach: function () {
// Run most tests without a limit
QUnit.dump.maxDepth = 0;
QUnit.config.maxDepth = 0;
},
afterEach: function () {
// Restore default
QUnit.dump.maxDepth = 5;
QUnit.config.maxDepth = 5;
}
});

Expand All @@ -25,28 +25,52 @@ QUnit.test('object [shallow]', function (assert) {
},
left: 0
};
QUnit.dump.maxDepth = 1;
QUnit.config.maxDepth = 1;
assert.equal(QUnit.dump.parse(obj), '{\n "left": 0,\n "top": [object Object]\n}');

QUnit.dump.maxDepth = 2;
QUnit.config.maxDepth = 2;
assert.equal(
QUnit.dump.parse(obj),
'{\n "left": 0,\n "top": {\n "middle": [object Object]\n }\n}'
);

QUnit.dump.maxDepth = 3;
QUnit.config.maxDepth = 3;
assert.equal(
QUnit.dump.parse(obj),
'{\n "left": 0,\n "top": {\n "middle": {\n "bottom": 0\n }\n }\n}'
);

QUnit.dump.maxDepth = 5;
QUnit.config.maxDepth = 5;
assert.equal(
QUnit.dump.parse(obj),
'{\n "left": 0,\n "top": {\n "middle": {\n "bottom": 0\n }\n }\n}'
);
});

QUnit.test('QUnit.dump.maxDepth alias', function (assert) {
assert.strictEqual(QUnit.dump.maxDepth, 0, 'alias matches initial config');

QUnit.config.maxDepth = 1;
assert.strictEqual(QUnit.dump.maxDepth, 1, 'change alias via config');

QUnit.dump.maxDepth = 2;
assert.strictEqual(QUnit.config.maxDepth, 2, 'change config via alias');

var obj = {
top: {
middle: {
bottom: 0
}
},
left: 0
};
assert.equal(
QUnit.dump.parse(obj),
'{\n "left": 0,\n "top": {\n "middle": [object Object]\n }\n}',
'object shallow with effective maxDepth=2'
);
});

QUnit.test('error [Error]', function (assert) {
assert.equal(
QUnit.dump.parse(new Error('foo')),
Expand Down Expand Up @@ -206,8 +230,6 @@ function chainwrap (depth, first, prev) {
}

QUnit.test('object [recursion]', function (assert) {
// QUnit.dump.maxDepth = 20;

var noref = chainwrap(0);
var nodump = QUnit.dump.parse(noref);
assert.equal(nodump, '{\n "first": true,\n "wrap": undefined\n}');
Expand All @@ -230,8 +252,6 @@ QUnit.test('object [recursion]', function (assert) {
});

QUnit.test('object equal/deepEqual [recursion]', function (assert) {
// QUnit.dump.maxDepth = 20;

var noRecursion = chainwrap(0);
assert.equal(noRecursion, noRecursion, 'I should be equal to me.');
assert.deepEqual(noRecursion, noRecursion, '... and so in depth.');
Expand All @@ -255,7 +275,7 @@ QUnit.test('array [basic]', function (assert) {
});

QUnit.test('array [shallow]', function (assert) {
QUnit.dump.maxDepth = 1;
QUnit.config.maxDepth = 1;
assert.equal(
QUnit.dump.parse([[]]),
'[\n [object Array]\n]');
Expand Down

0 comments on commit 5509595

Please sign in to comment.