简体   繁体   中英

Getting error while using ES6 in karma

I made configuration with karma to run my test cases. I'm getting error if use any ES6 code in test file or source file. Below I'm attaching my configuratio. please help me.

 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' }] }, ], }, }, }); }; 

And my test case file is

 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") }); }); 

If I use any ES6 code in help.js then also it throwing error. How I can transpile my source as well as test with above configuration.

You're having Karma run your tests in PhantomJS. PhantomJS does not support much of ES6, and possibly never will since development was indefinitely suspended earlier this year.

If you want to use ES6 (things like let and arrow functions) in Phantom, you'll need to transpile your code with something like Babel . There are some Karma plugins that exist already, I believe, such as this one .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM