设置环境变量启动mocha

使用mocha测试含有es6 modules的代码是,须要使用babel-register来转化语法。babel-register跟项目中web端的项目共享同一份.babelrc。以下:vue

{
  "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 中:git

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

这里要注意的是powershell中的管理命令链接符是 | ,而不是&&es6

在mocha的issue中,有一个是但愿加入--env标志,但tj大神直接说不须要, 你干吗不set xxx_env && mocha.github

底下也有人给了另外的解决办法web

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 60000shell

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

相关文章
相关标签/搜索