bdd.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Module dependencies.
  3. */
  4. var Suite = require('../suite')
  5. , Test = require('../test')
  6. , utils = require('../utils');
  7. /**
  8. * BDD-style interface:
  9. *
  10. * describe('Array', function(){
  11. * describe('#indexOf()', function(){
  12. * it('should return -1 when not present', function(){
  13. *
  14. * });
  15. *
  16. * it('should return the index when present', function(){
  17. *
  18. * });
  19. * });
  20. * });
  21. *
  22. */
  23. module.exports = function(suite){
  24. var suites = [suite];
  25. suite.on('pre-require', function(context, file, mocha){
  26. /**
  27. * Execute before running tests.
  28. */
  29. context.before = function(fn){
  30. suites[0].beforeAll(fn);
  31. };
  32. /**
  33. * Execute after running tests.
  34. */
  35. context.after = function(fn){
  36. suites[0].afterAll(fn);
  37. };
  38. /**
  39. * Execute before each test case.
  40. */
  41. context.beforeEach = function(fn){
  42. suites[0].beforeEach(fn);
  43. };
  44. /**
  45. * Execute after each test case.
  46. */
  47. context.afterEach = function(fn){
  48. suites[0].afterEach(fn);
  49. };
  50. /**
  51. * Describe a "suite" with the given `title`
  52. * and callback `fn` containing nested suites
  53. * and/or tests.
  54. */
  55. context.describe = context.context = function(title, fn){
  56. var suite = Suite.create(suites[0], title);
  57. suites.unshift(suite);
  58. fn.call(suite);
  59. suites.shift();
  60. return suite;
  61. };
  62. /**
  63. * Pending describe.
  64. */
  65. context.xdescribe =
  66. context.xcontext =
  67. context.describe.skip = function(title, fn){
  68. var suite = Suite.create(suites[0], title);
  69. suite.pending = true;
  70. suites.unshift(suite);
  71. fn.call(suite);
  72. suites.shift();
  73. };
  74. /**
  75. * Exclusive suite.
  76. */
  77. context.describe.only = function(title, fn){
  78. var suite = context.describe(title, fn);
  79. mocha.grep(suite.fullTitle());
  80. };
  81. /**
  82. * Describe a specification or test-case
  83. * with the given `title` and callback `fn`
  84. * acting as a thunk.
  85. */
  86. context.it = context.specify = function(title, fn){
  87. var suite = suites[0];
  88. if (suite.pending) var fn = null;
  89. var test = new Test(title, fn);
  90. suite.addTest(test);
  91. return test;
  92. };
  93. /**
  94. * Exclusive test-case.
  95. */
  96. context.it.only = function(title, fn){
  97. var test = context.it(title, fn);
  98. var reString = '^' + utils.escapeRegexp(test.fullTitle()) + '$';
  99. mocha.grep(new RegExp(reString));
  100. };
  101. /**
  102. * Pending test case.
  103. */
  104. context.xit =
  105. context.xspecify =
  106. context.it.skip = function(title){
  107. context.it(title);
  108. };
  109. });
  110. };