bdd.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * Module dependencies.
  3. */
  4. var Suite = require('../suite');
  5. var Test = require('../test');
  6. var escapeRe = require('escape-string-regexp');
  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. * @param {Suite} suite Root suite.
  23. */
  24. module.exports = function(suite) {
  25. var suites = [suite];
  26. suite.on('pre-require', function(context, file, mocha) {
  27. var common = require('./common')(suites, context);
  28. context.before = common.before;
  29. context.after = common.after;
  30. context.beforeEach = common.beforeEach;
  31. context.afterEach = common.afterEach;
  32. context.run = mocha.options.delay && common.runWithSuite(suite);
  33. /**
  34. * Describe a "suite" with the given `title`
  35. * and callback `fn` containing nested suites
  36. * and/or tests.
  37. */
  38. context.describe = context.context = function(title, fn) {
  39. var suite = Suite.create(suites[0], title);
  40. suite.file = file;
  41. suites.unshift(suite);
  42. fn.call(suite);
  43. suites.shift();
  44. return suite;
  45. };
  46. /**
  47. * Pending describe.
  48. */
  49. context.xdescribe = context.xcontext = context.describe.skip = function(title, fn) {
  50. var suite = Suite.create(suites[0], title);
  51. suite.pending = true;
  52. suites.unshift(suite);
  53. fn.call(suite);
  54. suites.shift();
  55. };
  56. /**
  57. * Exclusive suite.
  58. */
  59. context.describe.only = function(title, fn) {
  60. var suite = context.describe(title, fn);
  61. mocha.grep(suite.fullTitle());
  62. return suite;
  63. };
  64. /**
  65. * Describe a specification or test-case
  66. * with the given `title` and callback `fn`
  67. * acting as a thunk.
  68. */
  69. var it = context.it = context.specify = function(title, fn) {
  70. var suite = suites[0];
  71. if (suite.isPending()) {
  72. fn = null;
  73. }
  74. var test = new Test(title, fn);
  75. test.file = file;
  76. suite.addTest(test);
  77. return test;
  78. };
  79. /**
  80. * Exclusive test-case.
  81. */
  82. context.it.only = function(title, fn) {
  83. var test = it(title, fn);
  84. var reString = '^' + escapeRe(test.fullTitle()) + '$';
  85. mocha.grep(new RegExp(reString));
  86. return test;
  87. };
  88. /**
  89. * Pending test case.
  90. */
  91. context.xit = context.xspecify = context.it.skip = function(title) {
  92. context.it(title);
  93. };
  94. /**
  95. * Number of attempts to retry.
  96. */
  97. context.it.retries = function(n) {
  98. context.retries(n);
  99. };
  100. });
  101. };