How to specify test directory for mocha?

后端 未结 14 1483
走了就别回头了
走了就别回头了 2020-12-04 05:59

Mocha tries to find test files under test by default, how do I specify another dir, e.g. server-test?

相关标签:
14条回答
  • 2020-12-04 06:10

    I am on Windows 7 using node.js v0.10.0 and mocha v1.8.2 and npm v1.2.14. I was just trying to get mocha to use the path test/unit to find my tests, After spending to long and trying several things I landed,

    Using the "test/unit/*.js" option does not work on windows. For good reasons that windows shell doesn't expand wildcards like unixen.

    However using "test/unit" does work, without the file pattern. eg. "mocha test/unit" runs all files found in test/unit folder.

    This only still runs one folder files as tests but you can pass multiple directory names as parameters.

    Also to run a single test file you can specify the full path and filename. eg. "mocha test/unit/mytest1.js"

    I actually setup in package.json for npm "scripts": { "test": "mocha test/unit" },

    So that 'npm test' runs my unit tests.

    0 讨论(0)
  • 2020-12-04 06:12

    Run all files in test_directory including sub directories that match test.js

    find ./parent_test_directory -name '*test.js' | xargs mocha -R spec
    

    or use the --recursive switch

    mocha --recursive test_directory/
    
    0 讨论(0)
  • 2020-12-04 06:19

    I had this problem just now and solved it by removing the --recursive option (which I had set) and using the same structure suggested above:

    mochify "test/unit/**/*.js"

    This ran all tests in all directories under /test/unit/ for me while ignoring the other directories within /test/

    0 讨论(0)
  • 2020-12-04 06:19

    As @jeff-dickey suggested, in the root of your project, make a folder called test. In that folder, make a file called mocha.opts. Now where I try to improve on Jeff's answer, what worked for me was instead of specifying the name of just one test folder, I specified a pattern to find all tests to run in my project by adding this line:

    */tests/*.js --recursive in mocha.opts

    If you instead want to specify the exact folders to look for tests in, I did something like this:

    shared/tests/*.js --recursive
    server/tests/graph/*.js --recursive
    

    I hope this helps anyone who needed more than what the other answers provide

    0 讨论(0)
  • 2020-12-04 06:21

    Now a days(year 2020) you can handle this using mocha configuration file:

    Step 1: Create .mocharc.js file at the root location of your application

    Step 2: Add below code in mocha config file:

    'use strict';
    
    module.exports = {
      spec: 'src/app/**/*.test.js'
    };
    

    For More option in config file refer this link: https://github.com/mochajs/mocha/blob/master/example/config/.mocharc.js

    0 讨论(0)
  • 2020-12-04 06:22

    This doesn't seem to be any "easy" support for changing test directory.
    However, maybe you should take a look at this issue, relative to your question.

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