How to run a single test with Mocha?

前端 未结 12 1148
無奈伤痛
無奈伤痛 2020-12-04 05:46

I use Mocha to test my JavaScript stuff. My test file contains 5 tests. Is that possible to run a specific test (or set of tests) rather than all the tests in the file?

12条回答
  •  天命终不由人
    2020-12-04 05:52

    If you are using npm test (using package.json scripts) use an extra -- to pass the param through to mocha

    e.g. npm test -- --grep "my second test"

    EDIT: Looks like --grep can be a little fussy (probably depending on the other arguments). You can:

    Modify the package.json:

    "test:mocha": "mocha --grep \"\" .",
    

    Or alternatively use --bail which seems to be less fussy

    npm test -- --bail
    

提交回复
热议问题