bdd.js 2.6 KB

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