0

我使用 karma 进行配置以运行我的测试用例。如果在测试文件或源文件中使用任何 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 代码,那么它也会引发错误。我如何转换我的源代码以及使用上述配置进行测试。

4

1 回答 1

0

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

如果您想let在 Phantom 中使用 ES6(类似箭头功能的东西),您需要使用类似Babel的东西来转译您的代码。我相信已经有一些 Karma 插件存在,比如这个

于 2018-06-11T15:45:01.840 回答