I want to spy on a simple array for a simple TypeScript class. However when I set up the test and run it I get an error saying 'createDiscounts is not iterable' How can I mock the iterable?
Here is the code:
// discounts.ts
export function createDiscounts {
discounts: number[] = [0.5, 0.4, 0.7, 0. 9]
return discounts.entries()
}
export class ProcessDiscountedItemsService {
applyDiscounts = (items: OrderItem) => {
for(let [number, discount] of discounts){
//do logic
}
}
}
Here's my attempt to spy on the createDiscounts function
import * as DiscountUtil from '../discounts.ts'
describe('Process discount', () => {
const service = new ProcessDiscountedItemsService()
it('process item discounts verify' () =>{
const items = ....
const spy = (jest.spyOn(DiscountUtil, 'createDiscounts')) as jest.Mock
const iteratable = [0.5, 0.4, 0.7, 0. 9].entries()
service.applyDiscounts(items)
})
})
I replaced mockResolvedValueOnce with mockImplementation and that fixed the problem