Skip to content

Instantly share code, notes, and snippets.

@samwize
Created February 8, 2014 05:53
Show Gist options
  • Save samwize/8877226 to your computer and use it in GitHub Desktop.
Save samwize/8877226 to your computer and use it in GitHub Desktop.
Explain Mocha's testing framework - describe(), it() and before()/etc hooks
// # Mocha Guide to Testing
// Objective is to explain describe(), it(), and before()/etc hooks
// 1. `describe()` is merely for grouping, which you can nest as deep
// 2. `it()` is a test case
// 3. `before()`, `beforeEach()`, `after()`, `afterEach()` are hooks to run
// before/after first/each it() or describe().
//
// Which means, `before()` is run before first it()/describe()
// -----------------------------------------------------------------------------
// should.js is the preferred assertion library
var should = require('should');
// **Only 1 test case (in a nameless test suite)**
it('birds should fly', function(){
/** here.should.be.tested
* However, as long as no error within a it(),
* it() is considered PASSED */
})
// **Only 1 test case, but nested 3-level deep**
// describe() are:
// - commonly known as test suites, which contains test cases
// - merely groups, and you can have groups within groups
describe('galaxy', function(){
describe('earth', function(){
describe('singapre', function(){
it('birds should fly', function(){ /** ... */ })
})
})
})
// **2 test cases in 1 test suite**
// A common scenario.
describe('singapre', function(){
it('birds should fly', function(){ /** ... */ })
it('horse should gallop', function(){ /** ... */ })
})
// **Run once before the first test case**
describe('singapre', function(){
before(function(){
console.log('see.. this function is run ONCE only')
})
it('birds should fly', function(){ /** ... */ })
it('horse should gallop', function(){ /** ... */ })
})
// **Run once before each test case**
describe('singapre', function(){
beforeEach(function(){
console.log('see.. this function is run EACH time')
})
it('birds should fly', function(){ /** ... */ })
it('horse should gallop', function(){ /** ... */ })
})
// **2 test suites in a big test suite**
// A common scenario.
describe('earth', function(){
describe('singapre', function(){
it('birds should fly', function(){ /** ... */ })
})
describe('malaysia', function(){
it('birds should soar', function(){ /** ... */ })
})
})
// **before() can be applied to describe() too**
describe('earth', function(){
before(function(){
console.log('see.. this function is run ONCE only, before first describe()')
})
describe('singapre', function(){
it('birds should fly', function(){ /** ... */ })
})
describe('malaysia', function(){
it('birds should soar', function(){ /** ... */ })
})
})
// **beforeEach() can be applied to describe() too**
describe('earth', function(){
beforeEach(function(){
console.log('see.. this function is run EACH time, before each describe()')
})
describe('singapre', function(){
it('birds should fly', function(){ /** ... */ })
})
describe('malaysia', function(){
it('birds should soar', function(){ /** ... */ })
})
})
@sanfordstaab
Copy link

What this is not telling me is the scope of variables created in say the before() or describe() functions.
What I want to do is:

  1. create an object
  2. mess with it
  3. check its state
  4. mess with it some more
  5. check its state
    etc.

@tamlyn
Copy link

tamlyn commented Jan 29, 2023

@sanfordstaab there's nothing magic going on with variable scoping, normal JavaScript rules apply. If you create variable in an outer scope, you can modify it in an inner scope then access it in a sibling scope.

@sanfordstaab
Copy link

Ah, yes, I eventually figured that out but what I noticed is that each it() and apparently describe() is called sequentially by mocha in an apparently asynchronous fashion, which I imagine is necessary to support async tests. Things happen in order but the flow is not linear.
Anyway, I am now wondering how to do UI testing that requires user verification and interaction - like mouse clicks on rendered HTML. I am researching that now and considering using alert() or confirm() messages to do that. Any suggestions are welcome and thanks for your examples here!

@waveo-wangxiao
Copy link

Hi, I am new to mocha, may I know if I could put 'describe' in 'it'? Something like below:

let data: string[]

before('prepare data', ()=> {
	return new Promise((resolve: any) => {
		setTimeout(()=> {
			data = ['a', 'b'];
			resolve();
		}, 500)
	});
});

it("test", function () {
	describe("data", function () {
		data?.forEach((m) => {
			it(`${m}`, function () {
				assert(m, 'not empty');
			});
		})
	})
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment