Merge pull request #33214 from antoinebrault/jest-spyOn-optional

[jest] spyOn optional interface fields
This commit is contained in:
Jesse Trinity
2019-02-25 21:16:47 -08:00
committed by GitHub
2 changed files with 21 additions and 10 deletions
+4 -3
View File
@@ -220,9 +220,10 @@ declare namespace jest {
* spy.mockRestore();
* });
*/
function spyOn<T extends {}, M extends NonFunctionPropertyNames<T>>(object: T, method: M, accessType: 'get'): SpyInstance<T[M], []>;
function spyOn<T extends {}, M extends NonFunctionPropertyNames<T>>(object: T, method: M, accessType: 'set'): SpyInstance<void, [T[M]]>;
function spyOn<T extends {}, M extends FunctionPropertyNames<T>>(object: T, method: M): T[M] extends (...args: any[]) => any ? SpyInstance<ReturnType<T[M]>, ArgsType<T[M]>> : never;
function spyOn<T extends {}, M extends NonFunctionPropertyNames<Required<T>>>(object: T, method: M, accessType: 'get'): SpyInstance<Required<T>[M], []>;
function spyOn<T extends {}, M extends NonFunctionPropertyNames<Required<T>>>(object: T, method: M, accessType: 'set'): SpyInstance<void, [Required<T>[M]]>;
function spyOn<T extends {}, M extends FunctionPropertyNames<Required<T>>>(object: T, method: M): Required<T>[M] extends (...args: any[]) => any ?
SpyInstance<ReturnType<Required<T>[M]>, ArgsType<Required<T>[M]>> : never;
/**
* Indicates that the module system should never return a mocked version of
* the specified module from require() (e.g. that it should always return the real module).
+17 -7
View File
@@ -349,11 +349,15 @@ const mockContextVoid = jest.fn().mock;
const mockContextString = jest.fn(() => "").mock;
jest.fn().mockClear();
jest.fn().mockReset();
jest.fn().mockRestore();
jest.fn().mockImplementation((test: number) => test);
jest.fn().mockResolvedValue(1);
interface SpyInterface {
prop?: number;
method?: (arg1: boolean) => void;
}
const spiedTarget = {
returnsVoid(): void { },
setValue(value: string): void {
@@ -363,7 +367,6 @@ const spiedTarget = {
return "";
}
};
class SpiedTargetClass {
private _value = 3;
private _value2 = '';
@@ -380,6 +383,7 @@ class SpiedTargetClass {
this._value2 = value2;
}
}
const spiedTarget2 = new SpiedTargetClass();
// $ExpectError
@@ -425,11 +429,17 @@ const spy5 = jest.spyOn(spiedTarget2, "value", "get");
spy5.mockReturnValue('5');
// $ExpectType SpyInstance<void, [number]>
const spy6 = jest.spyOn(spiedTarget2, "value", "set");
jest.spyOn(spiedTarget2, "value", "set");
// should compile
jest.fn().mockImplementation((test: number) => test);
jest.fn().mockResolvedValue(1);
let spyInterfaceImpl: SpyInterface = {};
// $ExpectError
jest.spyOn(spyInterfaceImpl, "method", "get");
// $ExpectError
jest.spyOn(spyInterfaceImpl, "prop");
// $ExpectType SpyInstance<number, []>
jest.spyOn(spyInterfaceImpl, "prop", "get");
// $ExpectType SpyInstance<void, [boolean]>
jest.spyOn(spyInterfaceImpl, "method");
interface Type1 { a: number; }
interface Type2 { b: number; }