反应摩卡测试不认识ES6

时间:2018-03-24 20:42:52

标签: javascript reactjs unit-testing mocha babeljs

我想在我的反应应用程序上使用mocha + chai创建一些单元测试。 我已阅读教程并看过视频,我已完成以下步骤。

  1. 安装mocha chai --save-dev
  2. npm install --save-dev babel-register
  3. npm install --save babel-core babel-preset-latest babel-preset-stage-3
  4. 我的package.json脚本:

    "scripts": {
        "start": "node scripts/start.js",
        "build": "node scripts/build.js",
        "test": "mocha --compilers js:babel-core/register src/**/*.spec.js"
      },
    

    我的package.json babel:

    "babel": {
      "presets": [
        "react-app",
        "es2015",
        "stage-3",
        "latest",
        "env"
      ]
    },
    

    没有.babelrc文件

    当我执行$ npm run test时,我收到错误

    Error: Using `babel-preset-react-app` requires that you specify `NODE_ENV` or `BABEL_ENV` environment variables. Valid values are "development", "test", and "production". Instead, received: undefined. (While processing preset: "E:\\react\\games\\node_modules\\babel-preset-react-app\\index.js")
        at Object.<anonymous> (E:\react\games\node_modules\babel-preset-react-app\index.js:51:9)
        at Module._compile (module.js:570:32)
        at Module._extensions..js (module.js:579:10)
        at Object.require.extensions.(anonymous function) [as .js] (E:\react\games\node_modules\babel-register\lib\node.js:152:7)
        at Module.load (module.js:487:32)
        at tryModuleLoad (module.js:446:12)
        at Function.Module._load (module.js:438:3)
        at Module.require (module.js:497:17)
    

    我的单元测试test.spec.js

    import {expect } from 'chai';
     import { getGames } from './index'
    
     describe('games', function(){
     it('gets games list from state', () => {
       const state = 
       {games:{
         mygames: [
          {
            gameId:1,
            gameName: 'name',
            gameUrl: 'someurl',
            gameDscription: 'lorem ipsum',
          },
          {
            gameId:2,
            gameName: 'name',
            gameUrl: 'someurl',
            gameDscription: 'lorem ipsum',
          }
        ]}
      }
      const result = getGames(state)
    
      expect(result).to.eql(state.games.mygames)
       })
     })
    

0 个答案:

没有答案