I'm finally hopping on board with the ES6 train. I've written a small Node.js application using ES6 and Babel for compilation. I'm writing the tests using Mocha, which as far as I have read, you shouldn't use ES6 with just yet.
I'm trying to test some functions of an object class I've made. So in Mocha I'm doing the following:
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');
});
});
});
Which clearly won't work because of the line:
var icon = new Icon('https://cdn4.iconfinder.com/data/icons/social-media-2070/140/_whatsapp-128.png', 'icon-test');
I'm not quite sure how to be able to instantiate the ES6 object using ES5 and then test the function. Any help would be greatly appreciated.
** EDIT - ADDED ICON FILE **
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;