【问题标题】:Getting error while using ES6 in karma在业力中使用 ES6 时出错
【发布时间】:2018-06-07 09:25:43
【问题描述】:

我使用 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 代码,那么它也会引发错误。如何转换我的源代码并使用上述配置进行测试。

【问题讨论】:

  • 什么错误?....
  • 它显示了一个错误,如“let”未定义和箭头函数未定义

标签: javascript mocha.js karma-runner karma-webpack


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2014-07-12
    • 1970-01-01
    • 2021-10-18
    • 2013-05-09
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多