Skip to content

Commit 8bcc122

Browse files
mscdexjasnell
authored andcommitted
test: improve querystring.parse assertion messages
PR-URL: #11234 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Nicu Micleușanu <micnic90@gmail.com>
1 parent ff785fd commit 8bcc122

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

test/parallel/test-querystring.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
require('../common');
33
const assert = require('assert');
4+
const inspect = require('util').inspect;
45

56
// test using assert
67
const qs = require('querystring');
@@ -126,28 +127,43 @@ assert.strictEqual('918854443121279438895193',
126127
qs.parse('id=918854443121279438895193').id);
127128

128129

129-
function check(actual, expected) {
130+
function check(actual, expected, input) {
130131
assert(!(actual instanceof Object));
131-
assert.deepStrictEqual(Object.keys(actual).sort(),
132-
Object.keys(expected).sort());
133-
Object.keys(expected).forEach(function(key) {
134-
assert.deepStrictEqual(actual[key], expected[key]);
132+
const actualKeys = Object.keys(actual).sort();
133+
const expectedKeys = Object.keys(expected).sort();
134+
let msg;
135+
if (typeof input === 'string') {
136+
msg = `Input: ${inspect(input)}\n` +
137+
`Actual keys: ${inspect(actualKeys)}\n` +
138+
`Expected keys: ${inspect(expectedKeys)}`;
139+
}
140+
assert.deepStrictEqual(actualKeys, expectedKeys, msg);
141+
expectedKeys.forEach(function(key) {
142+
if (typeof input === 'string') {
143+
msg = `Input: ${inspect(input)}\n` +
144+
`Key: ${inspect(key)}\n` +
145+
`Actual value: ${inspect(actual[key])}\n` +
146+
`Expected value: ${inspect(expected[key])}`;
147+
} else {
148+
msg = undefined;
149+
}
150+
assert.deepStrictEqual(actual[key], expected[key], msg);
135151
});
136152
}
137153

138154
// test that the canonical qs is parsed properly.
139155
qsTestCases.forEach(function(testCase) {
140-
check(qs.parse(testCase[0]), testCase[2]);
156+
check(qs.parse(testCase[0]), testCase[2], testCase[0]);
141157
});
142158

143159
// test that the colon test cases can do the same
144160
qsColonTestCases.forEach(function(testCase) {
145-
check(qs.parse(testCase[0], ';', ':'), testCase[2]);
161+
check(qs.parse(testCase[0], ';', ':'), testCase[2], testCase[0]);
146162
});
147163

148164
// test the weird objects, that they get parsed properly
149165
qsWeirdObjects.forEach(function(testCase) {
150-
check(qs.parse(testCase[1]), testCase[2]);
166+
check(qs.parse(testCase[1]), testCase[2], testCase[1]);
151167
});
152168

153169
qsNoMungeTestCases.forEach(function(testCase) {

0 commit comments

Comments
 (0)