【问题标题】:Babel not working with Mocha and starting Node serverBabel 无法使用 Mocha 并启动 Node 服务器
【发布时间】:2015-07-12 07:32:17
【问题描述】:

我在sails 和mocha 上运行sailsjs、mocha 和babel。当我运行时,我的 before 函数在运行测试之前启动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

对于我的生活,我无法弄清楚如何让 mocha running babel 和sails running babel 同时工作。

我的 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();
  });
});

【问题讨论】:

    标签: node.js unit-testing sails.js mocha.js


    【解决方案1】:

    我使用sails-hook-babel,它就像一个魅力。在这里做:

    1. 安装npm install sails-hook-babel --save-dev
    2. 在函数加载 babel 之前编辑您的bootstrap.js/,即

      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);
      });
      
    3. 现在您可以在测试中使用 ES6。

    这是参考:

    1. GitHub 的 Babel 问题
    2. My Blog,对不起,它是用印度尼西亚语写的,如果你愿意,请使用谷歌翻译。

    【讨论】:

      猜你喜欢
      • 2018-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 2019-03-28
      • 1970-01-01
      • 2019-07-20
      相关资源
      最近更新 更多