tdd.js 2.2 KB

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