TypeScript compilation failure and Karma test execution?

爷,独闯天下 提交于 2019-12-10 13:58:38

问题


I'm currently using Karma + Jasmine to run the tests on my TypeScript-based project, and I want to 'break the tests' when TypeScript compilation fail in karma watch mode.

I'm using standard Karma configuration and compiling TS using webpack preprocessor (which compiles TS files). Everything works perfectly fine, except that seeing all tests pass when a compilation error occurs is highly misleading (karma rerun previous tests even if webpack compilation fails).

It seems rather trivial, but after an hour or two of looking at documentation and searching Google I'm desperately looking for a solution, which I didn't find.

Is there a solution involving karma, jasmine, webpack and TypeScript that can break the tests when a compilation error occurs without breaking the watch mode?

edit : Precision on the watch mode added.


回答1:


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).

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: []
                  }];                      
              });
          }
      ]
    }
  })

}

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.




回答2:


I was having this issue with tslint warnings. All I was getting is Compilation failed due to tslint errors. Adding this setting in webpack.config.js took care of it:

bail: true,

This goes together with all the regular webpack.config.js settings. E.g.:

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

Now I at least see the first tslint error before it fails.



来源:https://stackoverflow.com/questions/35840541/typescript-compilation-failure-and-karma-test-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!