Jest箭头语法会导致错误

时间:2017-03-14 09:02:48

标签: javascript ecmascript-6 jestjs webpack-2 babel-jest

我正在为Jest项目设置webpack 2而不是React项目。

我已安装jest-clibabel-jest

我收到了这个错误:

enter image description here

.babel-RC

{
  "presets": [
    "es2015"
  ],
  "plugins": [
    "syntax-jsx",
    ["transform-react-jsx", {"pragma": "html"}]
  ]
}

webpack.config.js

var path = require('path')
var webpack = require('webpack')
var ExtractTextPlugin = require('extract-text-webpack-plugin')

module.exports = {
  devtool: 'source-map',
  entry: [
    'webpack-dev-server/client?http://localhost:3000',
    './src/index'
  ],
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  module: {
    rules: [

      {
        test: /\.js$/,
        use: {
          loader: 'babel-loader'
        }
      },

      {
        test: /\.css$/,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              modules: true,
              localIdentName: '[name]__[local]___[hash:base64:5]'
            }
          }
        ]
      },

      {
        test: /\.styl$/,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              modules: true,
              localIdentName: '[name]__[local]___[hash:base64:5]'
            }
          },
          'stylus-loader'
        ],
      }

    ]
  },
  resolve: {
    extensions: ['.js'],
    modules: [
      path.join(__dirname, 'src'),
      'node_modules'
    ]
  },

  plugins: [
    new ExtractTextPlugin({ filename: 'style.css', allChunks: true }),
  ],

  jest: {
    moduleNameMapper: {
      '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': '<rootDir>/__mocks__/fileMock.js',
      '\\.(css|less)$': '<rootDir>/__mocks__/styleMock.js'
    }
  },

  'env': {
    'test': {
      'plugins': ['transform-es2015-modules-commonjs']
    }
  }
}

的package.json

{
  "name": "test",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "start": "node server.js",
    "test": "jest"
  },
  "babel": {
    "presets": [
      "es2015",
      "react",
      "stage-0"
    ]
  },
  "devDependencies": {
    "babel-core": "^6.23.1",
    "babel-jest": "^19.0.0",
    "babel-loader": "^6.3.2",
    "babel-plugin-transform-object-assign": "^6.22.0",
    "babel-preset-es2015": "^6.22.0",
    "babel-preset-react": "^6.23.0",
    "babel-preset-stage-0": "^6.22.0",
    "css-loader": "^0.26.2",
    "eslint": "^3.17.0",
    "extract-text-webpack-plugin": "^2.0.0",
    "identity-obj-proxy": "^3.0.0",
    "jest": "^19.0.2",
    "jest-cli": "^19.0.2",
    "regenerator-runtime": "^0.10.3",
    "style-loader": "^0.13.2",
    "stylus": "^0.54.5",
    "stylus-loader": "^2.5.0",
    "webpack": "^2.2.1",
    "webpack-dev-server": "^2.4.1"
  },
  "dependencies": {
    "@cycle/dom": "^16.0.0",
    "@cycle/run": "^3.0.0",
    "babel-plugin-transform-react-jsx": "^6.23.0",
    "bulma": "^0.3.2",
    "css-modules-require-hook": "^4.0.5",
    "eslint-plugin-jsx": "^0.0.2",
    "snabbdom-jsx": "^0.3.1",
    "xstream": "^10.3.0"
  }
}

如何摆脱错误?

1 个答案:

答案 0 :(得分:2)

使用arrow function 不带参数仍需要括号。

以下代码应该有效:

test('adds a and b together', () => {
    expect(sum(1, 2)).toBe(3)
})

请注意,不需要括号的唯一情况是只有一个参数

您可以阅读文档here