node.js 如何在控制台中获得更好的测试报告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17289423/
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-09-02 14:53:50  来源:igfitidea点击:

How to get better test reports in the console?

javascriptnode.jskarma-jasminekarma-runner

提问by blockloop

I have a fairly simple karma.config.js file

我有一个相当简单的 karma.config.js 文件

basePath = '../';


files = [
  JASMINE,
  JASMINE_ADAPTER,
  'js/lib/angular*.js',
  'test/lib/angular/angular-mocks.js',
  'js/**/*.js',
  'test/unit/**/*.js'
];

autoWatch = true;
browsers = ['PhantomJS'];


When I run karma start config/karma.conf.js --single-runI'm getting the following output


当我运行时,karma start config/karma.conf.js --single-run我得到以下输出

$ karma start config/karma.conf.js --single-run
[2013-06-24 23:47:08.750] [DEBUG] config - autoWatch set to false, because of singleRun
INFO [karma]: Karma server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9 (Mac)]: Connected on socket id LwMoWxzIbSUuBsvIqB_m
PhantomJS 1.9 (Mac): Executed 6 of 6 SUCCESS (0.073 secs / 0.02 secs)

I've been searching for something to tell me how to get the output of the tests that get logged (e.g. SUCCESS Unit: services myService should behave)

我一直在寻找一些东西来告诉我如何获取记录的测试的输出(例如SUCCESS Unit: services myService should behave

The only way I can see the output of the tests is to open Chrome and click 'Debug', then show the developer tools console. I want the messages to be logged out to the terminal, but I cannot figure out how to get this working.

我可以看到测试输出的唯一方法是打开 Chrome 并单击“调试”,然后显示开发人员工具控制台。我希望将消息注销到终端,但我不知道如何使其工作。

回答by blockloop

Fixed by installing the karma-spec-reporter

通过安装karma-spec-reporter修复

npm install karma-spec-reporter --save-dev

npm install karma-spec-reporter --save-dev

and adding this my karma.config.js

并添加这个我的 karma.config.js

reporters: ['spec'],

reporters: ['spec'],

According to karma documentation

根据业力文件

By default, Karma loads all NPM modules that are siblings to it and their name matches karma-*.

默认情况下,Karma 加载所有与其同级且名称与 karma-* 匹配的 NPM 模块。

but some users have had to add the following to their config

但有些用户不得不将以下内容添加到他们的配置中

plugins: ['karma-spec-reporter']

plugins: ['karma-spec-reporter']

回答by Fernando Ghisi

Just another detail - if you keep the default reporter 'progress' in the karma.config.js, like below:

另一个细节 - 如果您在karma.config.js 中保留默认报告器“进度” ,如下所示:

reporters: ["progress", "spec"]

or another console reporter, the "spec" reporter output won't work.

或其他控制台报告器,“规范”报告器输出将不起作用。

You should keep only the "spec" one, or the "spec" with other browser reporters. For example:

您应该只保留“规范”一个,或者与其他浏览器报告者一起保留“规范”。例如:

reporters: ["spec", "coverage"]

回答by billyjov

You can also use the Karma-mocha-reporteras reporter and you should have a clean report in your console.

您还可以使用Karma-mocha-reporter作为报告器,并且您的控制台中应该有一个干净的报告。

npm i karma-mocha-reporter --save-dev

// karma.conf.js
module.exports = function(config) {
  config.set({
    frameworks: ['jasmine'],

    // reporters configuration
    reporters: ['mocha']
  });
};

Sometimes, e.g in @angular/clienvironment you should require this like:

有时,例如在@angular/cli环境中,您应该像这样要求:

plugins: [
   ...
   require('karma-mocha-reporter'),
   ...
]

回答by gvlax

Here is my working (draft) configuration without the section 'plugins' (actually I don't fully understand why I should need to specify them ...):

这是我的工作(草案)配置,没有“插件”部分(实际上我不完全理解为什么我需要指定它们......):

package.json

包.json

  "devDependencies": {
    [...]
    "grunt-karma": "~0.9.0",
    "karma": "~0.12.24",
    "karma-jasmine": "~0.2.3",
    "karma-chrome-launcher": "~0.1.5",
    "karma-phantomjs-launcher": "~0.1.4",
    "karma-spec-reporter": "0.0.13"
  }

karma.conf.js

业力配置文件

module.exports = function (config) {
    config.set({
        frameworks: ['jasmine'],
        reporters: ['spec'],
        browsers: ['PhantomJS']
    });
};

Gruntfile.js

Gruntfile.js

    karma: {
        options: {
            configFile: 'karma.conf.js',
            files: [
                'app/libs/angular.js',
                'app/libs/angular-resource.js',
                'app/libs/angular-route.js',
                [...] 
                'app/modules/**/*-spec.js'
            ]
        },
        unit: {
            singleRun: true
        }
    }

Now when I run grunt karmamessages from the *-spec.jsfiles (describe('message', function() ...)) are displayed nicely in the console.

现在,当我运行grunt karma来自*-spec.js文件 ( describe('message', function() ...)) 的消息时,它会很好地显示在控制台中。

回答by Boris Yakubchik

I wrote a reporter to make the output more readable: karma-helpful-reporter

我写了一个记者来使输出更具可读性:karma-helpful-reporter

Has some nice customization options: https://github.com/whyboris/karma-helpful-reporter

有一些不错的自定义选项:https: //github.com/whyboris/karma-helpful-reporter

Instal instructions inside, basically npm install --save-dev karma-helpful-reporterand then add to the Karma configuration plugins section:

在里面安装说明,基本上npm install --save-dev karma-helpful-reporter然后添加到 Karma 配置插件部分:

plugins: [ 
  require('karma-helpful-reporter')
],
reporters: [ 'helpful' ],