Skip to content
gpawade edited this page Feb 11, 2018 · 1 revision

Mocha JS

Hooks

	describe('hooks', function() {

		  before(function() {
		    // runs before all tests in this block
		  });

		  after(function() {
		    // runs after all tests in this block
		  });

		  beforeEach(function() {
		    // runs before each test in this block
		  });

		  afterEach(function() {
		    // runs after each test in this block
		  });

		  // test cases
	});
Describing Hook
	beforeEach(function() {
	  // beforeEach hook
	});

	beforeEach(function namedFun() {
	  // beforeEach:namedFun
	});

	beforeEach('some description', function() {
	  // beforeEach:some description
	});

Asynchronous

	describe('User', function() {
	
		  describe('#save()', function() {

		    it('should save without error', function(done) {
		      
		       /// call done when asyn work is done

		    });

		  });

	});

EXCLUSIVE TESTS

The exclusivity feature allows you to run only the specified suite or test-case by appending .only() to the function.

	describe('Array', function() {
	  describe.only('#indexOf()', function() {
	    // ...
	  });
	});

INCLUSIVE TESTS

This feature is the inverse of .only(). By appending .skip(), you may tell Mocha to simply ignore these suite(s) and test case(s)

	describe('Array', function() {
	  describe.skip('#indexOf()', function() {
	    // ...
	  });
	});

Some helpful function

// Retry all tests in this suite up to 4 times
this.retries(4);


//To tweak what’s considered “slow”, you can use the slow() method:
this.slow(10000);

//
this.timeout(500);
//use this.timeout(0) to disable the timeout for a hook.

Mocha TDD Suite

	
	suite("suite name", function(){


		setup(function(){

			 // initial setup
		});

		

	})
Clone this wiki locally