bdd.js 2.5 KB

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