設置環境變量啓動mocha

使用mocha測試含有es6 modules的代碼是,需要使用babel-register來轉化語法。babel-register跟項目中web端的項目共享同一份.babelrc。如下:

{
  "presets": [
    [
      "env",
      {
        "modules": false,
        "targets": {
          "browsers": [
            "> 1%",
            "last 2 versions",
            "not ie <= 8"
          ]
        }
      }
    ],
    "stage-2"
  ],
  "plugins": [
    "transform-vue-jsx",
    "transform-runtime"
  ],
  "env": {
    "test" : {
      "presets": ["env", "stage-2"]
    }
  }
}

在windows powershell 中:

set BABEL_ENV=test | mocha --rquire babel-register 

這裏要注意的是powershell中的管理命令連接符是 | ,而不是&&

在mocha的issue中,有一個是希望加入--env標誌,但tj大神直接說不需要, 你幹嘛不set xxx_env && mocha.

底下也有人給了另外的解決辦法

Since this still seems to get a lot of traffic I thought I would throw out 
a relatively simply solution for people like me who wish there was a --env flag.
What I've been doing is add a test/mocha.env.js file in the repo, 
and then add --require test/mocha.env.js to mocha.opts:

// mocha.opts
--require babel-register
--require test/mocha.env.js
--timeout 60000

// mocha.env.js
process.env.NODE_ENV = 'test';


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章