karma-webpack: Build not getting cancelled when webpack fails to compile the file

I am not really sure if this is an error with our configuration, karma, karma-webpack or webpack šŸ˜„

We are compiling cjsx (coffee and jsx) files using webpack and then run our tests using jasmine. Everything is working fine as long as you do not have a syntax error in your coffee file. At that point the tests just run using an outdated version of our code, which often means they just stay green 😃. With webpack being quieted this does not result in any output and even if you output the error it can easily be overlooked because of the test run afterwards (which scrolls the error out of my terminal).

I would expect the tests to not be run and an error output to the displayed in karma.

Is this an issue that should be fixed here in the plugin or should this work out of the box and I am just too stupid to configure karma-webpack.

I already tried implementing this using failed callback of webpack. Is this the way to go here? I will try some more after work.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 23 (1 by maintainers)

Most upvoted comments

@d3viant0ne I don’t think this issue should be closed or is resolved. These plugins are workarounds. Tests should not run/pass if the code doesn’t build. If your using karma start it will fail to build, karma’s connection to the browser will timeout, and karma just sits there not exiting and not running tests.

I’ve created another plugin which also prevents Karma from hanging https://gist.github.com/mvgijssel/d3b121ad50e34c09a124

Morgan, Seems like the bail option in the webpack config is not being honored properly according to this report https://github.com/webpack/karma-webpack/issues/66.

On Thu, Dec 3, 2015 at 12:25 AM, Morgan Roderick notifications@github.com wrote:

@cedrics https://github.com/cedrics thanks

— Reply to this email directly or view it on GitHub https://github.com/webpack/karma-webpack/issues/49#issuecomment-161548933 .

Thanks for the insight into the optional modules and warnings, @codazzo. Based on that I wrote a mini webpack plugin that shows all warnings and then prevents the tests from running by pretending no assets were generated.

It’s available at https://gist.github.com/Stuk/6b574049435df532e905 and can be simply added to your karma webpack config plugins.

Hope this helps people!

You may be able to replace the stats.stats = line with throw new Error(...) to get the process to exit with a failure.

I use this in watch mode, hence the waiting.

On January 29, 2016 8:51:46 AM PST, Petar Paar notifications@github.com wrote:

@Stuk, with your plugin tests are not run but instead process with test run hangs (after showing warnings and before running tests). Is it possible to fail the test run instead?


Reply to this email directly or view it on GitHub: https://github.com/webpack/karma-webpack/issues/49#issuecomment-176857577

I’ve done some investigation on this and it appears that at least for me the chain of cause-and-effect is:

Importing single test files with require() seems to fix the issue. I have yet to assess why require.context marks modules as ā€œoptionalā€ and in that case whether it should be discouraged in use with karma-webpack.

I got it to work by adding this to my karma.conf.js file:

buildWebpack: {
  failureCb: () => {
    setTimeout(() => {
      process.exit(1);
    });
  },
},

setTimeout allowed the error logs to be displayed before exiting.

For those looking for a webpack v4 solution - https://gist.github.com/jonesmac/9ef456153c714db56be0ec24b61c6fbb

Seems to work for my use case but be interested to know if it works for others.