mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-12-04 04:01:52 +00:00
Fix Mocked type
This commit is contained in:
Vendored
+1
-6
@@ -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
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user