且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-09-15 23:25:04

我个人在单个工作流程中没有使用karma和webpack。但请记住,通过一些研究将它们一起使用,包括打字稿,并且存在一些问题 https:// github .com / webpack / karma-webpack / issues / 49 https:// github。 com / webpack / webpack / issues / 708 您可能面临的问题。所以如上所述 bail 选项没有按预期工作,你可以尝试使用提到的插件,这会在TS错误上失败(引用来自此评论发布#708

personally I am not using karma along with webpack in a single workflow. But remember from doing some research on using them together including typescript and there are issues https://github.com/webpack/karma-webpack/issues/49 and https://github.com/webpack/webpack/issues/708 wihich you might be facing. So as mentioned bail option is not working as expected you can try using a plugin mentioned which will fail on TS error (quoting suggestion from this comment to issue #708).

更新:对于监视的情况,我会考虑一个阻止webpack失败但同时引发错误并阻止业力执行测试的更改好(基于此建议)。

UPDATE: As for the watch case I would consider a change that prevents webpack from failing but at the same time raising the error and prevent karma from executing tests as well (based on this 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编译错误的测试都失败。

I've just checked adding above plugin to fairly simple project https://github.com/itajaja/tslib-webpack-starter and tests are failing for any TS compilation errors.