TypeScript编译失败和Karma测试执行?

时间:2016-03-07 09:49:17

标签: typescript jasmine karma-runner webpack

我目前正在使用Karma + Jasmine在我的基于TypeScript的项目上运行测试,而我想要打破这些测试'当Karma监视模式下TypeScript编译失败时。

我使用标准的Karma配置并使用webpack预处理器(编译TS文件)编译TS。一切都运行得很好,除了在发生编译错误时看到所有测试都是非常误导的(即使webpack编译失败,业力还会重新运行以前的测试)。

这看起来相当微不足道,但经过一两个小时的查看文档和搜索Google之后,我一直在拼命寻找一个我找不到的解决方案。

是否存在涉及karma,jasmine,webpack和TypeScript的解决方案,如果在不中断监视模式的情况下发生编译错误,可能会破坏测试?

编辑:添加了手表模式的精确度。

2 个答案:

答案 0 :(得分:2)

个人而言,我没有在单个工作流程中使用karma和webpack。但请记住,在对包括打字稿在内的一起使用它们进行一些研究时,可能会遇到问题https://github.com/webpack/karma-webpack/issues/49https://github.com/webpack/webpack/issues/708。因此,如上所述,bail选项未按预期工作,您可以尝试使用提到的插件,该插件将在TS错误上失败(引用来自this comment to issue #708的建议)。

更新:对于watch案例,我会考虑更改,以防止webpack失败但同时引发错误并阻止业力执行测试(基于这suggestion)。

module.exports = function (config) {

  config.set({

    browsers: [ 'Chrome' ],
    frameworks: [ 'mocha' ],
    reporters: [ 'mocha' ],

    files: [
      // ...
    ],

    // ...
    webpack: {
      plugins: [
          function()
          {
              this.plugin("done", function(stats)
              {
                  // Log each of the errors
                  stats.compilation.errors.forEach(function (error) {
                      console.log(error.message || error);
                  });

                  // Pretend no assets were generated. This prevents the tests
                  // from running making it clear that there were errors.
                  stats.stats = [{
                      toJson: function () {
                          return this;
                      },
                      assets: []
                  }];                      
              });
          }
      ]
    }
  })

}

我刚刚检查过将上面的插件添加到相当简单的项目https://github.com/itajaja/tslib-webpack-starter中,并且测试对于任何TS编译错误都失败了。

答案 1 :(得分:0)

我遇到tslint警告的问题。我得到的只是Compilation failed due to tslint errors。在webpack.config.js中添加此设置可以解决此问题:

bail: true,

这与所有常规webpack.config.js设置一起使用。 E.g:

{
    context: __dirname + "/app",
    entry: "./entry",
    output: {
        path: __dirname + "/dist",
        filename: "bundle.js"
    },
    bail: true
}

现在我至少在失败之前看到第一个tslint错误。

相关问题