-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
[Bug] Unable to mock default exported Component when same module also has named export #5579
Comments
Hey @zpalexander, thanks for the question I'm sure it's pretty frustrating Unfortunately, this issue tracker is not a help form. If you could open a new StackOverflow issue (as you mentioned you would do in the first SO issue) or ping me on our discord channel, I'd be happy to help there! |
Happy to ask in the appropriate channel, but before I do can you just confirm that this is expected behavior / a WONTFIX bug? I opened a bug here because the API is behaving inconsistently and there's no documentation around the discrepancy, and I think it'd be helpful for people coming to the issue queue with the same question to have a definitive statement that it is supposed to work this way. Thanks for the prompt reply and the offer of help! Edit: I've opened a StackOverflow issue in place of this issue: https://stackoverflow.com/questions/48831701/jest-how-to-mock-default-export-component-when-same-module-also-has-named-expor |
For others, this helped me out: jest.mock('./myModule', () => ({
__esModule: true,
namedExport: jest.fn(),
default: jest.fn(),
}));
import myModule, { namedExport } from './myModule'; |
To follow up, the solution I ended up using was: MyComponent.mockImplementation(() => {
return {
render: () => <div>MockComponent</div>
};
}); The solution that @joaovieira posted works as well. Hopefully Jest can eventually provide users with a mocking API that requires less boilerplate. |
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. |
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
I have an ES6 module that exports a React Component class by default, but also exports a plain JS function as a named export. When testing other packages that use this module, I want to mock both the default exported component and named exported function to keep my unit tests pure.
The module looks something like this:
I would like to use the following syntax to mock the exports:
When I try to use this syntax, however,
MyComponent
does not appear to be mocked when used within other components. When I try to mockMyComponent
like this and render it on its own, it renders out to null.This behavior is very strange, because if I use the exact same syntax, but both imports are JavaScript functions, the mocking works as expected. See the StackOverflow issue I opened here that confirms that the syntax works when the imports are both functions.
Here is a GitHub repo demoing the problem, as well as several solutions I've tried: https://github.com/zpalexander/jest-enzyme-problem
What is the expected behavior?
I should be able to import the default exported component and the named exported function into my test file, and mock both using
jest.mock('path')
and the.mockImplementation()
method.Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.
See this repo for an exact reproduction: https://github.com/zpalexander/jest-enzyme-problem
You can build the repo and run the tests with
yarn install && yarn test
Thanks!
The text was updated successfully, but these errors were encountered: