Testing javascript with Mocha - how can I use console.log to debug a test?

后端 未结 5 2127
名媛妹妹
名媛妹妹 2020-12-13 16:31

I am using the javascript test-runner \"Mocha\".

I have a test that is failing, so I would to debug it using console.log.

But when the tests are

相关标签:
5条回答
  • 2020-12-13 17:21

    I had an issue with node.exe programs like test output with mocha.

    In my case, I solved it by removing some default "node.exe" alias.

    I'm using Git Bash for Windows(2.29.2) and some default aliases are set from /etc/profile.d/aliases.sh,

      # show me alias related to 'node'
      $ alias|grep node
      alias node='winpty node.exe'`
    

    To remove the alias, update aliases.sh or simply do

    unalias node
    

    I don't know why winpty has this side effect on console.info buffered output but with a direct node.exe use, I've no more stdout issue.

    0 讨论(0)
  • 2020-12-13 17:21

    Use the debug lib.

    import debug from 'debug'
    const log = debug('server');
    

    Use it:

    log('holi')
    

    then run:

    DEBUG=server npm test
    

    And that's it!

    0 讨论(0)
  • 2020-12-13 17:24

    If you are testing asynchronous code, you need to make sure to place done() in the callback of that asynchronous code. I had that issue when testing http requests to a REST API.

    0 讨论(0)
  • 2020-12-13 17:26

    You may have also put your console.log after an expectation that fails and is uncaught, so your log line never gets executed.

    0 讨论(0)
  • 2020-12-13 17:33

    What Mocha options are you using?

    Maybe it is something to do with reporter (-R) or ui (-ui) being used?

    console.log(msg);
    

    works fine during my test runs, though sometimes mixed in a little goofy. Presumably due to the async nature of the test run.

    Here are the options (mocha.opts) I'm using:

    --require should
    -R spec
    --ui bdd
    

    Hmm..just tested without any mocha.opts and console.log still works.

    0 讨论(0)
提交回复
热议问题