TypeScript throws a `mockRestore` error
See original GitHub issue🐛 Bug Report
When using TypeScript, spyOn
and mockImplementation
, there is an error of mockRestore
.
To Reproduce
I have a test case like this:
const spiedGetDuration = jest.spyOn(Player, 'getDuration').mockImplementation(() => 3600);
// test logics
spiedGetDuration.mockRestore();
but tsc
throws an error:
error TS2551: Property 'mockRestore' does not exist on type 'Mock<() => any[]>'. Did you mean 'mockReset'?
I checked the @types/jest
file, mockRestore
is only available for SpyInstance
, and jest.spyOn
returns a SpyInstance
, when there is no following calls like mockImplementation
, there is no issue:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/jest/index.d.ts#L728
After mockImplementation
, since it returns a MockInstance
, mockRestore
is unavailable.
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/jest/index.d.ts#L778
Expected behavior
There should be no error.
Run npx envinfo --preset jest
Paste the results here:
npx: installed 1 in 4.137s
System:
OS: macOS 10.14
CPU: x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
Binaries:
Node: 8.9.1 - ~/.nvm/versions/node/v8.9.1/bin/node
Yarn: 1.3.2 - /usr/local/bin/yarn
npm: 5.5.1 - ~/.nvm/versions/node/v8.9.1/bin/npm
npmPackages:
@types/jest: ^22.1.3 => 22.1.3
jest: ^22.4.2 => 22.4.2
Issue Analytics
- State:
- Created 5 years ago
- Comments:5
Top Results From Across the Web
Call Jest mockRestore in TypeScript - remarkablemark
However, TypeScript throws the error: 2339[QF available]: Property 'mockRestore' does not exist on type '() => number'.
Read more >Typescript and Jest: Avoiding type errors on mocked functions
The above will run fine in Jest but will throw a Typescript error: Property 'mockReturnValueOnce' does not exist on type '(url: string, config?:...
Read more >Mock Functions - Jest
'throw' - Indicates that the call completed by throwing a value. 'incomplete' - Indicates that the call has not yet completed. This occurs...
Read more >Interface MockInstance<T, Y> - typescript
This is useful when you want to mock functions in certain test cases and restore the original implementation in others. Beware that mockFn.mockRestore...
Read more >How to use TypeScript and Jest mocks - Breno Calazans
When using TypeScript that might be a bit harder because they are not ... TypeScript throws an error since it doesn't resolve mock...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This needs to be reported on DefinitelyTyped, not here. That being said, just change your code to this:
I am still getting the issue