{ test("It should respond with a array of users",-6ren">
gpt4 book ai didi

jestjs - 如何在测试输出中停止 Jest 包装 `console.log`?

转载 作者:行者123 更新时间:2023-12-03 15:56:17 26 4
gpt4 key购买 nike

我正在根据请求进行 Jest 测试

describe("GET /user ", () => {
test("It should respond with a array of users", done => {
return request(url, (err, res, body) => {
if (err) {
console.log(err)
} else {
console.log("GET on " + url);
body = JSON.parse(body);
expect(body.length).toBeGreaterThan(0);
done();
}
})
})
});

这很奇怪,因为在终端中,jest 将 console.log 行显示为输出的一部分:
 ...test output...

console.log test/modules/user.test.js:18
GET on https://xpto/api/user

...more test output...
我需要隐藏该行:
console.log test/modules/user.test.js:18
如何在 Jest 输出中隐藏 console.log 行?

最佳答案

如果这是您编写的测试,为什么您甚至需要记录错误?你可以依靠 Jest 断言。
如果您没有其他解决方案,您可以删除 console.log功能如下:

const log = console.log;
console.log = () => {};

/** Test logic goes here **/

console.log = log; // Return things to normal so other tests aren't affected.

关于jestjs - 如何在测试输出中停止 Jest 包装 `console.log`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56448749/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com