在Transpiling上输入错误Babel class.default不是构造函数

时间:2016-10-21 15:07:23

标签: node.js ecmascript-6 mocha babel

'use strict';
/**
* PortalHTML.js
*/

import SingleContextHTML from './SingleContextHTML';
import MultiContextHTML from './MultiContextHTML';

export default class PortalHTML{
    constructor (type) {
          switch(type) {
        case "1":
          this.portalStrategy = new SingleContextHTML();
          break;
        default:
          this.portalStrategy = new MultiContextHTML();
          break;
           }
    }

    render (...args) {
        this.portalStrategy.renderHTML(args);
    }

    _strategyPeak(){
        return this.portalStrategy.constructor.name;
    }
}

/**
* SingleContextHTML.js
*/
'use strict';

import PortalHTMLStrategy from './PortalHTMLStrategy';

export default class SingleContextHTML extends PortalHTMLStrategy {
    constructor(){
            super()
    }
    renderHTML(args){}
}

/**
* Multi.js (same as single) ^^ above
*/

/**
* PortalHTMLStrategy.js
*/
'use strict';

export default class PortalHTMLStrategy{
    constructor(){}

    renderHTML(args){}
}

/**
* Tester Mocha and Chai
*/

'use strict';

import PortalHTML from "./PortalHTML";
import chai from 'chai';

let basicPortalHTMLTest = () => {
    let singleContextHTML = new PortalHTML("1");
    let multiContextHTML = new PortalHTML("multi");
    describe('PortalHTML: Initialization of PortalHTML', () => {
        it('Should be an instance of class SingleContextHTML', (done) => {
            chai.expect(singleContextHTML._strategyPeak()).to.equal('SingleContextHTML');
            done();
        });

        it('Should be an instance of MultiContextHTML', (done) => {
            chai.expect(singleContextHTML._strategyPeak()).to.equal('MultiContextHTML');
            done();
        });
    });
};

basicPortalHTMLTest();

我正在尝试测试策略模式的实现,但是在运行带有mocha的测试脚本后,我遇到了一个错误,指出了以下内容。

var singleContextHTML = new _PortalHTML2.default("1");
                                      ^                               
TypeError: _PortalHTML2.default is not a constructor

我正在使用node6和以下babel包:

    "babel-cli": "^6.14.0",
    "babel-core": "^6.17.0",
    "babel-loader": "^6.2.4",
    "babel-preset-es2015": "^6.16.0",
    "babel-preset-react": "^6.11.1",
    "babel-preset-stage-2": "^6.11.0"

我一直试图调试这个约半天。如果有人发现我正在做的任何错误,我会非常非常感激。

1 个答案:

答案 0 :(得分:0)

我使用了错误的babel预设。切换到es2016或更高版本可修复此错误。