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

Feature req: Add static functions for mock functions. #661

Closed
TKJohn opened this issue Aug 5, 2018 · 4 comments
Closed

Feature req: Add static functions for mock functions. #661

TKJohn opened this issue Aug 5, 2018 · 4 comments

Comments

@TKJohn
Copy link

TKJohn commented Aug 5, 2018

Issue :

When mocking module by jest.mock(), I would like to use jest.fn() to mock functions of the module.

jest.mock("../src/dependency", () => {
  return {
    dependencyFunc: jest.fn()
  };
});

Latter in the actual tests, I need to implement different behavior for the functions.

dependencyFunc.mockImplementation(() => {
  return "modified mock implementation";
});

The mock functions works fine with JS. But in TypeScript, with dependencyFunc has no defined mockImplementation, the IDE(vscode) shows error

[ts] Property 'mockImplementation' does not exist on type '(input: string) => string'.

The test can still run successfully. But I think it would be better to have some global functions to do the implement. Likes when in mockito.

Expected behavior :

I would suggest to add global/static functions for mock functions. Something could be used like:

jest.mockFn.mockImplementation(dependencyFunc, () => {
  return "modified mock implementation";
});

Output from debug log :

# content of debug.txt :

none

Minimal repo :

https://github.com/TKJohn/ut-ts-jest
https://github.com/TKJohn/ut-ts-jest/blob/master/test/dependent-modify.spec.ts

@huafu
Copy link
Collaborator

huafu commented Aug 5, 2018

Thanks @TKJohn. The easiest workaround is to put you jest.mock('xxx') call before the actual import ... from 'xxx' in your test file.

Also check https://github.com/kulshekhar/ts-jest/wiki/Troubleshooting#jestmock-hoisting. I have no time to investigate more right now sorry. In case what I quickly gave you here fixes your bug, thanks for closing this issue.

@TKJohn
Copy link
Author

TKJohn commented Aug 5, 2018

Thanks @huafu. And sorry for my misleading.

It is not a bug, the test works well as expect. Mocks works fine. Not the case in the trouble shooting.

The only problem is the IDE(as well TS) complain about 'mockImplementation' is not defined in the (actually mocked) function.

Tested putting jest.mock('xxx') call before the actual import ... from 'xxx' in the test file, but doesn't make different.

@huafu
Copy link
Collaborator

huafu commented Aug 5, 2018

oh ok, i misunderstood sorry. There is already an issue for this, it's typings for mock or so. It's a won't fix for now. Can't search for the issue now, but there is more info related to that in there, you should look for it.

@TKJohn
Copy link
Author

TKJohn commented Aug 5, 2018

Found out #576. And it is a good hint to use type case to deal with type issus.
I'll close this one. Thanks a lot for help.

@TKJohn TKJohn closed this as completed Aug 5, 2018
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

2 participants