Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spyOn(...).mockImplementation should return a SpyInstance rather than a Mock #17605

Closed
4 tasks done
jamiecopeland opened this issue Jun 29, 2017 · 5 comments
Closed
4 tasks done

Comments

@jamiecopeland
Copy link

While attempting to write a system to un-mock/un-spy on modules I came across what seems to be a problem with types returned by mockImplementation (and mockImplementationOnce, mockReturnThis, mockReturnValue, mockReturnValueOnce) as a result of calling spyOn. This is that while the typings specify that mockImplementation returns a Mock, what actually comes back is a SpyInstance with the mockRestore function available to call. It'd be great to get some feedback on this, and if you think it's a valid fix, I can get a PR put together reflecting this change.

The type hack version below works, even though the current types specify that it shouldn't

// Spy creating/cleaning utility (as it should be)
const createSpies = (spies: jest.SpyInstance<any>[]) => () => spies.forEach(spy => spy.mockRestore());

// Spy creating/cleaning utility (with hack to prevent compiler from complaining)
const createSpies = (spies: any[]) => () => spies.forEach(spy => spy.mockRestore());

// Test
it('should...', () => {
  const restoreMocks = createSpies([
    jest.spyOn(moduleA, 'sayHi').mockImplementation(() => 'mock hello')
    jest.spyOn(moduleB, 'sayBye').mockImplementationOnce(() => 'mock goodbye')
  ]);

  // Do tests...

  restoreMocks();
});


@jamiecopeland jamiecopeland changed the title spyOn(...).mockImplementation should return a SpyInstance rather than a mock spyOn(...).mockImplementation should return a SpyInstance rather than a Mock Jun 29, 2017
azasypkin added a commit to azasypkin/kibana that referenced this issue Feb 19, 2018
azasypkin added a commit to azasypkin/kibana that referenced this issue Feb 20, 2018
@duro
Copy link
Contributor

duro commented May 16, 2018

We need this! @alexjoverm @asvetliakov @NoHomey @orta

Can we get an update, they types haven't been updated in over a month

@diofeher
Copy link

diofeher commented Feb 8, 2019

+1 on that

@vkrol
Copy link
Contributor

vkrol commented Feb 8, 2019

Is this still an issue? I don't think so. The example from the first message in this thread is working without type errors. You can try it: https://github.com/vkrol/typescript-jest-spy-on-playground
Steps:

  1. Clone the repository
  2. Run the following commands:
    yarn
    yarn typecheck

@orta
Copy link
Collaborator

orta commented Feb 8, 2019

Is this needed now that there's been some pretty massive refactoring in
#32579 (comment) and #32816 also ?

@orta orta closed this as completed Jun 7, 2021
@orta
Copy link
Collaborator

orta commented Jun 7, 2021

Hi thread, we're moving DefinitelyTyped to use GitHub Discussions for conversations the @types modules in DefinitelyTyped.

To help with the transition, we're closing all issues which haven't had activity in the last 6 months, which includes this issue. If you think closing this issue is a mistake, please pop into the TypeScript Community Discord and mention the issue in the definitely-typed channel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants