2017-09-29 9 views
1

ES6、Windows 10 x64、Node.js 8.6.0、Mocha 3.5.3モカのテストでES6モジュールを使用することはできますか?

モカのテストでES6モジュールを使用することはできますか?私はexportimportキーワードに問題があります。

/* eventEmitter.js 
*/ 

/* Event emitter. */ 
export default class EventEmitter{ 

    constructor(){ 

     const subscriptions = new Map(); 

     Object.defineProperty(this, 'subscriptions', { 
      enumerable: false, 
      configurable: false, 
      get: function(){ 
       return subscriptions; 
      } 
     }); 
    } 

    /* Add the event listener. 
    * @eventName - the event name. 
    * @listener - the listener. 
    */ 
    addListener(eventName, listener){ 
     if(!eventName || !listener) return false; 
     else{ 
      if(this.subscriptions.has(eventName)){ 
       const arr = this.subscriptions.get(eventName); 
       arr.push(listener); 
      } 
      else{ 
       const arr = [listener]; 
       this.subscriptions.set(eventName, arr); 
      } 
      return true; 
     } 
    } 

    /* Delete the event listener. 
    * @eventName - the event name. 
    * @listener - the listener. 
    */ 
    deleteListener(eventName, listener){ 
     if(!eventName || !listener) return false; 
     else{ 
      if(this.subscriptions.has(eventName)){ 
       const arr = this.subscriptions.get(eventName); 
       let index = arr.indexOf(listener); 

       if(index >= 0){ 
        arr.splice(index, 1); 
        return true; 
       } 
       else{ 
        return false; 
       } 
      } 
      else{ 
       return false; 
      } 
     } 
    } 

    /* Emit the event. 
    * @eventName - the event name. 
    * @info - the event argument. 
    */ 
    emit(eventName, info){ 
     if(!eventName || !this.subscriptions.has(eventName)) { 
      return false; 
     } 
     else{ 
      for(let fn of this.subscriptions.get(eventName)){ 
       if(fn) fn(info); 
      } 
      return true; 
     } 
    } 
} 

モカ試験:

/* test.js 
* Mocha tests. 
*/ 
import EventEmitter from '../../src/js/eventEmitter.js'; 

const assert = require('assert'); 

describe('EventEmitter', function() { 
    describe('#constructor()', function() { 
    it('should work.', function() { 
     const em = new EventEmitter(); 
     assert.equal(true, Boolean(em)); 
    }); 
    }); 
}); 

私はPowerShellコンソールから直接mochaを起動します。結果:

enter image description here

+1

https://mochajs.org/#about-babel – robertklep

+0

http://jamesknelson.com/testing-in-es6-でそれが可能ですwith-mocha-and-babel-6/ – winseybash

+1

私は、ノード8.6が、蒸散を伴わないインポート/エクスポートをサポートしているというのがポイントだと思います。しかし、私はmochaがそれを使用するのに必要な '--experimental-modules'を許しているとは思っていないので、それができるまで(またはサポートが安定するまで)蒸散がおそらく必要です。 – CodingIntrigue

答えて

関連する問題