The goal of this function is to find the asymmetric difference between two or more arrays. However, I found a silent bug when I was adding more unit tests.
export const diff = (arr1, arr2) => {
return [
...arr1.filter((item) => !arr2.includes(item)),
...arr2.filter((item) => !arr1.includes(item)),
];
};
it('should return an array with the uncommon elements between arrays ', () => {
expect(diff([1, 2], [1, 2, 5])).toStrictEqual([5]); // passed
expect(diff([1, 2], [1, 2, 3], [1, 2, 3, 4])).toStrictEqual([4]); // failed
expect(diff(['a', 'b'], ['b', 'a'], ['a', 'b', 'c'])).toStrictEqual(['c']); // failed
expect(diff([null], [undefined, null], [undefined, null, 'Ala'])).toStrictEqual(['Ala']); //failed
expect(diff(['diorite', 'andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'],['diorite', 'andesite', 'grass', 'dirt', 'dead shrub'])
).toStrictEqual(['pink wool']); // failed
});
it('should return an empty array ', () => {
expect(diff([1, 2], [1, 2], [1, 2])).toStrictEqual([]); //failed
});
it('should return the original array ', () => {
expect(diff([1, 2])).toStrictEqual([1, 2]); //failed
});