console.log语句在Jest中不输出任何内容。昨天还挺管用的,今天突然就不管用了。我没有对我的配置做任何更改,也没有安装任何更新。
我没有使用——forceExit选项。仍然看到这个问题。
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,这是常用的
其他回答
这是一个相当古老的问题,至今仍没有公认的答案。然而,没有一个建议的解决方案对我有效(设置像-静音-verbose等)。主要问题是Jest更改了全局控制台对象。因此,最简单的解决方案是不使用全局控制台对象。
相反,从控制台模块导入专用的日志函数并使用它们:
import { error } from "console";
error("This is an error");
就这么简单。
尝试了关于jest配置设置的建议,但无济于事。相反,在我的案例中,问题似乎与不等待异步代码有关:
test("test", async () => {
console.log("Does output")
new Promise(resolve => {
// some expectation depending on async code
setTimeout(() => resolve(console.log("Does not output")) , 1)
})
})
相反,等待promise会输出async日志:
test("test", async () => {
console.log("Does output")
await new Promise(resolve => {
// some expectation depending on async code
setTimeout(() => resolve(console.log("Does output")) , 1)
})
})
可能相关背景: https://github.com/facebook/jest/issues/2441
根据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,这是常用的
在我的情况下,这个问题是由[只有]旗帜在: It.only()或test。(“文本”,()= > {})