-
-
Notifications
You must be signed in to change notification settings - Fork 383
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
TypeError: (0 , _component.default) is not a function mock tests #854
Comments
Hey @tejpowar 👋, |
|
Hi, We basically need to update unit tests whereby we are using loadable and need to mock it. The error is: Test suite failed to run
So the error is actually failing in our component when we run our unit tests. Basically we need to mock out the above code within our unit test |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Did anyone found any solution? |
I'm also looking for a solution. Any help would be appreciated! @tara-singh-danu did you find a solution? |
I am using these libraries now "@testing-library/jest-dom": "5.16.2",
|
@tejpowar how did you mock the loadable import? |
I faced a similar issue and included the function definition in a try catch block and it fixed the related failing test cases. |
Hi @HebleV can you expand a little in the solution? |
I have found a very hacky and ugly solution... It would be nice to know the proper way to do this. Let's assume we import a component (with classes) called Dummy: import loadable, { lazy } from '@loadable/component';
const Dummy = lazy(() => import('../components/Dummy'));
...
// lazyLoaded is a state variable to force Suspense
class SomeClass {
render() {
return (<>
{this.state.lazyLoaded && <Suspense fallback={<div>loading in suspense</div>}><Dummy /></Suspense>
< />)
}
} We can use the following super ugly code in jest to mock it. jest.mock('react', () => {
const React = jest.requireActual('react');
const Suspense = ({children, fallback}) => {
console.log('Suspense called children!!', typeof children, children.toString());
console.log('Suspense called fallback!!', typeof fallback, fallback.toString());
return children;
};
return {
...React,
Suspense
};
});
jest.mock('@loadable/component', () => {
const loadable = jest.requireActual('@loadable/component');
// The trick is to convert the async dynamic import we do not have in node, to a syncronous require!!!!!!
const lazy = (importer) => {
const matchCondition = `${importer?.toString()}`.match('Dummy');
if(matchCondition) {
return require('../components/Dummy').default;
}
return importer;
};
loadable.lazy = lazy;
return {
__esModule: true,
default: (children) => {
return children;
},
loadable,
lazy
};
}); |
Note - you do mock However, I should ask other questions
|
Hello @theKashey, I mock I manage to get this working on React 17, even though it is not supported. It might work on 16, but no idea... I would love to know the proper way to do this :-) |
Follow Jest babel's configuration (usually just
You cannot mock loadable, as you need "something" to transform |
Well, the whole point is to deal with I will take a look to the following example to see if I can make it working. Otherwise, I'll go with the ugly solution. Thanks for your help. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
There is something wrong with this package export. Seems it provides different exports in ESM and CJS. In ESM I fixed it by writing import rawLoadable from '@loadable/component';
const loadable = typeof rawLoadable === 'function' ? rawLoadable : rawLoadable.default; |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Finally found rather simple solution which works for me without mocking: import { render,screen } from '@testing-library/react';
import LoadableComponent from './LoadableComponent';
test('it renders', async () => {
const name = "LoadableComponent"
render(<LoadableComponent name={name} />);
expect(await screen.findByText(name)).toBeInTheDocument();
}); LoadableComponent file: import loadable from '@loadable/component';
const LoadableComponent = loadable(
() => import('your-library-path'),
);
export default LoadableComponent; |
@revenokanton dude, it's amazing!!! |
💬 Questions and Help
Hi,
We are using the loadable/component function to load components but are having issues with the mocking tests and running into the following error:
TypeError: (0 , _component.default) is not a function
Within our test we have just created a mock like so
Also tried
But this still errors.
Any ideas on how we can mock it?
The text was updated successfully, but these errors were encountered: