开玩笑地遇到了意外的令牌-看起来像无法转换jsx元素

时间:2019-07-17 15:57:01

标签: reactjs jestjs babel-jest

我最初是从react-scripts测试开始的,但是Console.log的行为不正常(有时会打印消息),所以我决定继续玩笑(console.log与jest一起工作,并且jest可以支持的不仅仅是react-脚本测试)。

现在,我遇到了错误。

FAIL src / App.test.js   ●测试套件无法运行

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

App.test.js:意外令牌(11:20)

  it('renders without crashing', () => {
    const div = document.createElement('div');
  ReactDOM.render(<App />, div);
                     ^
   ReactDOM.unmountComponentAtNode(div);
});

环境 此应用程序在Windows 10专业版上

节点v10.9.0  npm 6.2.0

我已关注Does Jest support ES6 import/export?  但是它会抛出相同的错误,第0阶段是什么,为什么第0阶段很危险?

package.json文件     在此处输入代码

{
"name": "my-ui",
"version": "0.1.0",
"private": true,
"dependencies": {
"node-sass": "^4.12.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^4.3.1",
"react-scripts": "^2.1.8"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"build-dev": "dotenv -e .env.development 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-jest": "^24.8.0",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"dotenv-cli": "^1.4.0",
"jest": "^24.8.0",
"jsx-to-string": "^1.4.0",
"react-test-renderer": "^16.8.6"
},
".babelrc": {
"presets": [
"es2015",
"react"
]
}
}

我有两个测试用例 其中一个向我展示了预期的行为,而另一个向我抛出了错误。

我有App.test.js文件

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

//below test case throws above error.
it('renders without crashing', () => {
const div = document.createElement('div');
 ReactDOM.render(<App />, div);
ReactDOM.unmountComponentAtNode(div);
});

//below should print output - working correctly.
it('it is printing', () => {
console.log('Hellllooo');
expect(true).toBe(true);
});

0 个答案:

没有答案