tdd.js 2.2 KB

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