Skip to content

Instantly share code, notes, and snippets.

@spiralx
Forked from samwize/mocha-guide-to-testing.js
Last active August 11, 2016 13:43
Show Gist options
  • Save spiralx/c1755434ad1d9786c4bf15942e2078dc to your computer and use it in GitHub Desktop.
Save spiralx/c1755434ad1d9786c4bf15942e2078dc to your computer and use it in GitHub Desktop.
Explain Mocha's testing framework - describe/it/before/beforeEach etc.
/* -----------------------------------------------------------------------------
### 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
as is required.
2. `it()` is a test case.
3. `before()`, `beforeEach()`, `after()`, `afterEach()` are hooks
that get called before/after first/each `it()` or `describe()`.
Which means that `before()` is run before the first `it()`/`describe()`.
----------------------------------------------------------------------------- */
// should.js is the preferred assertion library
import should from 'should'
// **Only 1 test case (in a nameless test suite)**
it('birds should fly', () => {
// As long as no error is thrown, it() is considered to have 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', () => {
describe('earth', () => {
describe('singapre', () => {
it('birds should fly', () => { /** ... */ })
})
})
})
// **2 test cases in 1 test suite**
describe('singapre', () => {
it('birds should fly', () => { /** ... */ })
it('horse should gallop', () => { /** ... */ })
})
// **Run once before the first test case**
describe('singapre', () => {
before(() => {
console.log('see.. this function is run ONCE only')
})
it('birds should fly', () => { /** ... */ })
it('horse should gallop', () => { /** ... */ })
})
// **Run once before each test case**
describe('singapre', () => {
beforeEach(() => {
console.log('see.. this function is run EACH time')
})
it('birds should fly', () => { /** ... */ })
it('horse should gallop', () => { /** ... */ })
})
// **2 test suites in a big test suite**
describe('earth', () => {
describe('singapre', () => {
it('birds should fly', () => { /** ... */ })
})
describe('malaysia', () => {
it('birds should soar', () => { /** ... */ })
})
})
// **before() can be applied to describe() too**
describe('earth', () => {
before(() => {
console.log('see.. this function is run ONCE only, before first describe()')
})
describe('singapre', () => {
it('birds should fly', () => { /** ... */ })
})
describe('malaysia', () => {
it('birds should soar', () => { /** ... */ })
})
})
// **beforeEach() can be applied to describe() too**
describe('earth', () => {
beforeEach(() => {
console.log('see.. this function is run EACH time, before each describe()')
})
describe('singapre', () => {
it('birds should fly', () => { /** ... */ })
})
describe('malaysia', () => {
it('birds should soar', () => { /** ... */ })
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment