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?
15 Answers
Try using mocha's --grep
option:
-g, --grep <pattern> only run tests matching <pattern>
You can use any valid JavaScript regex as <pattern>
. For instance, if we have test/mytest.js
:
it('logs a', function(done) {
console.log('a');
done();
});
it('logs b', function(done) {
console.log('b');
done();
});
Then:
$ mocha -g 'logs a'
To run a single test. Note that this greps across the names of all describe(name, fn)
and it(name, fn)
invocations.
Consider using nested describe()
calls for namespacing in order to make it easy to locate and select particular sets.
-
6When using Mocha programmatically (for example from Grunt), there's a catch: grep option needs to be a ´RegExp()´ object. If it's a string, it will be escaped.– h-kippoCommented Nov 21, 2014 at 7:42
-
1this is the only solution that worked for me. specifying files on the command line (
mocha some/test.js
) didn't work at all. thanks!– hrabanCommented Feb 23, 2018 at 10:52 -
2Any way to avoid also running
logs a bcd
which contains thelogs a
substring? Regexp^$
not working on 0.10.2. Commented Oct 26, 2019 at 20:26 -
Thanks.I get some errors if I don't specify directory as mocha -g 'logs a' .– ktaCommented Apr 12, 2020 at 9:48
-
For any windows users: the test name needs to be double quotes, at least that is what worked for me.– KrispiesCommented Nov 30, 2020 at 15:41
Depending on your usage pattern, you might just like to use only. We use the TDD style; it looks like this:
test.only('Date part of valid Partition Key', function (done) {
//...
}
Only this test will run from all the files/suites.
-
5BDD works the same. This is great for when you are working on a single test. It can also be used at the suite/describe level Commented Jul 19, 2013 at 2:14
-
31
-
4This answer is better since it will also work when mocha is used from inside karma, etc. Commented Aug 9, 2016 at 8:53
-
3
it.only
will not work together withgiven
of mocha-testdata. And it's easy to forget to remove it later on.– Tr1etCommented May 26, 2017 at 2:01 -
6
eslint-plugin-mocha
has a ruleno-exclusive-tests
which will catch you if you forget to remove.only
.– dostuCommented May 3, 2018 at 22:41
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 \"<DealsList />\" .",
Or alternatively use --bail
which seems to be less fussy
npm test -- --bail
-
2@Andy
--grep
can be a little fussy - try putting it right after mocha within the npm script entry. Elsenpm test -- --bail
is useful for bailing after the first failing test Commented Jan 3, 2017 at 16:42 -
2passing extra dashes before
--grep
saved my day! Thanks a lot. PS. I'm on Windows, runningnpm test -- --grep @tag
– MykolaCommented Apr 10, 2019 at 17:54
Just use .only before 'describe', 'it' or 'context'. I run using "$npm run test:unit", and it executes only units with .only.
describe.only('get success', function() {
// ...
});
it.only('should return 1', function() {
// ...
});
run single test –by filename–
Actually, one can also run a single mocha test by filename (not just by „it()-string-grepping“) if you remove the glob pattern (e.g. ./test/**/*.spec.js
) from your mocha.opts, respectively create a copy, without:
node_modules/.bin/mocha --opts test/mocha.single.opts test/self-test.spec.js
Here's my mocha.single.opts (it's only different in missing the aforementioned glob line)
--require ./test/common.js
--compilers js:babel-core/register
--reporter list
--recursive
Background: While you can override the various switches from the opts-File (starting with --
) you can't override the glob. That link also has
some explanations.
Hint: if node_modules/.bin/mocha
confuses you, to use the local package mocha. You can also write just mocha
, if you have it installed globally.
And if you want the comforts of package.json
: Still: remove the **/*
-ish glob from your mocha.opts
, insert them here, for the all-testing, leave them away for the single testing:
"test": "mocha ./test/**/*.spec.js",
"test-watch": "mocha -R list -w ./test/**/*.spec.js",
"test-single": "mocha",
"test-single-watch": "mocha -R list -w",
usage:
> npm run test
respectively
> npm run test-single -- test/ES6.self-test.spec.js
mind the --
which chains whatever text comes after it to the npm
script
-
$1 was not needed in my setup, instead, it lead to warnings like:
Warning: Could not find any test files matching pattern: $1
Commented Jan 13, 2020 at 12:27
There are multiple ways by which you can do this.
If you just want to run one test from your entire list of test cases then, you can write only ahead of your test case.
it.only('<test scenario name>', function() { // ... });
or you can also execute the mocha grep command as below
mocha -g <test-scenario-name>
If you want to run all the test cases which are inside one describe section, then you can also write only to describe as well.
describe.only('<Description of the tests under this section>', function() { // ... });
If you have multiple test files & you wanted to run only one of then you can follow the below command.
npm test <filepath>
eg :
npm test test/api/controllers/test.js
here 'test/api/controllers/test.js' is filepath.
-
1the
it.only()
syntax is really nifty especially if you don't want to modify yourpackage.json
(i.e., when the unit test runner is an aliased entry under"scripts"
) every time you want to change the test case. more context: medium.com/@cnadeau_/…– kip2Commented Jun 2, 2021 at 19:14
You can try "it.only"
it.only('Test one ', () => {
expect(x).to.equal(y);
});
it('Test two ', () => {
expect(x).to.equal(y);
});
in this the first one only will execute
-
2Dangerous if tests are are not executed via
--forbid-only
on the CI / build server..only
has a tendency to sneak into main branch and rendering the other tests mute as they just will not execute anymore. I came to think of the workflow of changing code to run different tests an anti-feature. The test runner should determine which tests should be executed (aka tagged tests) not the developer. Commented Mar 13, 2019 at 15:01 -
I have used this for testing one functionality in my local, Note: dont commit code with
it.only
to repo– Bikesh MCommented Mar 14, 2019 at 9:25
Hi above solutions didn't work for me. The other way of running a single test is
mocha test/cartcheckout/checkout.js -g 'Test Name Goes here'
This helps to run a test case from a single file and with specific name.
-
2Use an expression that is selective enough for
-g
and you won't need to specify a file name. If you cannot write an expression that is selective enough, that means that you're not naming your tests properly.– LouisCommented Nov 30, 2016 at 11:24
Looking into the doc, we see that simply using:
mocha test/myfile
will work. You can omit the '.js' at the end.
Using Mocha's --fgrep
(or just -f) you can select tests containing string, for example:
mocha -f 'my test x'
will run all tests containing my test x
in either it()
, describe()
or context()
blocks.
For those who are looking to run a single file but they cannot make it work, what worked for me was that I needed to wrap my test cases in a describe suite as below and then use the describe title e.g. 'My Test Description' as pattern.
describe('My Test Description', () => {
it('test case 1', () => {
// My test code
})
it('test case 2', () => {
// My test code
})
})
then run
yarn test -g "My Test Description"
or
npm run test -g "My Test Description"
-
getting 'Warning: Cannot find any files matching pattern "My Test Description"' and it runs the entire tests Commented Feb 14 at 4:52
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 for some reason.
npm test -- test/sometest.js
To run a single test with the following form:
//file: good.test.js
describe('describeTitle', function() {
it('itTitle', function() {
//some assertions
});
});
You can run mocha like this:
mocha --grep "^describeTitle itTitle$"
And if you have multiple tests in different files that match this pattern "^describeTitle itTitle$"
you can also specify the file's absolute/relative path, before --grep
like this: mocha path/to/good.test.js --grep "^describeTitle itTitle$"
For example, Consider the following
describe('POST /api/products', function() {
it('test 1', function() {
//some assertions
});
it('test 2', function() {
//some assertions
});
});
To run test 1
you can run mocha like this:
mocha --grep "^POST /api/products test 1$"
Consolidate all your tests in one test.js file and add a script in your package.json:
"scripts": {
"api:test": "node_modules/.bin/mocha --timeout 10000 --recursive api_test/"
},
Type this command in your test directory:
npm run api:test
-
in package.json, you can just use as 'mocha'. it has not to write node_modules. when you want to run on CLI, just use prefix 'npx' Commented Apr 30, 2022 at 18:04
Just add 'f' in front of 'it' function that run only a test where you added.
fit('should proprerly parse Enums', () => { }
-
This looks invalid. Use .only(), reference mochajs.org/#exclusive-tests Commented Sep 19, 2023 at 7:13