设置环境变量启动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';
相关推荐
88364458 2020-06-03
上海彭彭 2020-01-14
87510796 2019-12-01
86201043 2019-10-29
84344790 2019-10-19
不忘初心才能始终 2017-07-10
86201043 2019-07-01
王治 2019-07-01
84344790 2019-07-01
不忘初心才能始终 2019-07-01
84344790 2019-06-29
80520394 2019-06-29
mscinsidious 2019-06-29
88364458 2017-07-10
84344790 2017-06-08
86201043 2019-06-28
80520394 2015-06-28
87510796 2019-06-27
84344790 2019-06-27