Creé una función que compara dos matrices y se asegura de que no sean iguales entre sí. Ejecuté la función dos veces, pero debo admitir que es un diseño muy pobre, sin embargo, era la única forma en que podía hacer que funcionara. ¿Hay una manera más fácil de lograr lo mismo? ¿Quizás clonación profunda con lodash? Aquí está mi intento.
let callCount = 0; let values = []; let values2 = []; const getData = () => { const chart = [{ //dynamic: changes the id numbers on second call items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }, { id: 3, name: 'bar' }, { id: 4, name: 'bar' }] }]; if (callCount < 1) { //if and for block cannot be extracted for various reasons for (let i = 0; i < 5; i++) { const arr = chart[i].items[0]; arr.forEach((element) => { values.push(element.id); }); } callCount++; return values; } if (callCount >= 1) { for (let i = 0; i < 5; i++) { const arr = chart[i].items[0]; arr.forEach((element) => { values2.push(element.id); }); } expect(values).to.not.eq(values2); } }; it ('Comparing two arrays', function () { getData() // logic that will change effectively change some of the values of `chart` getData() // is successful but wildly inefficient }¿Hay alguna manera más fácil de usar Javascript para hacer lo mismo sin contar la cantidad de veces que se llama a la función y se vuelve a ejecutar lo mismo?
Devuelve la matriz cada vez y compara al final de la secuencia,
const getData = () => { const chart = [{ //dynamic: changes the id numbers on second call items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }, { id: 3, name: 'bar' }, { id: 4, name: 'bar' }] }]; const arr = chart[i].items[0].map(element => element.id) return arr; }; it ('Comparing two arrays', function () { const values1 = getData() // logic that will change effectively change some of the values of `chart` const values2 = getData() expect(values1).to.not.eq(values2); } Me pregunto cómo se cambia el chart : ¿hay algún cy.get() involucrado?
Si es así tendrás que devolver el resultado de ellos y anidar los resultados
it ('Comparing two arrays', function () { getData().then(values1 => { // logic that will change effectively change some of the values of `chart` getData().then(const values2 => { expect(values1).to.not.eq(values2); }) }) }