在业力中使用ES6时出错

时间:2018-06-07 09:25:43

标签: javascript mocha karma-runner karma-webpack

我使用业力进行配置来运行我的测试用例。如果在测试文件或源文件中使用任何ES6代码,我会收到错误。下面我附上我的配置。请帮帮我。



module.exports = function (config) {
  config.set({
   
    browsers: ['PhantomJS'],
    colors: true,
    client: {
      clearContext: false
    },
    failOnEmptyTestSuite: false,
    frameworks: [
      'mocha',
      'chai'
    ],
    files: [
      'tests/test.js',
      //{pattern: 'tests/globals.js'},
      'js/**/*.js'
    ],
    preprocessors: {
      'tests/test.js': ['webpack', 'sourcemap'],
      'js/**/*.js': ['webpack'],
      'tests/**/*.js': ['webpack'],
    },
    reporters:['spec', 'coverage'],
    coverageReporter: {
      reporters: [
        { type: 'text' },
        { type: 'html', subdir: 'html' }
      ],
    },
    webpack: {
      cache: true,
      devtool: 'inline-source-map',
      module: {
        loaders: [
          {
            enforce: 'pre',
            test: /.test\.js$/,
            include: /tests/,
            exclude: /node_modules/,
            use: [{ loader: 'babel-loader' }]
          },
          {
            enforce: 'pre',
            test: /\.js$/,
            include: /js/,
            exclude: /node_modules/,
            use: [{ loader: 'istanbul-instrumenter-loader', query: { esModules: true } }]
          },

          {
            test: /\.js$/,
            include: /js/,
            exclude: /node_modules|tests/,
            use: [{ loader: 'babel-loader' }]
          },
        ],
      },
    },
  });
};




我的测试用例文件是



import '../js/help/help.js';
describe("CamelCase Function",()=>{
	it("the given text should be a string", () =>{
        var str  = "satya";
			testString = Utility.camelCaseConvertion(str);
		expect(testString).to.be.a("string");		
    });
    
    it("Should convert the first letters to Capital",()=>{
        expect(Utility.camelCaseConvertion("test 123test test@123 anywhereworks any")).to.equal("Test 123test Test@123 Anywhereworks Any");	
    });

    it("should not convet to camel case",()=>{
        expect(Utility.camelCaseConvertion("@anywhereworks")).to.equal("@anywhereworks");
    });
    it("should convert to camel case after space",()=>{
        expect(Utility.camelCaseConvertion("<h1>aw anywhere")).to.equal("<h1>aw Anywhere")
    });
});
&#13;
&#13;
&#13;

如果我在help.js中使用任何ES6代码,那么它也会抛出错误。我如何能够转换我的源代码以及使用上述配置进行测试。

1 个答案:

答案 0 :(得分:0)

你正在让Karma在PhantomJS中运行你的测试。 PhantomJS不支持ES6的大部分内容,并且可能永远不会支持,因为今年早些时候开发无限期suspended

如果你想在Phantom中使用ES6(像let和箭头函数这样的东西),你需要用Babel之类的东西来转换你的代码。我相信已经存在一些Karma插件,例如this one