使用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