- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好吧,我在尝试在我的 React-native 应用程序上启用 Jest 时遇到了很大的问题。本主题的每个答案都为我的 webpack 或 package.json 提供了不同的配置。我希望能在这里找到答案。
对于这个简单的测试:
import 'react-native';
import React from 'react';
import Index from '../index.ios.js';
// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';
it('renders correctly', () => {
const tree = renderer.create(
<Index />
);
});
我运行:npm test
并得到:
SyntaxError: D:\workspaces\web\react\site\node_modules\react-native\jest\mockComponent.js: Support for the experimental syntax 'classProperties' isn't currently enabled (20:24):
有一个可爱的解决方案:
Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the 'plugins' section of your Babel config to enable transformation.
但是我不知道该怎么做。
这是我的 package.json 配置:
{
"name": "site",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"build-sitedev-home-css": "NODE_ENV=sitedev node_modules/webpack/bin/webpack.js --config ./modules/Home/assets/css/src/webpack.config.js",
"build-sitedev-home-js": "NODE_ENV=sitedev node_modules/bundle-js/bin/bundle-js.js ./modules/Home/assets/js/src/entryfile.js --disable-beautify -p > ./modules/Home/assets/js/build/bundle.js",
"startweb": "cross-env NODE_ENV=webdevelopment node_modules/webpack/bin/webpack.js --watch",
"buildweb": "cross-env NODE_ENV=webproduction node_modules/webpack/bin/webpack.js -p",
"test": "jest",
"android": "react-native run-android",
"ios": "react-native run-ios",
"android:expo": "react-native-scripts android",
"react-devtools": "react-devtools"
},
"dependencies": {
"abortcontroller-polyfill": "1.3.0",
"adb": "0.2.0",
"ag-grid": "18.1.1",
"ag-grid-react": "18.1.0",
"blueimp-file-upload": "9.32.0",
"bundle-js": "1.0.3",
"chart.js": "2.8.0",
"elastic-apm-js-base": "3.0.0",
"jquery": "3.4.1",
"lodash.get": "4.4.2",
"lodash.isequal": "4.5.0",
"material-ui": "0.20.2",
"meow": "5.0.0",
"moment": "2.24.0",
"moment-timezone": "0.5.26",
"qrcode.react": "0.9.3",
"react": "16.4.2",
"react-art": "16.8.6",
"react-chartjs": "1.2.0",
"react-dom": "16.8.6",
"react-dom-factories": "1.0.2",
"react-infinite-scroller": "1.2.4",
"react-native": "^0.60.5",
"react-native-calendars": "1.21.0",
"react-native-drawer": "2.5.1",
"react-native-floating-label-text-input": "0.1.5",
"react-native-progress-circle": "2.0.1",
"react-native-snap-carousel": "3.8.0",
"react-native-vector-icons": "4.6.0",
"react-native-video": "4.4.4",
"react-native-web": "0.11.5",
"react-native-web-linear-gradient": "1.0.5",
"react-tooltip": "3.10.0",
"video.js": "7.6.0",
"xdate": "0.8.2"
},
"devDependencies": {
"@babel/core": "7.4.3",
"@babel/plugin-proposal-class-properties": "7.5.5",
"@babel/preset-env": "7.4.3",
"@babel/preset-es2016": "7.0.0-beta.53",
"@babel/preset-react": "7.0.0",
"@babel/runtime": "7.4.3",
"@types/prop-types": "15.7.1",
"babel-cli": "6.26.0",
"babel-core": "6.22.1",
"babel-eslint": "10.0.1",
"babel-jest": "24.1.0",
"babel-loader": "6.2.10",
"babel-plugin-transform-async-to-generator": "6.24.1",
"babel-plugin-transform-runtime": "6.23.0",
"babel-preset-env": "1.7.0",
"babel-preset-es2016": "6.24.1",
"babel-preset-react": "6.24.1",
"babel-preset-react-native": "3.0.2",
"babel-preset-stage-0": "6.24.1",
"babel-register": "6.26.0",
"cross-env": "5.2.0",
"css-loader": "3.1.0",
"eslint": "5.16.0",
"eslint-plugin-react": "7.12.4",
"expo": "33.0.7",
"extract-text-webpack-plugin": "3.0.2",
"file-loader": "4.1.0",
"jest": "24.8.0",
"jest-react-native": "18.0.0",
"metro-react-native-babel-preset": "0.55.0",
"react-devtools": "3.4.3",
"react-native-scripts": "2.0.1",
"react-test-renderer": "16.4.2",
"style-loader": "0.23.1",
"webpack": "3.10.0",
"webpack-dev-server": "3.2.1"
},
"jest": {
"preset": "react-native"
},
"rnpm": {
"assets": [
"./assets/fonts/"
]
}
}
这是我的 webpack 配置:
var debug = process.env.NODE_ENV !== "webproduction";
var webpack = require('webpack');
var path = require('path');
var fs = require('fs');
var buildPath = __dirname + "/../../siteweb/site/www/themes/tmpux/assets/js/uxbuild";
var wpk_local_configuration = {};
if (fs.existsSync('./local.webpack.config.js')) {
let local_webpack_config = require('./local.webpack.config');
buildPath = local_webpack_config.buildPath;
wpk_local_configuration = local_webpack_config.wpk_local_configuration;
}
console.log('buildPath = ' + buildPath);
console.log('wpk_local_configuration = ' + JSON.stringify(wpk_local_configuration));
if (!fs.existsSync(buildPath)) {
fs.mkdirSync(buildPath);
}
fs.createReadStream(__dirname + "/components/parts/PDFViewer/configPDFViewer.js")
.pipe(fs.createWriteStream(buildPath + "/configPDFViewer.js"));
var _version = new Date().getTime();
console.log('version = ' + _version);
var wpk_configuration = {
entry: {
main: './index.web.js',
},
output: {
path: buildPath,
filename: "bundle.main.js",
publicPath: "./assets/js/uxbuild/",
chunkFilename: "bundle.[name].js"
},
module: {
loaders: [
{
test: /\.js?$/,
exclude: function (modulePath) {
return (
/node_modules/.test(modulePath) &&
!/node_modules(\\|\/)react-native-datepicker/.test(modulePath) &&
!/node_modules(\\|\/)react-native-progress-circle/.test(modulePath) &&
!/node_modules(\\|\/)react-native-snap-carousel/.test(modulePath)
);
},
loader: 'babel-loader',
query: {
presets: ['es2016', 'react-native'],
plugins: ['transform-runtime']
},
},
{
test: /\.(png|jpg|gif|mp4)$/,
loader: 'file-loader?context=' + __dirname + "/themes/tmpux/assets/js/components/" + '&emitFile=true&name=[path][hash].[ext]',
options: {
publicPath: "./assets/js/uxbuild/"
}
},
{
test: /\.css$/,
loader: 'style-loader!css-loader'
},
{
test: /\.ttf$/,
loader: 'file-loader',
include: path.resolve(__dirname, "node_modules/react-native-vector-icons"),
}
],
},
resolve: {
alias: {
'react-native': 'react-native-web',
'jquery-ui/ui/widget': 'blueimp-file-upload/js/vendor/jquery.ui.widget.js',
},
extensions: [ '.web.js', '.js' ]
},
plugins: debug ? [
new webpack.optimize.CommonsChunkPlugin({
name: 'node-static',
path: buildPath,
filename: 'node-static.js',
minChunks(module, count) {
var context = module.context;
return context && context.indexOf('node_modules') >= 0;
}
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('development')
}
}),
] : [
new webpack.optimize.CommonsChunkPlugin({
name: 'node-static',
path: buildPath,
filename: 'node-static.js',
minChunks(module, count) {
var context = module.context;
return context && context.indexOf('node_modules') >= 0;
}
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({ mangle: false, sourceMap: true }),
]
};
module.exports = Object.assign(wpk_configuration, wpk_local_configuration);
还有我的本地 babel 配置:local.webpack.config
module.exports = {
buildPath: __dirname + "/../../siteweb/site/www/themes/tmpux/assets/js/uxbuild",
wpk_local_configuration: {
devtool: 'eval-source-map',
},
presets: ["module:metro-react-native-babel-preset"],
plugins: ["@babel/plugin-proposal-class-properties"]
};
最佳答案
将其添加到 package.json
"jest": {
"preset": "react-native",
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
}
},
关于javascript - 使用 Jest、Babel、Webpack 和 React 进行测试。目前尚未启用对实验性语法 'classProperties' 的支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57809391/
我正在为我的 React 项目设置 Webpack,并对 babel 感到困惑, babel-core , babel-loader , babel-preset-2015和babel-preset-
所有这些 babel 依赖项是什么?它们各自的用途是什么,它们有何不同?我应该为我的 nodejs 网络应用程序使用哪一个? 我已经查看了 API 网站,但是否有任何指南可以将这些内容转化为简单的人类
我的 Node Webpack 项目使用了三个 babel 库。它们之间有什么区别以及如何使用它们? "dependencies": { "babel-runtime": "^5.8.24" }
我用 @babel/core 替换了 babel-core 而 babel-loader 期望 babel-core: { "name": "myproject-ui", "version":
我尝试配置一个环境来使用 babel 和 webpack 开发 javascript。 但是我不明白关于presets的babel配置. 在 Usage Guide ,我们可以看到预设为 "@babe
我已经安装了 babel 7.5,理想情况下它应该以 preset-env 包为目标,但不确定它为什么要寻找“babel-preset-es2015”。 你们能告诉我我做错了什么吗?下面是我的代码 p
我正在更新a boilerplate中使用的babel包,从 babel-core、babel-register 等到@babel/core、@babel/register > 等 问题:在 npm
设置 通天塔 6 (^6.0.0), Node 5.4.0,Express 4.13.x, babel-node 和 babel-register 都有警告,禁止在生产环境中使用babel.io 网站
我正在尝试转换编译我的 react/es6 代码并且来自 browserify。由于新的 babel 6 版本以及大多数教程现在已经过时的事实,我正在努力创建一个 webpack 构建。这适用于我的
我有一个使用 Typescript 和 @babel/preset-env 的项目。与 Webpack 捆绑失败并显示此错误和以下配置。 如果我取消注释该行,这会强制 @babel/plugin-pr
我在浏览器中使用 babel -> babel-standalone 现在我想使用 ES decorator 语法。但是所有的通天塔doc intro 是服务器端的 babel,比如 ` { "p
我正在编写对从 cdnjs.com 引入的所有这些库的 react 。但是,我发现它报告错误:'Uncaught TypeError: Cannot read property 'keys' of u
我正在使用 babel v7.6.x 并设置了以下内容。 包.json "scripts": { "dev": "nodemon --exec babel-node bin/index.js
我正在学习巨大的 JavaScript 生态系统,但我无法理解以下内容。 Babel 是一个编译器,来自官网: Babel is a toolchain that is mainly used to
在我使用yarn add -D babel-plugin-react-relay安装relay-query插件后,并在运行开发服务器后,我收到此错误: Error: [BABEL] /Users/ne
我使用 babel 6 和 React 插件,并按照文档说明设置转译过程。我已经阅读过要让 React 工作,我需要使用 es2015 和 React preset。最初,使用这两个预设一切都运行良好
我对 Babel 选项/配置有点迷失。我想使用最新的 js 功能并编译(使用 webpack)为浏览器代码。 babel-polyfill 和有什么区别和 babel plugins与 babel-p
这个问题在这里已经有了答案: Babel file is copied without being transformed (10 个答案) 关闭 6 年前。 我只是使用 npm (npm inst
安装时 npm install browserify babelify babel-preset-es2015 我正面临以下警告信息 npm WARN deprecated babel-preset-
什么版本的标准? 12.0.1 什么操作系统、Node.js 和 npm 版本? 视窗 10, 节点 v10.15.1, NPM v.6.8.0 你期望会发生什么? 我在 devDependencie
我是一名优秀的程序员,十分优秀!