Skip to content

Commit 5f3ba84

Browse files
committed
test: add tests for hashObject function
1 parent f2ba85b commit 5f3ba84

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`test hash object > hash object 1`] = `"c710e218177ae3b76ac1b058d43fbd56c3dc71b48709dc15326862a75b0beec2"`;

tests/utils.spec.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
findClosestConfigFile,
33
findClosestPackageRoot,
4+
hashObject,
45
normalizeAlias,
56
normalizePatterns,
67
sortPaths,
@@ -380,3 +381,57 @@ describe("test normalize alias", () => {
380381
expect(normalizedAlias).toBeUndefined();
381382
});
382383
});
384+
385+
describe("test hash object", () => {
386+
it("hash object", () => {
387+
const obj = {
388+
a: 1,
389+
b: "string",
390+
c: [1, 2, 3],
391+
d: { e: "f" },
392+
};
393+
394+
const hash = hashObject(obj);
395+
expect(hash).toMatchSnapshot();
396+
});
397+
398+
it("hash object with different order", () => {
399+
const obj1 = {
400+
a: 1,
401+
b: "string",
402+
c: [1, 2, 3],
403+
d: { e: "f" },
404+
};
405+
406+
const obj2 = {
407+
b: "string",
408+
a: 1,
409+
d: { e: "f" },
410+
c: [1, 2, 3],
411+
};
412+
413+
const hash1 = hashObject(obj1);
414+
const hash2 = hashObject(obj2);
415+
expect(hash1).toBe(hash2);
416+
});
417+
418+
it("hash object with different values", () => {
419+
const obj1 = {
420+
a: 1,
421+
b: "string",
422+
c: [1, 2, 3],
423+
d: { e: "f" },
424+
};
425+
426+
const obj2 = {
427+
a: 2,
428+
b: "string",
429+
c: [1, 2, 3],
430+
d: { e: "f" },
431+
};
432+
433+
const hash1 = hashObject(obj1);
434+
const hash2 = hashObject(obj2);
435+
expect(hash1).not.toBe(hash2);
436+
});
437+
});

0 commit comments

Comments
 (0)