Added one more example for jest.Mocked

This commit is contained in:
Alexey Svetliakov
2017-01-09 06:59:08 +01:00
parent b4fc6f27dc
commit f2581e2b98
2 changed files with 11 additions and 0 deletions
+5
View File
@@ -200,6 +200,11 @@ 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>;
+6
View File
@@ -431,6 +431,8 @@ interface TestApi {
testMethod(a: number): void;
}
declare function mockedFunc(a: number): string;
describe("Mocked type", function () {
it("Works", function () {
const mock: jest.Mocked<TestApi> = jest.fn(() => ({
@@ -442,5 +444,9 @@ describe("Mocked type", function () {
mock.testMethod.mockImplementation(() => "test");
mock.testMethod(5);
mock.mockClear();
const funcMock: jest.Mocked<typeof mockedFunc> = require("./mockedFunc");
funcMock.mockImplementation(() => "test");
funcMock(5).toUpperCase();
});
});