Skip to content

Commit

Permalink
Fixes #3739, use JSON.stringify to stringify objects (#3747)
Browse files Browse the repository at this point in the history
  • Loading branch information
swrdfish committed May 24, 2023
1 parent ac1a1ff commit 90993d9
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 21 deletions.
5 changes: 2 additions & 3 deletions lib/assertion/assertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ const {Logger, stringifyObject} = Utils;

class NightwatchAssertion {
static getExpectedMessage({expected, actual}) {
[expected, actual] = stringifyObject([expected, actual]);

const expectedMsg = Logger.colors.green(`"${expected}"`);
const receivedMsg = Logger.colors.red(`"${actual}"`);
const expectedMsg = Logger.colors.green(stringifyObject(expected));
const receivedMsg = Logger.colors.red(stringifyObject(actual));

return ` - expected ${expectedMsg} but got: ${receivedMsg}`;
}
Expand Down
16 changes: 2 additions & 14 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,21 +645,9 @@ class Utils {
}

static stringifyObject(objects) {
if (Array.isArray(objects)) {
return objects.map(obj => {
if (!Array.isArray(obj) && Utils.isObject(obj)) {
return inspect(obj);
}

return obj;
});
}

if (Utils.isObject(objects)) {
return inspect(objects);
}
const objectString = Utils.isObject(objects) ? inspect(objects) : objects;

return objects;
return `"${objectString}"`;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions test/apidemos/expect-global/deepEqual.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
describe('expect() tests ', function () {
it('deepEual', function() {
const expectedMsg = {a: 1, b: 4};
const receivedMsg = {b: 5};
const expectedMsg = [{a: 1, b: 4}];
const receivedMsg = [{b: 5}];

expect(expectedMsg).eql(receivedMsg);
})
});
});
2 changes: 1 addition & 1 deletion test/src/apidemos/expect-global/testExpect.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('expect(element.<command>) - passed', function() {
assert.strictEqual(results.lastError.name, 'NightwatchAssertError');
/* eslint-disable no-control-regex */
const cleanErrorMessage = results.lastError.message.replace(/\x1b\[[0-9;]*m|\(.*?\)/g, '');
assert.strictEqual(cleanErrorMessage, 'expected { a: 1, b: 4 } to deeply equal { b: 5 } - expected "{ b: 5 }" but got: "{ a: 1, b: 4 }" ');
assert.strictEqual(cleanErrorMessage, 'expected [ { a: 1, b: 4 } ] to deeply equal [ { b: 5 } ] - expected "[ { b: 5 } ]" but got: "[ { a: 1, b: 4 } ]" ');
}
};

Expand Down

0 comments on commit 90993d9

Please sign in to comment.