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?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
Not sure why the grep method is not working for me when using npm test. This works though. I also need to specify the test folder also for some reason.
npm test -- test/sometest.js
Looking into https://mochajs.org/#usage we see that simply use
will work. You can omit the '.js' at the end.
eg :
here 'test/api/controllers/test.js' is filepath.
If you are using
npm test
(using package.json scripts) use an extra--
to pass the param through to mochae.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:
Or alternatively use
--bail
which seems to be less fussyTry using mocha's
--grep
option:You can use any valid JavaScript regex as
<pattern>
. For instance, if we havetest/mytest.js
:Then:
To run a single test. Note that this greps across the names of all
describe(name, fn)
andit(name, fn)
invocations.Consider using nested
describe()
calls for namespacing in order to make it easy to locate and select particular sets.Depending on your usage pattern, you might just like to use only. We use the TDD style; it looks like this:
Only this test will run from all the files/suites.