From 5509595e141b6fd6fb35bb763d9af9aa4f9408fa Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Sat, 13 Jul 2024 21:41:35 +0100 Subject: [PATCH] Core: Make QUnit.dump.maxDepth live alias to QUnit.config.maxDepth --- docs/api/config/maxDepth.md | 1 + docs/api/extension/QUnit.dump.parse.md | 6 ++-- src/dump.js | 15 +++++++-- test/main/dump.js | 42 +++++++++++++++++++------- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/docs/api/config/maxDepth.md b/docs/api/config/maxDepth.md index 3121fd68a..de685de6d 100644 --- a/docs/api/config/maxDepth.md +++ b/docs/api/config/maxDepth.md @@ -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`. diff --git a/docs/api/extension/QUnit.dump.parse.md b/docs/api/extension/QUnit.dump.parse.md index e15fb8cae..8ecd4f48b 100644 --- a/docs/api/extension/QUnit.dump.parse.md +++ b/docs/api/extension/QUnit.dump.parse.md @@ -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 @@ -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] } } ``` diff --git a/src/dump.js b/src/dump.js index c5a5a1924..425470d3d 100644 --- a/src/dump.js +++ b/src/dump.js @@ -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]'; } @@ -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: { @@ -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]'; } diff --git a/test/main/dump.js b/test/main/dump.js index 44991bc2e..1958ec2cb 100644 --- a/test/main/dump.js +++ b/test/main/dump.js @@ -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; } }); @@ -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')), @@ -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}'); @@ -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.'); @@ -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]');