运行“npm test"时没有控制台日志到 STDOUT;(笑话) [英] No console.log to STDOUT when running "npm test" (jest)

查看:30
本文介绍了运行“npm test"时没有控制台日志到 STDOUT;(笑话)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知 console.log() 在运行脚本时应该可以正常打印到控制台的 STDOUT.

From what I know console.log() should work without problem printing to the STDOUT of my console, when running a script.

但在我的情况下,我将 NPM 配置为在从 shell 发出 npm test 时运行 Jest,并且测试文件中的任何 console.log() 都没有在屏幕上打印任何东西.我也尝试使用 process.stdout.write() 但在运行 npm test 时仍然没有自定义输出.

But in my case I have NPM configured to run Jest when issuing npm test from the shell, and any console.log() inside the test files doesn't print anything on the screen. I tried also to use process.stdout.write() but still I get no custom output when running npm test.

我应该如何调试测试脚本中的内容?我不知道这是来自 Node、NPM 还是 Jest 的问题.有一个 Jest issue 看起来与我的很相似,但我仍然无法解决并输出一个简单的字符串;而其余的 Jest 输出则照常回显.

How am I supposed to debug stuff in my test scripts? I can't figure out if it is a problem from Node, from NPM, or from Jest. There's a Jest issue that looks similar to mine but still I cannot solve and get to output a simple string; while there rest of Jest output is echoed as usual.

有人遇到过类似的问题吗?

Anybody experienced a similar problem?

编辑 1:我尝试运行 npm test -- --runInBand 但没有出现日志.

EDIT 1: I tried running npm test -- --runInBand but the log doesn't appear.

尝试像这样重复运行命令

trying to run the command repeatedly like

console.log('foo...');console.log('bar..');console.log('baz..');

我有时可以看到日志,但随后会被以下 Jest 输出的其余部分覆盖/隐藏.

I can sometimes see the log, but this is then overwritten/hidden by the rest of the following Jest output.

推荐答案

来自链接的问题,因为问题已经持续多年了...

From the linked issue since the problem is going on since years...

alpha 版本中有拉取请求这将解决这个问题.看起来最后的精炼是在过去几个小时内进行的(参见 Github 问题链接).

There is a pull request in the alpha release that is going to fix this problem. Looks like the last refining is being worked in the past few hours (see Github issue link).

但是我找到了一个适用于我最简单情况的解决方案:

However I found a solution that works for my simplest case:

  • 只需运行启用详细模式,日志就会神奇地出现!
  • just run enabling verbose mode and the log will magically appear!
npm test -- --verbose=true

这个不会在所有情况下都解决;也许异步/多线程/等仍然会出现问题(请报告您的经验,以便我可以更新答案);例如在日志后面添加一个process.exit(1)会再次隐藏它.

This will not fix in every case; maybe async/multi-thread/etc will still have problems (please report your experiences, so I can update the answer); fo r example adding a process.exit(1) after the log will hide it again.

process.exit() 运行之前尝试按 ctrl + c 将显示日志实际上存在并且正在运行被覆盖.

Trying to press ctrl + c before the process.exit() runs (with the right timing...) will show that the log is actually there and being overridden.

我会用新闻更新答案,但这可能会帮助其他人开始使用 Node/NPM/Jest 设置!

I will update the answer with news, but probably this will help others in starting with Node/NPM/Jest setups!

这篇关于运行“npm test"时没有控制台日志到 STDOUT;(笑话)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆