I understand what's going on in: Using Jasmine to spy on a function without an object and why it doesn't work in TypeScript. I also see this TypeScript answer: https://stackoverflow.com/a/43532075/2548010.
Unfortunately this only solves the case for imported spied on functions. I am testing a function that takes in a function, and I am trying to see that the passed in function is called with the correct parameters.
import { chowDown } from './file.ts';
interface Taco {
filling: string;
hasCheese: boolean;
isCrispy: boolean;
}
describe('chowdown', () => {
const eatTaco = (taco: Taco) => {};
const expectedTaco = {filling: 'ground beef', hasCheese: false, isCrispy: true};
it('eats the taco', () => {
chowdown(eatTaco);
expect(eatTaco).toHaveBeenCalledWith(expectedTaco);
});
});
What I would like to do is something like
spyOn(eatTaco);
//or
spyOn(window, 'eatTaco');
None of the proposed solutions that I have found online actually work for this infrequent spy event. How can I actually spy on eatTaco
properly?
Replace the eatTaco
function with the following spy:
const eatTaco = jasmine.createSpy<(taco: Taco) => void>();
This TypeScript generics keeps you type safe.