console.log() not allowed in Jest tests



  • I am using the @quasar/testing harness for Jest and everything is fine except that I can not use console.log() in tests, instead the console shows the following error:

    Do not use console.log() in production

    It even persists when I go to .eslintrc.js and disable the line:

    // 'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',

    This leads me to conclude that there must be some other .eslintrc.js file that is connected to the Jest environment.

    Why would the Jest environment be set up to be detected as production???

    Can somebody please tell me how to correct this?

    Thank you.



  • Hi,

    In your /test/jest/jest.setup.js file, there is this line:
    console.log = jest.fn(() => { throw new Error(‘Do not use console.log() in production’) })

    Feel free to comment it out.



  • On a side note, console.log is not your friend. It blocks javascript threads, but I’ll take your experience and turn it off by default so that users have to “opt in”.



  • @nothingismagick thank you for your help. I suspect I must have had case-matching turned on during the search for that line of code, so apologies for the silly question.

    I would be happy to forego console.log() and begin using the Chrome debugger but I have been unable to get this to work with Quasar. I have tried the following:

    1. Add debugger statement to the test code
    2. Run the tests using yarn test:unit:debug
    3. Navigate to ‘chrome://inspect’
    4. Click on the ‘inspect’ link for Remote Target: jest file:///

    New Chrome window opens with:

    Uncaught Error: Cannot find module '<my-project-folder>\jest'
    

    A guide to accessing debugging would be a great addition to the Quasar Testing documentation. 🙂