Синтаксис стрелки Jest вызывает ошибку

Я настраиваю Jest для проекта webpack 2, который не является проектом React.

Я установил jest-cli и babel-jest.

Я получаю эту ошибку:

введите здесь описание изображения

.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"
  }
}

Как я могу избавиться от ошибки?


person BeniaminoBaggins    schedule 14.03.2017    source источник
comment
ты забыл скобки, () => { ... }   -  person Reuven Chacha    schedule 14.03.2017


Ответы (1)


Использование arrow function без параметров по-прежнему требует круглых скобок.

Следующий код должен работать:

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

Обратите внимание, что круглые скобки не нужны только в том случае, когда есть только один параметр.

Вы можете прочитать документацию здесь.

person Erazihel    schedule 14.03.2017
comment
Спасибо. Похоже, у меня есть еще ошибки, вероятно, не связанные с этой. .toBe() выдает ошибку: sum(...).toBe is not a function и to.be() выдает ошибку: Cannot read property 'be' of undefined - person BeniaminoBaggins; 14.03.2017
comment
Как упоминалось в документации Jest, .toBe() нужно вызывать после expect(...), а не внутри (как я сделал в своем ответе) :) - person Erazihel; 14.03.2017