asvetliakov / jest-create-mock-instance

Create mock instances with Jest
MIT License
34 stars 4 forks source link

jest.clearAllMocks() ? #4

Closed mikecann closed 6 years ago

mikecann commented 6 years ago

Hey,

I might be doing something wrong but it seems like if I call jest.clearAllMocks(); from my code then it doesnt clear the mocks created by this lib?

asvetliakov commented 6 years ago

@mikecann Does the following workaround work for you?


let service;

beforeEach(() => {
  service = createMockInstance(MyService);
});
mikecann commented 6 years ago

I decided to rock my own implementation with this gem:

type Constructor<T> = new(...args: any[]) => T;
function createMock<T>(clazz: Constructor<T>): jest.Mocked<T> {
    const instance = new clazz();
    const o: any = {};
    for (var key in Object.getOwnPropertyDescriptors(instance)) o[key] = jest.fn();
    for (var key in Object.getOwnPropertyDescriptors(clazz.prototype)) o[key] = jest.fn();
    return o;
}

Cheers