How can I stop Jest wrapping `console.log` in test output?

Viewed 7391

I'm making a Jest test with request

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();
            }
        })
    })
});

and it's weird because in the terminal, jest is showing console.log line as part of the output:

 ...test output...

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

...more test output...

I need to hide the line:

console.log test/modules/user.test.js:18

How to hide console.log line in Jest output?

5 Answers

You can replace Jest's console implementation with the "normal" console like this:

const jestConsole = console;

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

afterEach(() => {
  global.console = jestConsole;
});

If it's a test you've written, why do you even need to log the error? You can rely on jest assertions for that.
If you have no other solution, you can stub out the console.log function like so:

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

/** Test logic goes here **/

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

You can use jest.spyOn to mock the console methods:

const spy = jest.spyOn(console,'warn').mockReturnValue()

// after you are done with the test, restore the console
spy.mockRestore()

You can create a utility function to mock the console:

function mockConsole(method = 'warn', value){
    return jest.spyOn(console,method).mockReturnValue(value).mockRestore
}

const restore = mockConsole() //mock it

// later when you are done
restore() // unmock it 

Also, you can combine above code answers with jest.beforeEach to automatically restore the console:

beforeEach(()=>{
    // beware that this will restore all mocks, not just the console mock
    jest.restoreAllMocks()
})

You need to create your own log function with process.stdout.write or use it instead of console.log because jest is spying on all console functions.

While ago I have been trying to bring to Typescript one utility that I really like in Scala; GivenWhenThen annotations in tests and I encountered the same problem as you. I don't understand why jest prints these "consol.log + line", it doesn't make sense because you can easily find them with Crt + Find and there is no option to switch them off with --silent and --verbose=false options (of course silent will work but it will remove all log so what is the point).

Finally, I came up with:

const colors = require('colors');

const testLog = (str : string) => process.stdout.write(str)

export const Given = (givenMsg: string) => testLog(`\n+ Given ${givenMsg}\n`)
export const When = (whenMsg: string) => testLog(`+ When ${whenMsg}\n`)
export const Then = (thenMsg: string) => testLog(`+ Then ${thenMsg}\n`)
export const And = (andMsg: string) => testLog(`- And ${andMsg}\n`)

export const testCase = (description: string, testFunction: Function) => it (description, () => {
    testLog(colors.yellow(`\n\nTest: ${description}\n`))
    testFunction()
    testLog('\n')
})

Looked like this:

given when then typescript

You could try running jest with the --silent argument.

In package.json, use two scripts:

"scripts": {
   "test": "jest --silent",
   "testVerbose": "jest"
},
Related