我在帆和摩卡上运行sailsjs,mocha和babel。当我在运行测试之前运行之前的函数来启动sails应用程序时,我得到了这个:
> PORT=9999 NODE_ENV=test mocha --recursive --compilers js:babel/register
lifting sails
1) "before all" hook
0 passing (757ms)
1 failing
1) "before all" hook:
Uncaught Error: only one instance of babel/polyfill is allowed
对于我的生活,我无法弄清楚如何让摩卡运行巴贝尔和帆运行巴贝尔同时工作。
我的before()代码如下所示:
import Sails from 'sails'
// Global before hook
before(function (done) {
console.log('lifting sails')
// Lift Sails with test database
Sails.lift({
log: {
level: 'error'
},
models: {
connection: 'testMongoServer',
migrate: 'drop'
},
hooks: {
// sails-hook-babel: false
babel: false
}
}, function(err) {
if (err) {
return done(err);
}
// Anything else you need to set up
// ...
console.log('successfully lifted sails')
done();
});
});
答案 0 :(得分:1)
我使用sails-hook-babel,它就像一个魅力。在这做:
npm install sails-hook-babel --save-dev
编辑你的bootstrap.js
/之前的函数以加载babel,即
var Sails = require('sails'),
sails;
var options = {
loose : "all",
stage : 2,
ignore : null,
only : null,
extensions: null
};
global.babel = require("sails-hook-babel/node_modules/babel/register")(options);
before(function (done) {
Sails.lift({
//put your test only config here
}, function (err, server) {
sails = server;
if (err) return done(err);
// here you can load fixtures, etc.
done(err, sails);
});
});
after(function (done) {
// here you can clear fixtures, etc.
sails.lower(done);
});
现在,您可以在测试中使用ES6。
以下是参考资料: