2021年5月6日星期四

How to achieve a clean test report for "throws" assertions in Jest

I've been looking for a way of hide some unnecessary console.error on Jest tests and found some are trying to hide all console messages for passed tests (https://github.com/facebook/jest/issues/4156).

I don't agree that jest should provide any mechanism to hide console.error even if a test passed. I believe that unintended errors should be printed even if a test passed. That way a developer should give it some attention as it could be a undesired situation that was not properly addressed.

But a situation that annoys me is when I write assertions ensuring something has failed and a console.error is printed anyway. Following a example:

test("NetworkError", async () => {    let httpClient = new HttpClient();      await expect(httpClient.send({ url: "/", method: "get" }))      .rejects //      .toThrow(NetworkError);  });  
 PASS  src/infra/httpClient.test.js    ● Console        console.error        Error: Error: connect ECONNREFUSED 127.0.0.1:80            at Object.dispatchError (C:\Users\paama\Projects\sandbox\react-arch\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:62:19)            at Request.<anonymous> (C:\Users\paama\Projects\sandbox\react-arch\node_modules\jsdom\lib\jsdom\living\xhr\XMLHttpRequest-impl.js:654:18)            at Request.emit (events.js:215:7)            at Request.onRequestError (C:\Users\paama\Projects\sandbox\react-arch\node_modules\request\request.js:877:8)            at ClientRequest.emit (events.js:210:5)            at Socket.socketErrorListener (_http_client.js:406:9)            at Socket.emit (events.js:210:5)            at emitErrorNT (internal/streams/destroy.js:92:8)            at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)            at processTicksAndRejections (internal/process/task_queues.js:80:21) undefined          at VirtualConsole.<anonymous> (node_modules/jsdom/lib/jsdom/virtual-console.js:29:45)        at Object.dispatchError (node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:65:53)        at Request.<anonymous> (node_modules/jsdom/lib/jsdom/living/xhr/XMLHttpRequest-impl.js:654:18)        at Request.onRequestError (node_modules/request/request.js:877:8)  

I get that this is the expected behavior for JavaScript applications, but somebody knows a way to deal with the situation and clear the test summary. I feel a little bit annoyed with error outputs confusing a successful test result.

https://stackoverflow.com/questions/67428121/how-to-achieve-a-clean-test-report-for-throws-assertions-in-jest May 07, 2021 at 10:06AM

没有评论:

发表评论