【发布时间】:2017-09-16 15:16:36
【问题描述】:
我知道这在某种程度上是一个重复的问题,但到目前为止我发现的任何提示都没有帮助,这就是我决定再次提问的原因。
我在 Mocha 中创建了一个简单的测试,当我尝试运行它时,我不断收到 unexpected token import 错误。我在这里和其他地方尝试了许多不同的解决方案,但它们似乎都与我的情况无关。由于我是一名初级程序员,我并不理解我找到的所有答案,因此我无法在此处列出所有答案。不过,最常给出的提示是使用--compilers js:babel-core/register.,但是,这在我的情况下不起作用。下面是我的package.json:
`{
"name": "beer-guru",
"version": "1.0.0",
"description": "A simple app displaying info about various beers",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --inline --hot --open",
"prettier": "prettier --single-quote --write ./app/**/*.js",
"lint": "eslint **/*.js",
"test": "mocha **/*.test.js"
},
"keywords": [
"React.js"
],
"author": "Maciek Maslowski",
"license": "ISC",
"dependencies": {
"lodash": "^4.17.4",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react-router": "^4.1.2",
"react-router-dom": "^4.1.2",
"styled-components": "^2.1.1",
"styled-tools": "^0.1.4"
},
"devDependencies": {
"babel-core": "^6.22.1",
"babel-eslint": "^7.2.3",
"babel-loader": "^6.2.10",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.22.0",
"eslint": "^4.4.1",
"eslint-loader": "^1.9.0",
"eslint-plugin-react": "^7.2.1",
"expect": "21.0.2",
"html-webpack-plugin": "^2.26.0",
"mocha": "3.5.3",
"prettier": "^1.5.3",
"react-redux": "5.0.6",
"redux": "3.7.2",
"supertest": "3.0.0",
"webpack": "^1.14.0",
"webpack-dev-server": "^1.16.2"
}
}`
我的.babelrc:
"presets": [
"es2015", "react", "env"
],
"plugins": ["transform-class-properties"]
还有我的webpack.config.js:
var HtmlWebpackPlugin = require('html-webpack-plugin');
var HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
template: __dirname + '/app/index.html',
filename: 'index.html',
inject: 'body'
});
module.exports = {
entry: [
'./app/index.js'
],
devServer: {
historyApiFallback: true
},
output: {
path: __dirname + '/dist',
filename: "index_bundle.js"
},
module: {
loaders: [
{test: /\.js$/, exclude: /node_modules/, loaders: ["babel-loader", "eslint-loader"]}
]
},
plugins: [HtmlWebpackPluginConfig]
}
这里有没有人知道是否可以使用此配置运行 Mocha 测试?如果是这样,有人知道怎么做吗?
非常感谢所有提示!
【问题讨论】:
-
Mocha 现在原生支持 ESM。 See here了解详情。