bdd.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict';
  2. /**
  3. * Module dependencies.
  4. */
  5. var Test = require('../test');
  6. /**
  7. * BDD-style interface:
  8. *
  9. * describe('Array', function() {
  10. * describe('#indexOf()', function() {
  11. * it('should return -1 when not present', function() {
  12. * // ...
  13. * });
  14. *
  15. * it('should return the index when present', function() {
  16. * // ...
  17. * });
  18. * });
  19. * });
  20. *
  21. * @param {Suite} suite Root suite.
  22. */
  23. module.exports = function (suite) {
  24. var suites = [suite];
  25. suite.on('pre-require', function (context, file, mocha) {
  26. var common = require('./common')(suites, context, mocha);
  27. context.before = common.before;
  28. context.after = common.after;
  29. context.beforeEach = common.beforeEach;
  30. context.afterEach = common.afterEach;
  31. context.run = mocha.options.delay && common.runWithSuite(suite);
  32. /**
  33. * Describe a "suite" with the given `title`
  34. * and callback `fn` containing nested suites
  35. * and/or tests.
  36. */
  37. context.describe = context.context = function (title, fn) {
  38. return common.suite.create({
  39. title: title,
  40. file: file,
  41. fn: fn
  42. });
  43. };
  44. /**
  45. * Pending describe.
  46. */
  47. context.xdescribe = context.xcontext = context.describe.skip = function (title, fn) {
  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. 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. };