nickcorin nickcorin - 1 year ago 67
Javascript Question

Using ES6 classes with ES5 code

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('', 'icon-test');
var result =;

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('', '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.


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() {
// 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)) {
console.log('Destination directory created.\n');

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

export default Icon;

Answer Source

../lib/icon is ES6 module that has default export.

require('../lib/icon') returns ES6 module object. To require default export it should be

var Icon = require('../lib/icon').default;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download