tdd.js 2.6 KB

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