tdd.js 2.4 KB

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