Skip to content

Commit

Permalink
Speed up tests by avoiding t.deepEqual for changes arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrichina committed Feb 16, 2024
1 parent 9fb6639 commit 458f062
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion test/changes-index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const roundtrip = customTest((test, fileName) => {
const tempFileName = `${fileName}.tmp`;
await writeChangesFile(`${ROOT_DIR}/${tempFileName}`, convertChanges(changes));
const tempChanges = await readChanges(tempFileName);
t.deepEqual(tempChanges, changes);
t.ok(changesAreEqual(changes, tempChanges));
t.ok((await fs.readFile(`${ROOT_DIR}/${tempFileName}`)).equals(await fs.readFile(`${ROOT_DIR}/${fileName}`)));
});
});
Expand Down Expand Up @@ -100,6 +100,25 @@ function changesAsString({ accountId, key, changes }) {
return `${accountId} ${key.toString('hex')} ${10_000_000_000 - changes[0]}`;
}

function changesAreEqual(changes1, changes2) {
if (changes1.length !== changes2.length) {
return false;
}
for (let i = 0; i < changes1.length; i++) {
const c1 = changes1[i];
const c2 = changes2[i];
if (c1.accountId !== c2.accountId || !c1.key.equals(c2.key) || c1.changes.length !== c2.changes.length) {
return false;
}
for (let j = 0; j < c1.changes.length; j++) {
if (c1.changes[j] !== c2.changes[j]) {
return false;
}
}
}
return true;
}

function customTest(fn) {
const result = function(...args) {
fn(test, ...args);
Expand Down

0 comments on commit 458f062

Please sign in to comment.