Javascript 在 Jasmine 测试中将对 console.log() 的调用重定向到标准输出

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

Redirect calls to console.log() to standard output in Jasmine tests

javascriptjasminejasmine-maven-plugin

提问by Lóránt Pintér

I'm using Jasmine via the jasmine-maven-plugin, and I would like to see console.log() messages in the Maven build output. Is there a way to achieve this?

我正在通过 jasmine-maven-plugin 使用 Jasmine,我希望在 Maven 构建输出中看到 console.log() 消息。有没有办法实现这一目标?

If console.log() cannot be redirected, is there any other way to log from my tests so that they show up on the Maven build output?

如果 console.log() 无法重定向,是否还有其他方法可以从我的测试中记录日志,以便它们显示在 Maven 构建输出中?

I'm running these tests on Jenkins in a headless fashion, and would like a means to get some debug output from the tests.

我正在以无头方式在 Jenkins 上运行这些测试,并且想要一种从测试中获取一些调试输出的方法。

回答by user1338062

Try

尝试

console.info('foo')

From the test javascripts.

来自测试javascripts。

回答by BanksySan

Jasmine 1.x

茉莉花 1.x

You can use:

您可以使用:

jasmine.log("I've got a big log.");

Jasmine 2+

茉莉花 2+

Use console.logdirectly, as per douglas-treadwell'scomment below.

console.log直接使用,根据下面道格拉斯-特雷德威尔的评论。

回答by Geoffrey Samper

If you are running in a node env. You could use

如果您在节点环境中运行。你可以用

process.stdout.write("this will be send to the console");

回答by Martin Borthiry

I think it is not possible.

我认为这是不可能的

I had to overwrite the console.log implementation in the spec loader. i.e (using jQuery):

我不得不覆盖规范加载器中的 console.log 实现。即(使用jQuery)

var console = {
    panel: $('body').append('<div>').css({position:'fixed', top:0, right:0,background:'transparent'}),
    log: function(m){
        this.panel.prepend('<div>'+m+'</div>');
    }       

};
        console.log('message 1');
        console.log('message 2');

? hereyour have a functional example

? 这里有一个功能示例

回答by Dave Sag

I'm using jasmine 2via guardand phantom jsand have found that standard console.logmessages within tests are output to the jasmine spec runner's console just fine.

我正在使用jasmine 2viaguardphantom js发现console.log测试中的标准消息输出到 jasmine spec runner 的控制台就好了。

I've also found that console.logmessages within the javascript code elements that I am testing do get written out to stdoutbut not console.logmessages within the tests themselves.

我还发现console.log我正在测试的 javascript 代码元素中的消息确实会被写出,stdoutconsole.log测试本身中的消息却没有。

回答by SkipHyman

Ran into the same issue using grunt/karma/jasmine (karma-jasmine 0.2.2) -

使用 grunt/karma/jasmine (karma-jasmine 0.2.2) 遇到了同样的问题 -

To go along with what Dave Sag said, I've found that all my console.logmessages from the code I'm testing run fine, however nothing from my describe() {}and it() {}blocks log anything.

与 Dave Sag 所说的一致,我发现console.log我正在测试的代码中的所有消息都运行良好,但是我的describe() {}it() {}块中没有任何内容记录任何内容。

I didfind that you can log from a beforeEach() {}block. At least it worked for me :

确实发现您可以从beforeEach() {}块中登录。至少它对我有用:

beforeEach(function() {
  this.model = new blahModel();
  console.log('this.model = ', this.model);
});

Note thatthis only logs in the browser console and for some reason won't log in command line. Somewhat strange when the console.log statements from the tested code block do log in the command line. I've also found what seems to be a better approach for consistent logging here.

请注意,这只会登录浏览器控制台,出于某种原因不会登录命令行。当来自测试代码块的 console.log 语句确实记录在命令行中时,有些奇怪。我还发现了什么,似乎是一致的记录一个更好的方法在这里

UPDATE : I'm actually seeing the logging working for it blocks as well, I believe I had other errors that were preventing this.

更新:我实际上也看到了为它工作的日志记录块,我相信我还有其他错误阻止了这一点。

回答by Professor Tom

If you're desperate for any output in a Terminal window so you can have data to review after the test has completed and the browser has closed, console.error()seems to do the trick.

如果您迫切需要终端窗口中的任何输出,以便您可以在测试完成且浏览器关闭后查看数据,那么console.error()似乎可以解决问题。

回答by Jegan Gopalakrishnan

1) Go to your project directory where you have your pom.xml. Run the following command in cmd. mvn jasmine:bdd

1) 转到您拥有 pom.xml 的项目目录。在 cmd 中运行以下命令。mvn茉莉花:bdd

2) You will get the localhost URL : localhost:8234 (just an example).

2) 您将获得 localhost URL:localhost:8234(只是一个示例)。

3) Run this URL in the browser. Now all your test cases gets executed.

3) 在浏览器中运行这个 URL。现在你所有的测试用例都被执行了。

4) Do the Inspect element of this page. In the browser console you will be able to see all the console.log() or console.error() traces.

4) 执行此页面的 Inspect 元素。在浏览器控制台中,您将能够看到所有 console.log() 或 console.error() 跟踪。