我有一个为内置节点测试运行程序编写的测试用例。
const { describe, test } = require('node:test')
const assert = require('node:assert')
describe('hello', () => {
test('can say hello', () => {
console.log('Hello World')
assert.equal(2 + 2, 4)
})
})
我可以运行此测试并且它通过,为我提供摘要统计信息、计时信息等。
Hello World
▶ hello
✔ can say hello (1.157378ms)
▶ hello (2.997094ms)
ℹ tests 1
ℹ suites 1
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 53.233975
我的问题是我不希望包含控制台输出。在本例中,它只是一行“Hello World”,但在我的真实代码库中,它会变得非常混乱。如果我使用 jest runner,我只需执行 --silent (或将等效内容放入配置文件中),它就会消失。
node --test 的等效项是什么?我在文档中看不到任何看起来正确的内容(https://nodejs.org/api/test.html)
我尝试将 test-reporter-destination 设置为 stderr 并将 stdout 设置为 /dev/null 但这不起作用,因为控制台输出是规范输出的一部分。
简单地将所有内容重定向到文件是行不通的,因为这样我就无法获得摘要输出(即通过/失败的内容)。
考虑使用 Winston 记录器代替默认的 Node.js 记录器,并根据环境进行配置。 将 Winston 集成到您的应用程序中时,请将其设置为开发环境。运行测试时,切换到测试环境,您可以将 Winston 配置为仅在使用 logger.error 时记录日志。
或者,您可以简单地完全删除 console.log 语句。如果您不打算在控制台中看到它们,那么保留它们可能没有多大意义?