I’m currently facing an issue while trying to test my react application using Jest.
While I try to test a component that imports an Ionic module, I have the following error message “import statement outside a module”.
This is my test file :
import { render, cleanup } from '@testing-library/react';
import { IonImg } from '@ionic/react';
import '@ionic/react-test-utils';
describe('<IonImg />', () => {
afterEach(cleanup);
it('renders an image with a valid src', () => {
const { getByAltText } = render(
<IonImg src="http://placehold.it/200x200" alt="Test image"></IonImg>
);
expect(getByAltText('Test image')).toHaveAttribute('src', 'http://placehold.it/200x200');
});
});
The error occurs on this line :
import { IonImg } from '@ionic/react';
I tried several things like :
- Use experimental feature from jest to import ES6 Module (https://jestjs.io/docs/ecmascript-modules)
- Use babel to transform this module through babel-jest preset (https://github.com/jestjs/jest/issues/9395)
But nothing has made it work, can someone help me please ?