Fix Mocked type

This commit is contained in:
Alexey Svetliakov
2017-01-11 06:36:08 +01:00
parent 3e757ce662
commit 654c2b4e11
2 changed files with 6 additions and 19 deletions
+1 -6
View File
@@ -201,15 +201,10 @@ declare namespace jest {
*
* const myApi: jest.Mocked<Api> = new Api() as any;
* myApi.myApiMethod.mockImplementation(() => "test");
*
* jest.mock("./func");
* const myFuncMock: jest.Mocked<typeof myFunc> = require("./func");
* myFuncMock.mockImplementation(() => 5);
* myFuncMock();
*/
type Mocked<T> = T & {
[P in keyof T]: T[P] & MockInstance<T>;
} & MockInstance<T>;
};
interface MockInstance<T> {
mock: MockContext<T>;
+5 -13
View File
@@ -426,28 +426,20 @@ describe('strictNullChecks', function () {
})
});
interface TestApi {
class TestApi {
constructor() { };
testProp: boolean;
testMethod(a: number): void;
testMethod(a: number): string { return ""; }
}
declare function mockedFunc(a: number): string;
describe('Mocked type', function () {
it('Works', function () {
const mock: jest.Mocked<TestApi> = jest.fn(() => ({
testProp: true,
testMethod1: jest.fn(),
testMethod2: jest.fn()
})) as any;
const mock: jest.Mocked<TestApi> = new TestApi() as any;
mock.testProp;
mock.testMethod.mockImplementation(() => 'test');
mock.testMethod(5);
mock.mockClear();
const funcMock: jest.Mocked<typeof mockedFunc> = require('./mockedFunc');
funcMock.mockImplementation(() => 'test');
funcMock(5).toUpperCase();
mock.testMethod(5).toUpperCase();
});
});