Author: Not specified Language: javascript
Description: Not specified Timestamp: 2018-04-20 09:25:44 +0000
View raw paste Reply
  1. // # Mocha Guide to Testing
  2. // Objective is to explain describe(), it(), and before()/etc hooks
  3.  
  4. // 1. `describe()` is merely for grouping, which you can nest as deep
  5. // 2. `it()` is a test case
  6. // 3. `before()`, `beforeEach()`, `after()`, `afterEach()` are hooks to run
  7. // before/after first/each it() or describe().
  8. //
  9. // Which means, `before()` is run before first it()/describe()
  10.  
  11. // -----------------------------------------------------------------------------
  12.  
  13. // should.js is the preferred assertion library
  14. var should = require('should');
  15.  
  16. // **Only 1 test case (in a nameless test suite)**
  17. it('birds should fly', function(){
  18.   /** here.should.be.tested
  19.     * However, as long as no error within a it(),
  20.     * it() is considered PASSED */
  21. })
  22.  
  23.  
  24. // **Only 1 test case, but nested 3-level deep**
  25.  
  26. // describe() are:
  27. // - commonly known as test suites, which contains test cases
  28. // - merely groups, and you can have groups within groups
  29. describe('galaxy', function(){
  30.   describe('earth', function(){
  31.     describe('singapre', function(){
  32.       it('birds should fly', function(){ /** ... */ })
  33.     })
  34.   })
  35. })
  36.  
  37.  
  38. // **2 test cases in 1 test suite**
  39.  
  40. // A common scenario.
  41. describe('singapre', function(){
  42.   it('birds should fly', function(){ /** ... */ })
  43.   it('horse should gallop', function(){ /** ... */ })
  44. })
  45.  
  46.  
  47. // **Run once before the first test case**
  48. describe('singapre', function(){
  49.   before(function(){
  50.     console.log('see.. this function is run ONCE only')
  51.   })
  52.   it('birds should fly', function(){ /** ... */ })
  53.   it('horse should gallop', function(){ /** ... */ })
  54. })
  55.  
  56.  
  57. // **Run once before each test case**
  58. describe('singapre', function(){
  59.   beforeEach(function(){
  60.     console.log('see.. this function is run EACH time')
  61.   })
  62.   it('birds should fly', function(){ /** ... */ })
  63.   it('horse should gallop', function(){ /** ... */ })
  64. })
  65.  
  66. // **2 test suites in a big test suite**
  67.  
  68. // A common scenario.
  69. describe('earth', function(){
  70.   describe('singapre', function(){
  71.     it('birds should fly', function(){ /** ... */ })
  72.   })
  73.   describe('malaysia', function(){
  74.     it('birds should soar', function(){ /** ... */ })
  75.   })
  76. })
  77.  
  78.  
  79. // **before() can be applied to describe() too**
  80. describe('earth', function(){
  81.   before(function(){
  82.     console.log('see.. this function is run ONCE only, before first describe()')
  83.   })
  84.   describe('singapre', function(){
  85.     it('birds should fly', function(){ /** ... */ })
  86.   })
  87.   describe('malaysia', function(){
  88.     it('birds should soar', function(){ /** ... */ })
  89.   })
  90. })
  91.  
  92.  
  93. // **beforeEach() can be applied to describe() too**
  94. describe('earth', function(){
  95.   beforeEach(function(){
  96.     console.log('see.. this function is run EACH time, before each describe()')
  97.   })
  98.   describe('singapre', function(){
  99.     it('birds should fly', function(){ /** ... */ })
  100.   })
  101.   describe('malaysia', function(){
  102.     it('birds should soar', function(){ /** ... */ })
  103.   })
  104. })
  105.  
  106.  
View raw paste Reply