在业力中使用ES6时出错

问题描述 投票:0回答:1

我使用业力进行了配置,以运行我的测试用例。 如果在测试文件或源文件中使用任何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") }); }); 

如果我在help.js中使用任何ES6代码,则也会引发错误。 如何转换我的源代码以及如何使用上述配置进行测试。

javascript mocha karma-runner karma-webpack
1个回答
0
投票

您正在让Karma在PhantomJS中运行测试。 PhantomJS不支持ES6的大部分功能,并且可能永远不会支持,因为今年年初无限期暂停了开发。

如果你想使用ES6(喜欢的东西let在幻影和箭头的功能),你需要的东西,如以transpile代码巴贝尔 。 我相信,已经有一些Karma插件,例如这个

© www.soinside.com 2019 - 2024. All rights reserved.