TypeScript编译失败和Karma测试执行? [英] TypeScript compilation failure and Karma test execution?
问题描述
我目前正在使用Karma + Jasmine在我的基于TypeScript的项目上运行测试,并且我希望在Karma监视模式下TypeScript编译失败时打破测试。
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.
我正在使用标准的Karma配置并使用webpack预处理器(编译TS文件)编译TS。一切都运行得很好,除了看到所有测试在发生编译错误时通过是非常误导的(即使webpack编译失败,业力还会重新运行以前的测试)。
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).
这看起来相当微不足道,但是经过一两个小时的查看文档和搜索Google后,我正在拼命寻找一个我找不到的解决方案。
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.
是否存在涉及karma,jasmine,webpack和TypeScript的解决方案,在发生编译错误时可以在不破坏监视模式的情况下中断测试?
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.
推荐答案
我个人在单个工作流程中没有使用karma和webpack。但请记住,通过一些研究将它们一起使用,包括打字稿,并且存在一些问题 https:// github .com / webpack / karma-webpack / issues / 49 和 https:// github。 com / webpack / webpack / issues / 708 您可能面临的问题。所以如上所述 bail
选项没有按预期工作,你可以尝试使用提到的插件,这会在TS错误上失败(引用来自此评论发布#708 。
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).
更新:对于监视
的情况,我会考虑一个阻止webpack失败但同时引发错误并阻止业力执行测试的更改好(基于此建议)。
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: []
}];
});
}
]
}
})
}
我刚刚检查了将上面的插件添加到相当简单的项目 https: //github.com/itajaja/tslib-webpack-starter 并且任何TS编译错误的测试都失败。
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.
这篇关于TypeScript编译失败和Karma测试执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!