gpt4 book ai didi

jasmine - 语法错误: Unexpected token static

转载 作者:行者123 更新时间:2023-12-01 20:17:04 24 4
gpt4 key购买 nike

我目前正在尝试评估与 React 配合使用的不同测试框架,结果发现 Jest 就在我的列表中。但是,我尝试使用此处概述的静态属性:https://github.com/jeffmo/es-class-fields-and-static-properties

因此,我引用了 Jest 主页上提供的教程,并添加了一个静态 propTypes 属性,我的代码如下所示:

import React from 'react';

class CheckboxWithLabel extends React.Component {

static defaultProps = {}

constructor(props) {
super(props);
this.state = {isChecked: false};

// since auto-binding is disabled for React's class model
// we can prebind methods here
// http://facebook.github.io/react/blog/2015/01/27/react-v0.13.0-beta-1.html#autobinding
this.onChange = this.onChange.bind(this);
}

onChange() {
this.setState({isChecked: !this.state.isChecked});
}

render() {
return (
<label>
<input
type="checkbox"
checked={this.state.isChecked}
onChange={this.onChange}
/>
{this.state.isChecked ? this.props.labelOn : this.props.labelOff}
</label>
);
}
}

module.exports = CheckboxWithLabel;

当我运行测试(npm test 或 jest)时,它会抛出以下错误:

➜  jest            
Using Jest CLI v0.8.2, jasmine1
FAIL __tests__/CheckboxWithLabel-test.js
● Runtime Error
SyntaxError: Desktop/jest/examples/react/CheckboxWithLabel.js: Unexpected token (5:22)

我的 package.json 文件如下所示:

{
"dependencies": {
"react": "~0.14.0",
"react-dom": "~0.14.0"
},
"devDependencies": {
"babel-jest": "*",
"babel-preset-es2015": "*",
"babel-preset-react": "*",
"jest-cli": "*",
"react-addons-test-utils": "~0.14.0"
},
"scripts": {
"test": "jest"
},
"jest": {
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
"unmockedModulePathPatterns": [
"<rootDir>/node_modules/react",
"<rootDir>/node_modules/react-dom",
"<rootDir>/node_modules/react-addons-test-utils",
"<rootDir>/node_modules/fbjs"
],
"modulePathIgnorePatterns": [
"<rootDir>/node_modules/"
]
}
}

关于我在这里缺少什么有什么想法吗?

谢谢。

最佳答案

Any ideas on what I'm missing here?

类属性既不是 es2015 的一部分,也不是 react 预设的一部分。

您必须加载处理类属性的插件:

npm install babel-plugin-transform-class-properties babel-plugin-syntax-class-properties

在您的 .babelrc 文件中(现有插件或预设旁边):

"plugins": [
"syntax-class-properties",
"transform-class-properties"
]

关于jasmine - 语法错误: Unexpected token static,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34821270/

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