Bug 1642295 - Do not check property order in WebConsole stub tests r=nchevobbe

Depends on D86962

Differential Revision: https://phabricator.services.mozilla.com/D86963
This commit is contained in:
Julian Descottes 2020-08-19 14:08:39 +00:00
Родитель e9bb161639
Коммит 09373e81c9
5 изменённых файлов: 44 добавлений и 9 удалений

Просмотреть файл

@ -40,9 +40,10 @@ add_task(async function() {
let failed = false;
for (const [key, packet] of generatedStubs) {
const packetStr = getSerializedPacket(packet);
const packetStr = getSerializedPacket(packet, { sortKeys: true });
const existingPacketStr = getSerializedPacket(
existingStubs.rawPackets.get(key)
existingStubs.rawPackets.get(key),
{ sortKeys: true }
);
is(packetStr, existingPacketStr, `"${key}" packet has expected value`);

Просмотреть файл

@ -39,9 +39,10 @@ add_task(async function() {
let failed = false;
for (const [key, packet] of generatedStubs) {
const packetStr = getSerializedPacket(packet);
const packetStr = getSerializedPacket(packet, { sortKeys: true });
const existingPacketStr = getSerializedPacket(
existingStubs.rawPackets.get(key)
existingStubs.rawPackets.get(key),
{ sortKeys: true }
);
is(packetStr, existingPacketStr, `"${key}" packet has expected value`);
failed = failed || packetStr !== existingPacketStr;

Просмотреть файл

@ -43,9 +43,10 @@ add_task(async function() {
let failed = false;
for (const [key, packet] of generatedStubs) {
const packetStr = getSerializedPacket(packet);
const packetStr = getSerializedPacket(packet, { sortKeys: true });
const existingPacketStr = getSerializedPacket(
existingStubs.rawPackets.get(key)
existingStubs.rawPackets.get(key),
{ sortKeys: true }
);
is(packetStr, existingPacketStr, `"${key}" packet has expected value`);
failed = failed || packetStr !== existingPacketStr;

Просмотреть файл

@ -40,9 +40,10 @@ add_task(async function() {
let failed = false;
for (const [key, packet] of generatedStubs) {
const packetStr = getSerializedPacket(packet);
const packetStr = getSerializedPacket(packet, { sortKeys: true });
const existingPacketStr = getSerializedPacket(
existingStubs.rawPackets.get(key)
existingStubs.rawPackets.get(key),
{ sortKeys: true }
);
is(packetStr, existingPacketStr, `"${key}" packet has expected value`);
failed = failed || packetStr !== existingPacketStr;

Просмотреть файл

@ -466,7 +466,38 @@ function getStubFile(fileName) {
return require(CHROME_PREFIX + STUBS_FOLDER + fileName);
}
function getSerializedPacket(packet) {
function sortObjectKeys(obj) {
const isArray = Array.isArray(obj);
const isObject = Object.prototype.toString.call(obj) === "[object Object]";
const isFront = obj?._grip;
if (isObject && !isFront) {
// Reorder keys for objects, but skip fronts to avoid infinite recursion.
const sortedKeys = Object.keys(obj).sort((k1, k2) => k1.localeCompare(k2));
const withSortedKeys = {};
sortedKeys.forEach(k => {
withSortedKeys[k] = k !== "stacktrace" ? sortObjectKeys(obj[k]) : obj[k];
});
return withSortedKeys;
} else if (isArray) {
return obj.map(item => sortObjectKeys(item));
}
return obj;
}
/**
* @param {Object} packet
* The packet to serialize.
* @param {Object}
* - {Boolean} sortKeys: pass true to sort all keys alphabetically in the
* packet before serialization. For instance stub comparison should not
* fail if the order of properties changed.
*/
function getSerializedPacket(packet, { sortKeys = false } = {}) {
if (sortKeys) {
packet = sortObjectKeys(packet);
}
return JSON.stringify(
packet,
function(_, value) {