Skip to content

Instantly share code, notes, and snippets.

@jonesmac
Created December 22, 2018 00:47
Show Gist options
  • Save jonesmac/9ef456153c714db56be0ec24b61c6fbb to your computer and use it in GitHub Desktop.
Save jonesmac/9ef456153c714db56be0ec24b61c6fbb to your computer and use it in GitHub Desktop.
/*
* This plugin makes sure karma doesn't run any specs when there's a
* compilation error in a module by checking the finished compilation for warnings
* which would prevent tests from accurately running.
*
* Inspiration for looking at the warnings length
* https://gist.github.com/Stuk/6b574049435df532e905
*/
import { Compiler } from 'webpack';
export class WebpackKarmaWarningsPlugin {
public apply(compiler: Compiler) {
compiler.hooks.emit.tapAsync(
'WebpackKarmaWarningsPlugin',
(compilation, callback) => {
if (compilation.warnings.length) {
throw new Error(compilation.warnings.toString());
}
callback();
}
);
}
}
@SpartanDigitalDotNet
Copy link

Hello!
Great work sir. I am sooo close, but the apply() function is giving me a bit of an issue adding the plugin when npm/webpack build is performed:

  public apply(compiler: Compiler) {
         ^^^^^
SyntaxError: Unexpected identifier
    at new Script (vm.js:80:7)

webpack.config.js:

 ...
 plugins: [
        new webpackKarmaWarningsPlugin()
    ]
...

Here is a slight modification to the .ts:

const Compiler = required('webpack');

/*
 * REF: https://gist.github.com/jonesmac/9ef456153c714db56be0ec24b61c6fbb
 *
 * This plugin makes sure karma doesn't run any specs when there's a
 * compilation error in a module by checking the finished compilation for warnings
 * which would prevent tests from accurately running.
 *
 * Inspiration for looking at the warnings length
 * https://gist.github.com/Stuk/6b574049435df532e905
 */

module.exports = class WebpackKarmaWarningsPlugin {
  function apply(compiler: Compiler) {
    compiler.hooks.emit.tapAsync(
      'WebpackKarmaWarningsPlugin',
      (compilation, callback) => {
        if (compilation.warnings.length) {
          throw new Error(compilation.warnings.toString());
        }
        callback();
      }
    );
  }
}

Please advise.

@TranquilMarmot
Copy link

This no longer works with Webpack 5 😭

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment