gpt4 book ai didi

reactjs - Jest 遇到了意外的 token ReactJS

转载 作者:行者123 更新时间:2023-12-03 13:47:15 24 4
gpt4 key购买 nike

我在我的react项目中设置了以下环境。以下是 package.json 文件:-

 {
"name": "testing",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.5.2",
"react-dom": "^16.5.2",
"react-scripts": "2.0.5"
},
"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-core": "^6.26.3",
"babel-jest": "^23.6.0",
"babel-loader": "^8.0.4",
"babel-polyfill": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"enzyme": "^3.7.0",
"enzyme-adapter-react-16": "^1.6.0",
"jest": "^23.6.0",
"react-test-renderer": "^16.5.2"
},
"jest": {
"setupTestFrameworkScriptFile": "<rootDir>/src/setupTest.js",
"transform": {
"\\.js$": "babel-jest"
},
"verbose": true,
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/client/assetsTransformer.js"
},
"moduleFileExtensions": [
"js",
"jsx"
],
"moduleDirectories": [
"node_modules"
]
},
"babel": {
"presets": [
"react",
"es2015"
]
}
}

每当我运行 npm test 命令时,它都会抛出以下错误

enter image description here

我正在处理的组件文件如下所示:-

import React, { Component } from 'react';

class Increment extends Component {
constructor() {
super();
this.state = {
count: 0,
}
}

makeIncrementer = amount => () =>
this.setState(prevState => ({
count: prevState.count + amount,
}));

increment = this.makeIncrementer(1);
decrement = this.makeIncrementer(-1);

render() {
return (
<div>
<p>Count: {this.state.count}</p>
<button className="increment" onClick={this.increment}>Increment count</button>
<button className="decrement" onClick={this.decrement}>Decrement count</button>
</div>
)
}
}

export default Increment;

我编写的测试用例如下:-

import React from 'react';
import { shallow } from 'enzyme';
import Increment from './increment';

describe('App component', () => {

it('starts with a count of 0', () => {
const wrapper = shallow(<Increment />);
const text = wrapper.find('p').text();
expect(text).toEqual('Count: 0');
});
});

你能帮我解决这个问题吗?我尝试了 StackOverflow 上与此特定主题相关的所有解决方案,但无法解决它。

最佳答案

我几天来遇到同样的问题并尝试了不同的解决方案,但它们对我不起作用。然而,我的问题是通过修改 package.json 来修复 "test": "react-scripts test" 或用 "react-scripts test 替换 "jest" " 在脚本下就像这样:package.json:

"scripts": {
"test": "react-scripts test",
}

关于reactjs - Jest 遇到了意外的 token ReactJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52852740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com