X Tutup
The Wayback Machine - https://web.archive.org/web/20201222155611/https://github.com/facebook/jest/issues/10021
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

https://jestjs.io/docs/en/mock-functions.html does not document ES6 imports. #10021

Open
alexanderkjeldaas opened this issue May 11, 2020 · 7 comments

Comments

@alexanderkjeldaas
Copy link

@alexanderkjeldaas alexanderkjeldaas commented May 11, 2020

There is no information on how to submit documentation bugs, so I assume it is classified as a bug.

🐛 Bug Report

The "mocking functions" page at https://jestjs.io/docs/en/mock-functions.html

does not document an example using ES6 imports like this:

import { foo } from 'bar';

To Reproduce

Goto https://jestjs.io/docs/en/mock-functions.html

Expected behavior

Documentation on how to mock a function using ES6 import syntax.

Link to repl or repo (highly encouraged)

envinfo


@SimenB
Copy link
Collaborator

@SimenB SimenB commented May 11, 2020

Wanna send a PR for it?

@alexanderkjeldaas
Copy link
Author

@alexanderkjeldaas alexanderkjeldaas commented May 11, 2020

If I knew how it worked, maybe.

@aldeed
Copy link

@aldeed aldeed commented May 11, 2020

@alexanderkjeldaas there's a related comment here, but also I just filed a related issue here

It's possible that the "correct" way to do it is different with or without Babel. Ideally I don't think it should be to avoid confusion.

Agree this should be documented after this is all working properly. Hopefully there are some test cases that could just be pasted into the docs as simple examples?

@alexanderkjeldaas
Copy link
Author

@alexanderkjeldaas alexanderkjeldaas commented May 12, 2020

I can do this:

import * as bar from './bar'; 

jest.mock('./bar');

bar.foo.mockReturnValue('baz');

But this doesn't work for me:

import { foo } from './bar'; 

jest.mock('./bar');

foo.mockReturnValue('baz');

So whether this is indeed a limitation, or maybe I have configured something wrong? It would be good if the docs said something on this topic.

@morgan-sam
Copy link

@morgan-sam morgan-sam commented Aug 22, 2020

Started working on this issue but not sure what is needed.

There is already a section on ES6 class mocks.

Are the examples @alexanderkjeldaas provided enough? Both examples worked fine when I tested them.

@alexanderkjeldaas
Copy link
Author

@alexanderkjeldaas alexanderkjeldaas commented Aug 22, 2020

If both should work, then I guess at least giving one example that should work would help, because then the user would at least suspect that it's something wrong in their setup instead of thinking that the ES6 syntax or mocking they are doing is wrong.

@XifeiNi
Copy link

@XifeiNi XifeiNi commented Oct 1, 2020

if no one else works on it, I would like to give it a try as my first issue. :)

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

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.
X Tutup