bdd.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. 'use strict';
  2. var Test = require('../test');
  3. /**
  4. * BDD-style interface:
  5. *
  6. * describe('Array', function() {
  7. * describe('#indexOf()', function() {
  8. * it('should return -1 when not present', function() {
  9. * // ...
  10. * });
  11. *
  12. * it('should return the index when present', function() {
  13. * // ...
  14. * });
  15. * });
  16. * });
  17. *
  18. * @param {Suite} suite Root suite.
  19. */
  20. module.exports = function bddInterface(suite) {
  21. var suites = [suite];
  22. suite.on('pre-require', function(context, file, mocha) {
  23. var common = require('./common')(suites, context, mocha);
  24. context.before = common.before;
  25. context.after = common.after;
  26. context.beforeEach = common.beforeEach;
  27. context.afterEach = common.afterEach;
  28. context.run = mocha.options.delay && common.runWithSuite(suite);
  29. /**
  30. * Describe a "suite" with the given `title`
  31. * and callback `fn` containing nested suites
  32. * and/or tests.
  33. */
  34. context.describe = context.context = function(title, fn) {
  35. return common.suite.create({
  36. title: title,
  37. file: file,
  38. fn: fn
  39. });
  40. };
  41. /**
  42. * Pending describe.
  43. */
  44. context.xdescribe = context.xcontext = context.describe.skip = function(
  45. title,
  46. fn
  47. ) {
  48. return common.suite.skip({
  49. title: title,
  50. file: file,
  51. fn: fn
  52. });
  53. };
  54. /**
  55. * Exclusive suite.
  56. */
  57. context.describe.only = function(title, fn) {
  58. return common.suite.only({
  59. title: title,
  60. file: file,
  61. fn: fn
  62. });
  63. };
  64. /**
  65. * Describe a specification or test-case
  66. * with the given `title` and callback `fn`
  67. * acting as a thunk.
  68. */
  69. 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. return common.test.only(mocha, context.it(title, fn));
  84. };
  85. /**
  86. * Pending test case.
  87. */
  88. context.xit = context.xspecify = context.it.skip = function(title) {
  89. return context.it(title);
  90. };
  91. /**
  92. * Number of attempts to retry.
  93. */
  94. context.it.retries = function(n) {
  95. context.retries(n);
  96. };
  97. });
  98. };