【发布时间】:2019-04-05 09:12:18
【问题描述】:
我正在尝试对使用装饰器和 Typescript 与 Create React App v2.1.0 配合使用的 React 应用程序进行测试
我知道装饰器不受官方支持。
感谢React App Rewired 和@babel/plugin-proposal-decorators,我可以正常运行应用程序。
我卡住的地方是在运行测试时启用装饰器支持。
我的 npm test 脚本是:"test": "react-app-rewired test --env=jsdom --runInBand"
测试失败:
“decorators”插件需要一个“decoratorsBeforeExport”选项, 其值必须是布尔值。如果您从巴比伦/巴别塔迁移 6 或者想使用旧的装饰器建议,你应该使用 'decorators-legacy' 插件而不是 'decorators
我尝试添加一个 .babelrc 文件,其中包含以下内容:
{
"plugins": [["@babel/plugin-proposal-decorators", { "legacy": true }]]
}
但会被:
不能同时使用 decorators 和 decorators-legacy 插件
【问题讨论】:
-
在 babelrc 添加这些:
[ "@babel/plugin-proposal-decorators", { "legacy": true } ], [ "transform-class-properties", { "loose": true }]并安装允许您使用装饰器的转换类属性 -
@somsgod 这会导致相同的错误消息
标签: reactjs typescript jestjs create-react-app