简体   繁体   English

我可以更改jasmine-node的输出以显示控制台中的传递测试以及失败吗?

[英]Can I change the output of jasmine-node to show the passing tests in the console as well as the failing?

I have the following file that runs when I run node test.js . 我运行node test.js时运行以下文件。

var Jasmine = require('jasmine');
var jasmine = new Jasmine();
var request = require('request');

describe("test", function(){
    it("works", function(){
        expect(5 + 2).toEqual(4);
    });

    it("should respond with hello world", function(done){
        request('http://localhost:3000/', function(err, res, body){
            expect(body).toEqual('hello world');
            done();
        })
    });
})

jasmine.execute();

And this gives me the following output: 这给了我以下输出:

Started
F.

Failures:
1) test works
  Message:
    Expected 7 to equal 4.
  Stack:
    Error: Expected 7 to equal 4.
        at Object.<anonymous> 

2 specs, 1 failure
Finished in 0.037 seconds

Obviously one fails, showing the F, and one passes, showing the dot. 显然,一个失败,显示F,一个通过,显示点。 Can I change this configuration to have it show both the passing and the failing tests? 我是否可以更改此配置以使其显示通过和失败的测试?

You'll want to use a custom reporter . 您将要使用自定义报告者 I recommend using jasmine-console-reporter , which will give you nicely formatted output that will include all tests that ran (not just the failed ones). 我建议使用jasmine-console-reporter ,它将为您提供格式良好的输出,其中包括所有运行的测试(不仅仅是失败的测试)。 Your original script would change to the following: 您的原始脚本将更改为以下内容:

var Jasmine = require("jasmine");
var jasmine = new Jasmine();
var request = require('request');

// Register a Custom Reporter
const Reporter = require('jasmine-console-reporter');
jasmine.jasmine.getEnv().addReporter(new Reporter());

describe("test", function(){
    it("works", function(){
        expect(5 + 2).toEqual(4);
    }); 

    it("should respond with hello world", function(done){
        request('http://localhost:3000/', function(err, res, body){
            expect(body).toEqual('hello world');
            done();
        })  
    }); 
})

jasmine.execute();

Note that if you are using the jasmine command line to run the tests (and so Jasmine has exported its helpers into your namespace), your code would be as follows: 请注意,如果您使用jasmine命令行来运行测试(因此Jasmine已将其帮助程序导出到您的命名空间中),您的代码将如下所示:

const Reporter = require('jasmine-console-reporter');
jasmine.getEnv().addReporter(new Reporter());

I personally find it easiest to use that with Gulp and gulp-jasmine to make the definition clear and in one place, while also allowing me to run build steps prior to the tests: 我个人觉得最简单的方法就是使用Gulpgulp-jasmine将定义清楚地放在一个地方,同时还允许我在测试之前运行构建步骤:

const gulp = require('gulp');
const jasmine = require('gulp-jasmine');
const Reporter = require('jasmine-console-reporter');

gulp.task('default', function() {
    gulp.src('spec/**/*.js')
        .pipe(jasmine({
            reporter: new Reporter()
        }));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM