我終於在ES6列車上跳來跳去。我編寫了一個使用ES6和Babel進行編譯的小型Node.js應用程序。我正在使用Mocha編寫測試,據我所知,你不應該只使用ES6。使用ES6類和ES5代碼
我想測試我所做的一個對象類的一些功能。因此,在摩卡我做了以下內容:
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');
});
});
});
其中明確不會因爲該行的工作:
var icon = new Icon('https://cdn4.iconfinder.com/data/icons/social-media-2070/140/_whatsapp-128.png', 'icon-test');
我不太清楚如何能夠實例化對象ES6使用ES5然後測試功能。任何幫助將不勝感激。
**編輯 - 添加圖標文件**
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;
「顯然哪個行不通,因爲行」爲什麼不行?對象存在於ES5中,它只是新的'class'語法。 – Ajedi32
反對'ES6'到'ES5'是沒有意義的。這是JS。請提供有關問題的詳細信息。錯誤消息和'../ lib/icon'列表將有所幫助。從ES6模塊中,類不可能成功導入爲'var Icon = require('../ lib/icon')'。 – estus
錯誤消息:'類型錯誤:圖標不是構造函數' – nickcorin