console.log语句在Jest中不输出任何内容。昨天还挺管用的,今天突然就不管用了。我没有对我的配置做任何更改,也没有安装任何更新。

我没有使用——forceExit选项。仍然看到这个问题。


当前回答

根据v27文档,沉默是你想要的。verbose false(默认值)阻止Jest输出层次结构中每个测试的结果,而silent true(默认值)将:

防止测试通过控制台打印消息。

使用npx jest——silent false如果你想在CLI中使用该选项运行jest。刚才用console.log进行了测试,结果与预期一致。

其他回答

日志记录不能打印的一个潜在原因是console.log被嘲笑了。如下所示

// jest-setup.js
global.console = {
  // eslint-disable-next-line no-undef
  log: jest.fn(), // console.log are ignored in tests
  // log: console.log,

  // Keep native behaviour for other methods, use those to print out things in your own tests, not `console.log`
  error: console.error,
  warn: console.warn,
  info: console.info,
  debug: console.debug,
};
// package.json
  "jest": {
    "preset": "react-native",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ],
    "setupFilesAfterEnv": [
      "@testing-library/jest-native/extend-expect",
      "<rootDir>/src/config/jest-setup.js"
    ],
    "testMatch": [
      "<rootDir>/src/**/__tests__/**/*.test.{ts,tsx}"
    ]
  },

如果你想禁用console.log,这是常用的

尝试使用console.info(),它是console.log()的别名。我尝试了上面几乎所有的答案,但console.log()仍然不适合我。因此,使用console.info()来完成这项工作。

Jest默认情况下抑制控制台日志消息。为了显示控制台日志消息,在命令行中将silent选项设置为false

在命令行设置——silent=false:

NPM运行测试-- --silent=false

这对我来说很管用:开玩笑——废话连篇

还要确保你的笑话配置没有silent: true。就我而言,我没有意识到其他人已经将其添加到我们的配置中。

我在配置选项列表中没有看到它,但是这里记录了命令行标志。