Last active
August 27, 2019 10:21
-
-
Save mvgijssel/d3b121ad50e34c09a124 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This plugin makes sure karma doesn't run any specs when there's a | |
* compilation error in a module by including a module even if the module has errors. | |
* | |
* Webpack's normal behaviour is to not include modules which have errors, | |
* which causes Karma to run all the tests without the failed spec. | |
* | |
* Some links to where this magic actually happens in Webpack: | |
* | |
* The module will be removed from the dependency: | |
* https://github.com/webpack/webpack/blob/v1.12.14/lib/Compilation.js#L646-L649 | |
* | |
* and dependencies without a module won't be included in the module map: | |
* https://github.com/webpack/webpack/blob/v1.12.14/lib/ContextModule.js#L87-L89 | |
* | |
* Setting the module._source to a javascript error is copied from: | |
* https://github.com/webpack/webpack/blob/v1.12.14/lib/NormalModule.js#L127 | |
*/ | |
var WebpackKarmaWarningsPlugin = function() {}; | |
var RawSource = require("webpack-core/lib/RawSource"); | |
WebpackKarmaWarningsPlugin.prototype.apply = function(compiler) { | |
compiler.plugin("compilation", function(compilation) { | |
compilation.plugin("failed-module", function(module) { | |
var moduleErrorMessage = module.error.error.toString() | |
module._source = new RawSource(`throw new Error(${JSON.stringify(moduleErrorMessage)});`); | |
module.error = null; | |
}); | |
}); | |
}; | |
module.exports = WebpackKarmaWarningsPlugin; |
Any chance it could work with webpack@3 ?
I get this error using [email protected]
:
20 04 2018 06:56:55.348:ERROR [config]: Invalid config file!
Error: Cannot find module 'webpack-core/lib/RawSource'
@aszmyd Use this: const RawSource = require("webpack-sources").RawSource;
for those looking for a webpack 4 approach in typescript - https://gist.github.com/jonesmac/9ef456153c714db56be0ec24b61c6fbb
I found I had to use the compilation.warnings.length
to test if a relevant error occurred. I tried the failedModule
hook but with no success.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So sorry for not replying, for some reason I'm not getting gist notifications 🙈. Not sure if it helps anyone, but we require karma specs as follows using a context require:
The webpack plugins for running karma
Some of the versions:
I'll publish this on npm if people are still using this!
No clue :/. Do you use the same versions / require strategy?
Do you use the same plugins in the same order? Also do you include the
NoErrorsPlugin
? Maybe that's preventing stuff from firing?