Jest:意外的令牌@ Decorator

时间:2017-10-31 12:21:31

标签: reactjs decorator enzyme jest mobx

我收到错误:测试套件无法运行:意外令牌(5:0)

3 | import Locale from '../stores/view/language'
4 |
5 | @observer
  | ^
6 | export default class DateFormat extends Component {
7 |   constructor(props) {
8 |     super(props)

我正在使用Webpack + Babel + Jest + Enzyme +反应+ Mobx

这是我的一些package.json

{
  "scripts": {
    "test": "jest",
  },
  "devDependencies": {
    "babel-eslint": "8.0.1",
    "babel-jest": "21.2.0",
    "enzyme": "3.1.0",
    "enzyme-adapter-react-16": "1.0.2",
    "jest": "21.2.1",
  },
  "babel": {
    "presets": [
      "env",
      "react"
    ],
    "env": {
      "test": {
        "presets": [
          "env",
          "react"
        ]
      }
    }
  },
  "dependencies": {
    "axios": "0.16.2",
    "babel-core": "6.26.0",
    "babel-loader": "7.1.2",
    "babel-plugin-transform-class-properties": "6.24.1",
    "babel-plugin-transform-decorators-legacy": "1.3.4",
    "babel-preset-env": "1.6.1",
    "babel-preset-es2015": "6.24.1",
    "babel-preset-react": "6.24.1",
    "extract-text-webpack-plugin": "3.0.2",
    "mobx": "3.3.1",
    "mobx-react": "4.3.3",
    "node-sass": "4.5.3",
    "react": "16.0.0",
    "react-dom": "16.0.0",
    "react-interpolate-component": "0.11.0",
    "react-router-dom": "4.2.2",
    "react-test-renderer": "16.0.0",
    "react-translate-component": "0.14.0",
    "webpack": "3.8.1"
  },
  "jest": {
    "testPathIgnorePatterns": [
      "./__tests__/test-setup.js"
    ],
    "setupTestFrameworkScriptFile": "./__tests__/test-setup.js",
    "transform": {
      "^.+\\.jsx?$": "babel-jest"
    }
  }
}

这是组件即时测试:

import React, { Component } from 'react'
import { observer } from 'mobx-react'
import Locale from '../stores/view/language'

@observer
export default class DateFormat extends Component {
  constructor(props) {
    super(props)
    this.state =
    {
      fi: { lang: 'fi-FI', options: { year: 'numeric', month: 'numeric', day: 'numeric' } },
      en: { lang: 'en-US', options: { year: 'numeric', month: 'long', day: 'numeric' } },
    }
  }

  render() {
    return (
      <span>
        {new Date(this.props.date).toLocaleDateString(
          this.state[Locale.currentLang].lang,
          this.state[Locale.currentLang].options,
        )}
      </span>
    );
  }
}

无法确定如何修复observable。 我错过了什么吗?或者我应该用其他方式测试一下吗?

我没有.babelrc文件。

1 个答案:

答案 0 :(得分:1)

我认为您需要添加.babelrc文件。听起来你错过了用于babel的es2016插件,它将ES7代码(如装饰器)转换为ES5。

https://babeljs.io/docs/plugins/preset-es2016/