question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

26reactions
milesjcommented, Jul 31, 2018

This needs to be reported on DefinitelyTyped, not here. That being said, just change your code to this:

const spiedGetDuration = jest.spyOn(Player, 'getDuration');
spiedGetDuration.mockImplementation(() => 3600);

// test logics

spiedGetDuration.mockRestore();
1reaction
alexandermckaycommented, Sep 16, 2019

I am still getting the issue

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found