How to run multiple test files in different groups from grunt mocha

If we have mocha configured, we can run tests using grunt mocha / mochaTest. What if we want to run different configurations ex. integration tests / smoke tests / unit tests etc.

Below is sample code which shows mochaTest configuration.

GRUNTFILE.JS
grunt.initConfig({
	... // skipping other details

	mochaTest : {
		unit : {
			options : {
				//reporter : 'mocha-sonar-reporter', 

				reporter : 'spec',
				timeout : 5000,
				quiet : false 
			},
			src : ["tests/app-unit-test.js"]
		}
	}
});

We can run tests using grunt mochaTest

grunt mochaTest

grunt mochaTest:unit

What if, we want to add new tests and want to run them separately? We can use new targets and add files you want to run corresponding to that.

GRUNTFILE.JS
grunt.initConfig({
	... // skipping other details

	mochaTest : {
		unit : {
			options : {
				//reporter : 'mocha-sonar-reporter', 

				reporter : 'spec',
				timeout : 5000,
				quiet : false 
			},
			src : ["tests/app-unit-test.js"]
		},
		smoke : {
			options : {
				//reporter : 'mocha-sonar-reporter', 

				reporter : 'spec',
				timeout : 5000,
				quiet : false 
			},
			src : ["tests/app-smoke-test.js"]
		},
		it : {
			options : {
				//reporter : 'mocha-sonar-reporter', 

				reporter : 'spec',
				timeout : 5000,
				quiet : false 
			},
			src : ["tests/app-integration-test.js"]
	}
});

and then we can run unit test using following command

grunt mochaTest:unit

We can run smoke test using following command

grunt mochaTest:smoke

and integration test using below command

grunt mochaTest:it

To run all tests together, run below command

grunt mochaTest

Hope it helps!

Version History


Date Description
2016-03-03    Initial Version