diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index b663e37076..9670986c45 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -197,7 +197,7 @@ declare namespace jest { * spy.mockRestore(); * }); */ - function spyOn(object: T, method: M, accessType?: 'get' | 'set'): SpyInstance; + function spyOn(object: T, method: M, accessType?: 'get' | 'set'): MockInstance; /** * 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). @@ -726,16 +726,6 @@ declare namespace jest { (...args: any[]): any; } - interface SpyInstance extends MockInstance { - /** - * Removes the mock and restores the initial implementation. - * - * This is useful when you want to mock functions in certain test cases and restore the - * original implementation in others. - */ - mockRestore(): void; - } - /** * Wrap module with mock definitions * @@ -776,6 +766,18 @@ declare namespace jest { * don't access stale data. */ mockReset(): void; + /** + * Does everything that `mockFn.mockReset()` does, and also restores the original (non-mocked) implementation. + * + * This is useful when you want to mock functions in certain test cases and restore the original implementation in others. + * + * Beware that `mockFn.mockRestore` only works when mock was created with `jest.spyOn`. Thus you have to take care of restoration + * yourself when manually assigning `jest.fn()`. + * + * The [`restoreMocks`](https://jestjs.io/docs/en/configuration.html#restoremocks-boolean) configuration option is available + * to restore mocks automatically between tests. + */ + mockRestore(): void; /** * Accepts a function that should be used as the implementation of the mock. The mock itself will still record * all calls that go into and instances that come from itself – the only difference is that the implementation diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index 2de3cadfd4..f8b119921e 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -279,6 +279,8 @@ jest.fn().mockClear(); jest.fn().mockReset(); +jest.fn().mockRestore(); + const spiedTarget = { returnsVoid(): void { }, returnsString(): string {