How to specify test directory for mocha?

后端 未结 14 1485
走了就别回头了
走了就别回头了 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:23

    If in node.js, some new configurations as of Mocha v6:

    Option 1: Create .mocharc.json in project's root directory:

    {
      "spec": "path/to/test/files"
    }
    

    Option 2: add mocha property in project's package.json:

    {
      ...
    
      "mocha": {
        "spec": "path/to/test/files"
      }
    }
    

    More options are here.

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

    If you are using nodejs, in your package.json under scripts

    1. For global (-g) installations: "test": "mocha server-test" or "test": "mocha server-test/**/*.js" for subdocuments
    2. For project installations: "test": "node_modules/mocha/bin/mocha server-test" or "test": "node_modules/mocha/bin/mocha server-test/**/*.js" for subdocuments

    Then just run your tests normally as npm test

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

    Edit : This option is deprecated : https://mochajs.org/#mochaopts


    If you want to do it by still just running mocha on the command line, but wanted to run the tests in a folder ./server-tests instead of ./test, create a file at ./test/mocha.opts with just this in the file:

    server-tests
    

    If you wanted to run everything in that folder and subdirectories, put this into test/mocha.opts

    server-tests
    --recursive
    

    mocha.opts are the arguments passed in via the command line, so making the first line just the directory you want to change the tests too will redirect from ./test/

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

    Here's one way, if you have subfolders in your test folder e.g.

    /test
    /test/server-test
    /test/other-test
    

    Then in linux you can use the find command to list all *.js files recursively and pass it to mocha:

    mocha $(find test -name '*.js')
    
    0 讨论(0)
  • 2020-12-04 06:29

    Don't use the -g or --grep option, that pattern operates on the name of the test inside of it(), not the filesystem. The current documentation is misleading and/or outright wrong concerning this. To limit the entire command to a portion of the filesystem, you can pass a pattern as the last argument (its not a flag).

    For example, this command will set your reporter to spec but will only test js files immediately inside of the server-test directory:

    mocha --reporter spec server-test/*.js
    

    This command will do the same as above, plus it will only run the test cases where the it() string/definition of a test begins with "Fnord:":

    mocha --reporter spec --grep "Fnord:" server-test/*.js
    
    0 讨论(0)
  • 2020-12-04 06:31

    Use this:

    mocha server-test
    

    Or if you have subdirectories use this:

    mocha "server-test/**/*.js"
    

    Note the use of double quotes. If you omit them you may not be able to run tests in subdirectories.

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