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鈥檒l 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

Closed
alexanderkjeldaas opened this issue May 11, 2020 · 8 comments 路 Fixed by #11537
Closed

Comments

@alexanderkjeldaas
Copy link

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
Member

SimenB commented May 11, 2020

Wanna send a PR for it?

@alexanderkjeldaas
Copy link
Author

If I knew how it worked, maybe.

@aldeed
Copy link

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

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

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 commented Oct 1, 2020

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

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants