Remove logging the origin line in Jest

前端 未结 3 774
说谎
说谎 2020-12-06 13:29

Jest has this feature to log the line that outputs to console methods.

In some cases, this can become annoying:

  console.log _modules/l         


        
3条回答
  •  时光取名叫无心
    2020-12-06 14:21

    Jest injects custom console implementation that is based on extendable Console class into test global scope. Normally it provides useful debugging information alongside printed message that answers the question where potentially unwanted output comes from.

    In case this is undesirable for some reason, a simple way to retrieve default console implementation is to import it from Node built-in module.

    Can be done for specific console calls:

    let console = require('console');    
    ...
    console.log(...)
    

    For many of them that occur inside a range of tests:

    const jestConsole = console;
    
    beforeEach(() => {
      global.console = require('console');
    });
    
    afterEach(() => {
      global.console = jestConsole;
    });
    

    And so on.

提交回复
热议问题