Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build not getting cancelled when webpack fails to compile the file #49

Closed
cedrics opened this issue May 18, 2015 · 23 comments
Closed

Build not getting cancelled when webpack fails to compile the file #49

cedrics opened this issue May 18, 2015 · 23 comments

Comments

@cedrics
Copy link

cedrics commented May 18, 2015

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.

@marr
Copy link

marr commented May 31, 2015

have you tried the bail option in your webpack config? Also you can specify it in the cli: webpack --bail

@cedrics
Copy link
Author

cedrics commented Jun 3, 2015

I did some more research yesterday (I forgot to do it initially) and as far as I can tell this is not really an issue of karma-webpack. We are using:

var testsContext = require.context(".", true, /_test$/);
testsContext.keys().forEach(testsContext);

To require all our specs and are using the coffee loader to precompile them. If the compilation of a spec fails the file is just ignored and the spec is therefore not run. When I require the file with a syntax error by hand (using require) the build gets cancelled. The bail option does not affect this behaviour as far as I can tell.

@yoni-tock
Copy link

I'm also having this issue. I guess one way to handle it would be to just specify all the tests individually...

@mroderick
Copy link

@cedrics did you find an elegant solution for this issue?

@cedrics
Copy link
Author

cedrics commented Dec 2, 2015

Hi @mroderick, sadly I didn't. We are currently not working on the project using this setup and therefore did not run into the problem again and so I didn't spend anymore time on it / forgot all about it.

@mroderick
Copy link

@cedrics thanks

@marr
Copy link

marr commented Dec 3, 2015

Morgan,
Seems like the bail option in the webpack config is not being honored
properly according to this report
#66.

On Thu, Dec 3, 2015 at 12:25 AM, Morgan Roderick [email protected]
wrote:

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


Reply to this email directly or view it on GitHub
#49 (comment)
.

@codazzo
Copy link

codazzo commented Dec 4, 2015

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.

@clouddra
Copy link

clouddra commented Dec 9, 2015

+1. Also encountering the same issue.

@Stuk
Copy link

Stuk commented Jan 13, 2016

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!

@pepaar
Copy link

pepaar commented Jan 29, 2016

@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?

@Stuk
Copy link

Stuk commented Jan 29, 2016

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 [email protected] 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:
#49 (comment)

@pepaar
Copy link

pepaar commented Feb 1, 2016

That worked, thanks!

@mvgijssel
Copy link

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

@joshwiens
Copy link
Contributor

Closing this one, issue has multiple resolutions & the issue with karma-webpack not respecting bail is a known issue already on the hot list for the project team.

@icd2k3
Copy link

icd2k3 commented Aug 24, 2016

@mvgijssel that plugin fixed the issue for me - thanks!

@filipesilva
Copy link

I don't think this is the same as the bail issue. When running karma in watch mode it's troublesome to have karma report passing tests after fatal compilation errors.

I'm using a variation of @Stuk's plugin myself, except I'm clearing the stats on errors instead of warnings. It blocks the compilation until I fix the errors. Only then does it re-run the tests in Karma.

@aldencolerain
Copy link

aldencolerain commented Sep 20, 2017

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

@jonesmac
Copy link

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.

@tscislo
Copy link

tscislo commented Apr 4, 2020

For TypeScript it is sufficient to add to tsconfig.json

 "compilerOptions": {
   ...
    "noEmitOnError": true
  },

@thw0rted
Copy link

thw0rted commented Nov 4, 2020

I'm using noEmitOnError and instead of failing, it just runs the last-built version of the test. Might only work if you're not using the "alternate" (require.context-based) Karma config?

@TranquilMarmot
Copy link

TranquilMarmot commented May 17, 2021

We ended up using this with Webpack 5:

webpack-karma-die-hard-plugin.ts:

import { Compiler } from 'webpack';

/**
 * `karma-webpack` will just hang forever if there are compilation errors.
 * This plugin makes it so that the process actually ends with an error.
 */
export default class WebpackKarmaDieHardPlugin {
  public apply(compiler: Compiler) {
    compiler.hooks.failed.tap('WebpackKarmaDieHardPlugin', (error) => {
      console.error(error);
      process.exit(1);
    });
  }
}

Would it be possible to have karma-webpack detect failures and exit the process? It seems a little silly to have a plugin just to do this.

@marleypowell
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests