0

I start a new react project with createReactApp then I used this manual to configure the test for react https://jestjs.io/docs/en/tutorial-react

my babel.config.js

module.exports = {
  presets: ['@babel/preset-env', '@babel/preset-react'],
};

some json in package.json

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "jest",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "devDependencies": {
    "@babel/preset-env": "^7.5.5",
    "@babel/preset-react": "^7.0.0",
    "babel-jest": "^24.9.0",
    "enzyme": "^3.10.0",
    "jest": "^24.9.0",
    "react-test-renderer": "^16.9.0"
  }
}

my test file src/tests/Card.test.js

import React from 'react';
import {shallow} from 'enzyme';
import Cards from "../components/Cards"


describe('Card.js', () => {
  const component = shallow(<Cards />, { });

  it('check exists card', () => {
    expect(component).toBe(true);
  });

});

when I run yarn test I get this error

yarn run v1.15.2
$ jest
 FAIL  src/tests/App.test.js
  ● Test suite failed to run

    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.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    /home/vladimir/Data/Dev/Projects/HomeProject/english_cards_frontend/src/App.css:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){.App {
                                                                                             ^

    SyntaxError: Unexpected token .

      1 | import React, { Component } from 'react';
    > 2 | import './App.css';
        | ^
      3 | import Cards from './components/Cards'
      4 | 
      5 | class App extends Component {

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
      at Object.<anonymous> (src/App.js:2:1)

how to fix it ?
I tried to do jest --no-cache, delete the node modules folder and reinstall , tried this https://stackoverflow.com/a/51994539/9098461 and this https://stackoverflow.com/a/52621836/9098461
Thanks in advance for your help

guest381
  • 155
  • 1
  • 2
  • 12

2 Answers2

0

Use Configure method from enzyme library

import { shallow,configure } from 'enzyme';

configure({ adapter: new Adapter() })
0

when I run this command react-scripts test everything worked, I don't know how it is connected with

guest381
  • 155
  • 1
  • 2
  • 12