Within my project, there are various utility functions that handle data transformations and other tasks. These functions are not contained within a class and are utilized across multiple utility files.
Being relatively new to angular testing, I've spent some time searching for a way to stub the imports of a tested file. To illustrate this issue, take a look at the following:
/utils/helpers.ts
export const sumNumber = (a: number, b: number): number => {
return a + b;
}
/utils/file-i-want-to-test.ts
import { sumNumber } from "./helpers";
export const sumArrays = (arr1: number[], arr2: number[]): number[] => {
const len = arr1.length > arr2.length ? arr1.length : arr2.length;
const result = new Array(len).fill(0);
return result.map((_, index) => sumNumber(arr1[index] || 0, arr2[index] || 0));
}
/utils/file-i-want-to-test.spec.ts
import { sumArrays } from './file-i-want-to-test';
describe('Utilities', () => {
describe('Function - sumArrays', () => {
it('should return empty array', () => {
const actual = sumArrays([], []);
expect(actual).toEqual([]);
});
it('should call sumValue 2 times', () => {
const actual = sumArrays([1, 2], [3, 4]);
// How to test that sumArray is actually calling his dependency sumNumber?
});
});
});
Challenge
For effective unit testing of the sumArrays function, it is necessary to stub its dependency sumNumber. How can this be achieved?