I am writing a library that implements various sorting algorithms, and everything was fine until I ran an array of objects.
const exec = (array, fnCompare) =>{
return sort.bubble(array, fnCompare);
}
describe('Array Obejetos', ()=> {
let array_in = [
{name: "Alex", age: 12},
{name: "Max", age: 34},
{name: "Mary", age: 9},
{name: "Justin", age: 53}
];
let array_out_asc = [
{name: "Mary", age: 9},
{name: "Alex", age: 12},
{name: "Max", age: 34},
{name: "Justin", age: 53}
]
const fnASC = (a,b) => {
return a.age < b.age; //linha onde o console reclama que o erro ocorreu
};
it('Ordenação crescente', ()=> {
assert.deepEqual(array_out_asc, exec(array_in, fnASC));
});
});
But I end up having the following output in my unit test:
24 passing (29ms)
1 failing
1) Bubble Array Objetos Ordenação crescente:
TypeError: Cannot read property 'age' of undefined
at fnASC (test/bubblesort.spec.js:85:23)
at core/bubble.js:25:10
at Array.forEach (native)
at Object.array.forEach [as bubble] (core/bubble.js:24:11)
at exec (test/bubblesort.spec.js:10:17)
at Context.<anonymous> (test/bubblesort.spec.js:89:39)
The error says that it was not possible to read the property acts, however if I add a console log within the fnASC function described above it prints the object perfectly. Anyone have any idea what might be happening?