I am trying to test with Jest, but got an error: Jest encountered an unexpected token This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
I have a photo gallery app and whenever I click on an image, a modal pops up with an image. I want to test that when I click on the image, the popup shows or exists. Is there a way to factor this?
import { shallow, mount, render } from 'enzyme';
import App from '../client/src/components/App';
describe('<PhotoItem />', () => {
it('should popup image on click', () => {
const wrapper = shallow(
<App title="mock" />
);
wrapper.find('.item item-0').simulate('click');
expect('.modal-opactiy').toExist();
});
HERE is my package.json:
"jest": {
"verbose": true,
"transformIgnorePatterns": ["/node_modules/(?!App)"],
"testURL": "http://localhost/photos"
}
I solved a similar problem by doing the following:
1- add enzyme setup file and write the following:
2- add jest configuration to your package.json like that:
3- add .babelrc file to your root path and write the following in it:
4- if you got an error on "expect" keyword in your test just run
npm install -D chai
and import the expect function in your test code like thatimport { expect } from 'chai';
if you still get an error try to install babel dependencies like that
npm install -D @babel/core @babel/preset-env @babel/preset-react
hope this helps.
Here it is the same problem: Jest Error Unexpected token on react component
You need to add a .babelrc file with: