My project has a lot of utility functions that perform some data transformations, etc. These functions are not in class and are reused in many utility files.
I'm very new to angular testing. Searched for quite some time and I couldn't find a way to stub the imports of a tested file. Here's the example:
/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?
});
});
});
Problem
In order to unit test function sumArrays properly, I need to stub it's dependency sumNumber. How it can be done?