Typescript + jasmine.createSpy() Typescript + jasmine.createSpy() typescript typescript

Typescript + jasmine.createSpy()


Use the type already defined and used by Jasmine SpyObj<T>.

const serviceMock: jasmine.SpyObj<IMyService> = jasmine.createSpyObj<IMyService>('service',['method']);

This way every method of IMyService will be augmented with the Spy ones:

serviceMock.method.and.callFake(()=>'hello');


Try it with a mapped type

export type Spied<T> = {    [Method in keyof T]: jasmine.Spy;};

and cast your service mock with it

const serviceMock = Spied<IMyService>{

Take a look here for a detailed description


Try using the Partial type:

const serviceMock = <Partial<IMyService>>{ 

For more information check out: https://www.typescriptlang.org/docs/handbook/advanced-types.html