I am earning unit testing. I know that best practice is that the functions should have local scope and they return something for example
export function test(arr) {
let arr2 = [];
for(let i = 0;i < arr.length;i++) {
if(arr[i] % 2 === 0) {
arr2.push(arr[i])
}
}
return arr2;
}
i can write the test for this function very simple - where i will expect some result
import { it, expect } from 'vitest';
it('should return even numbers', () => {
let result = test([1,2,3,4]);
expect(result).toEqual([2,4])
})
but I wonder, in functions where the global scope is modified from some function and the function is not returning anything for example
let arr2 = [];
export function test(arr) {
for(let i = 0;i < arr.length;i++) {
if(arr[i] % 2 === 0) {
arr2.push(arr[i])
}
}
}
how can i write test for such a functions ? How can i assume that arr2 will containt [2,4] at the end ?