Reputation: 171509
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?
Upvotes: 448
Views: 304974
Reputation: 3267
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$"
Upvotes: 2
Reputation: 715
Just add 'f' in front of 'it' function that run only a test where you added.
fit('should proprerly parse Enums', () => { }
Upvotes: 0
Reputation: 8383
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
Upvotes: 18
Reputation: 351
Looking into the doc, we see that simply using:
mocha test/myfile
will work. You can omit the '.js' at the end.
Upvotes: 11
Reputation: 29
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
Upvotes: 0
Reputation: 1133
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
Upvotes: 5
Reputation: 1189
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.
Upvotes: 6
Reputation: 10416
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
Upvotes: 40
Reputation: 1331
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.
Upvotes: 38
Reputation: 7344
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"
Upvotes: 5
Reputation: 1251
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() {
// ...
});
Upvotes: 93
Reputation: 11585
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
Upvotes: 129
Reputation: 1841
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.
Upvotes: 12
Reputation: 19377
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.
Upvotes: 514
Reputation: 2470
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.
Upvotes: 230