【问题标题】:Jest encountered an unexpected token it's not plain JavaScriptJest 遇到了一个意外的令牌,它不是纯 JavaScript
【发布时间】:2020-01-01 10:17:05
【问题描述】:

我使用 createReactApp 开始了一个新的 react 项目,然后我使用本手册配置了 react https://jestjs.io/docs/en/tutorial-react 的测试

我的 babel.config.js

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

package.json 中的一些 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"
  }
}

我的测试文件 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);
  });

});

当我运行 yarn test 时出现此错误

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)

如何解决?
我试着做jest --no-cache,删除节点模块文件夹并重新安装,试过这个https://stackoverflow.com/a/51994539/9098461和这个https://stackoverflow.com/a/52621836/9098461
提前感谢您的帮助

【问题讨论】:

  • React cra 已经配置好了 jest。

标签: javascript reactjs jestjs


【解决方案1】:

使用酶库中的配置方法

import { shallow,configure } from 'enzyme';

configure({ adapter: new Adapter() })

【讨论】:

    【解决方案2】:

    当我运行这个命令react-scripts test 一切正常,我不知道它是如何连接的

    【讨论】:

      猜你喜欢
      • 2019-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      • 2019-10-07
      • 2019-12-13
      • 1970-01-01
      • 2021-12-07
      相关资源
      最近更新 更多