tdd.js 2.3 KB

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