javascript 使用 Karma 将测试输出到浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18741653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:03:02  来源:igfitidea点击:

Output tests to browser with Karma

javascripttestingkarma-runner

提问by BanksySan

I am using Karma to test my project and can see tests passing an failing in the console window, however, how do I get these to show in the browser? The browser only has a green bar (even though a test is failing) with

我正在使用 Karma 来测试我的项目,并且可以在控制台窗口中看到测试通过失败,但是,如何让这些显示在浏览器中?浏览器只有一个绿色条(即使测试失败)

Karma v0.10.2 - connected

Karma v0.10.2 - 已连接

Written in it.

写在里面。

I have tried addong singleRun :falseto the karma.config.js file.

我已经尝试添加singleRun :false到 karma.config.js 文件。

The config file looks like this:

配置文件如下所示:

module.exports = function (config) {
    config.set({
        basePath: '../',

        files: [
            'app/lib/angular/angular.js',
            'app/lib/angular/angular-*.js',
            'test/lib/angular/angular-mocks.js',
            'app/js/**/*.js',
            'test/unit/**/*.js'
        ],

        autoWatch: true,
        singleRun: false,
        frameworks: ['jasmine'],

        browsers: ['Chrome'],

        plugins: [
            'karma-junit-reporter',
            'karma-chrome-launcher',
            'karma-firefox-launcher',
            'karma-jasmine'
        ],

        junitReporter: {
            outputFile: 'test_out/unit.xml',
            suite: 'unit'
        }

    })
}

回答by daddywoodland

matthias-schuetzwrote a plugin that claims to produce html test output.

matthias-schuetz编写了一个插件,声称可以生成 html 测试输出。

https://npmjs.org/package/karma-htmlfile-reporter

https://npmjs.org/package/karma-htmlfile-reporter

Along with the instructions on the plugin page I had to include a reference to the plugin in the Karma config -

除了插件页面上的说明外,我还必须在 Karma 配置中包含对插件的引用 -

plugins: [
'karma-htmlfile-reporter'
]

回答by Dan-Nolan

I wanted to display HTML5 Web Notificationswith Karma so I wrote something quick to get it to work with Karma version 0.11. Might behave slightly different with other versions. I load this script in with the rest of my application scripts, it will store the karma test results and after completion it will determine the success of the test and then reset to the original karma functions so they're not changed when this script gets run again.

我想用 Karma显示HTML5 Web 通知,所以我写了一些快速的东西让它与 Karma 0.11 版一起使用。可能与其他版本的行为略有不同。我将这个脚本与我的其余应用程序脚本一起加载,它将存储 karma 测试结果,完成后它将确定测试是否成功,然后重置为原始 karma 函数,以便在运行此脚本时不会更改它们再次。

// store all my test results
var results = [];
// Wrap the karma result function
var resultFunc = window.__karma__.result;
window.__karma__.result = function(result){
    // run the original function
    resultFunc(result);
    // push each result on my storage array
    results.push(result);
}

// wrap the karma complete function
var completeFunc = window.__karma__.complete;
window.__karma__.complete = function(result){
    // run the original function
    completeFunc(result);
    // determine success
    var success = results.every(function(r){ return r.success });

    if (success) {
        // display a success notification
    }
    else {
        // display a test failure notification
    }

    // reset the result function
    window.__karma__.result = resultFunc;
    // reset the complete function
    window.__karma__.complete = completeFunc;
}

回答by micfan

Accroding to Documention, even not very perfect:

根据Documention,甚至不是很完美:

If 'singleRun' is false, it will set the ci-mode on, so, make it true, and you will see some failed red status on the top bars of browers.

如果 'singleRun' 为 false,它将设置 ci-mode,因此,将其设为 true,您将在浏览器的顶部栏上看到一些失败的红色状态。

回答by Metareven

There is no straight forward way to do this with Karma. The "best" way to solve this problem would be to hunker down and write a html-reporter for karma (which would make a lot of us other Karma users very happy).

使用 Karma 没有直接的方法可以做到这一点。解决这个问题的“最佳”方法是蹲下来为 karma 编写一个 html-reporter(这会让我们其他 Karma 用户非常高兴)。

If this is too much work for you, the second best thing is to use the junit reporter which generates an xml file. You can then post-process the xml file in some way that turns it into a HTML-file which you can then view in your browser

如果这对您来说工作量太大,第二好的方法是使用生成 xml 文件的 junit 报告器。然后,您可以以某种方式对 xml 文件进行后处理,将其转换为 HTML 文件,然后您可以在浏览器中查看该文件