2016-11-08 10 views
0

私はES6列車で最終的にホッピングしています。コンパイルのためにES6とBabelを使って小さなNode.jsアプリケーションを作成しました。私はMochaを使ってテストを書いています。私が読んだ限り、あなたはまだES6を使うべきではありません。ES5コードでのES6クラスの使用

私が作ったオブジェクトクラスのいくつかの関数をテストしようとしています。だから、モカに私がやっている次:

はっきりためのラインの仕事ではないでしょう
var assert = require('assert'); 
var Icon = require('../lib/icon'); 

describe('Icons', function() { 
    describe('#save()', function() { 
    it('should return a success message & save the icon', function() { 
     var icon = new Icon('https://cdn4.iconfinder.com/data/icons/social-media-2070/140/_whatsapp-128.png', 'icon-test'); 
     var result = Icon.save(); 

     if(result !== '_whatsapp-128.png saved successfully.') return false; 

     return fs.existsSync('icon-test/_whatsapp-128.png'); 
    }); 
    }); 
}); 

:私はES6オブジェクトをインスタンス化することができることはどのように非常にわからない

var icon = new Icon('https://cdn4.iconfinder.com/data/icons/social-media-2070/140/_whatsapp-128.png', 'icon-test'); 

ES5を使用して機能をテストします。どんな助けでも大歓迎です。

** EDIT追加 - アイコンファイル**

import fs from 'fs'; 
import https from 'https'; 
import path from 'path'; 

class Icon { 
    constructor(source, destination) { 
     this.source = source; 
     this.destination = path.resolve(destination); 
    } 

    save() { 
     console.log(this.source); 
     // Fetching the icon. 
     let request = https.get(this.source, (response) => { 

      // Splitting the file information. 
      let fileInfo = path.parse(this.source); 

      // Creating the directory, if it does not already exist. 
      if(!fs.existsSync(this.destination)) { 
       fs.mkdirSync(this.destination); 
       console.log('Destination directory created.\n'); 
      } 

      // Piping the icon data & saving onto disk. 
      let iconFile = fs.createWriteStream(this.destination + '/' + fileInfo.base); 
      response.pipe(iconFile); 
      return `${fileInfo.base} saved successfully.`; 
     }); 
    } 
} 

export default Icon; 
+4

"これは明らかに行のために動作しません"どうしてですか?オブジェクトはES5に存在しますが、新しい 'class'構文だけです。 – Ajedi32

+2

'ES6'と 'ES5'に反対するのは意味がありません。それはJSです。問題の内容を詳細に記入してください。エラーメッセージと '../lib/ icon'のリストが役立ちます。 ES6モジュールから 'var Icon = require( '../lib/ icon')'としてクラスを正常にインポートすることはできません。 – estus

+0

エラーメッセージ:「型エラー:アイコンがコンストラクタではありません」 – nickcorin

答えて

0

../lib/iconは、デフォルトのエクスポートを持つES6モジュールです。

require('../lib/icon')は、ES6モジュールオブジェクトを返します。デフォルトのエクスポートを要求するには、それはする必要があります

var Icon = require('../lib/icon').default; 
+0

これは完璧に機能しました、ありがとう! – nickcorin

+0

ようこそ。 – estus

-1

使用ES6-シムまたはES5-シム(ここhttps://github.com/paulmillr/es6-shimを参照)、それはECMAスクリプト5での作業を取得することで作業をdoesn't場合は、他のライブラリも含めて、ポリフィルとその他すべてのものについてhttps://github.com/es-shimsが見つかるかもしれないので、従来のサポートを得ることができます。

希望に役立ちます。

関連する問題