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

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


当前回答

日志记录不能打印的一个潜在原因是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,这是常用的

其他回答

在我的案例中,问题是当需要模块时,在实际测试用例开始之前,日志就被记录了下来。从顶级导入更改为在测试用例中使用require解决了这个问题。

你可以像这样一起运行两个选项——watch——verbose false,如果你还想监视文件并查看输出。

只运行一次就做——冗长的错误

尝试使用console.debug()代替。

在测试函数中运行console.debug('Message here', yourValueHere),它应该在运行测试脚本时显示在控制台输出中。您可以使用Ctrl+F验证它是否工作,并在标准输出中找到Message。

这是在控制台中显示输出的技巧,虽然这不是一个关于如何使用console.log的答案,我理解。

我运行@test -library/jest-dom和jest-junit 12.0.0作为开发依赖。 joke -junit的最小配置为

  "jest-junit": {
    "usePathForSuiteName": "true"
  },

在package.json。这主要用于配置覆盖率报告。 Jest是这样配置的:

  "jest": {
    "testMatch": [
      "**/__tests__/**/*.[jt]s?(x)",
      "**/?(*.)+(spec|test).[jt]s?(x)",
      "!**/utilities.ts",
    ],

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

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

除了前面提到的——verbose选项会导致这个错误外,注意——watch也可能导致这个错误。