common.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. 'use strict';
  2. var Suite = require('../suite');
  3. /**
  4. * Functions common to more than one interface.
  5. *
  6. * @param {Suite[]} suites
  7. * @param {Context} context
  8. * @param {Mocha} mocha
  9. * @return {Object} An object containing common functions.
  10. */
  11. module.exports = function(suites, context, mocha) {
  12. return {
  13. /**
  14. * This is only present if flag --delay is passed into Mocha. It triggers
  15. * root suite execution.
  16. *
  17. * @param {Suite} suite The root suite.
  18. * @return {Function} A function which runs the root suite
  19. */
  20. runWithSuite: function runWithSuite(suite) {
  21. return function run() {
  22. suite.run();
  23. };
  24. },
  25. /**
  26. * Execute before running tests.
  27. *
  28. * @param {string} name
  29. * @param {Function} fn
  30. */
  31. before: function(name, fn) {
  32. suites[0].beforeAll(name, fn);
  33. },
  34. /**
  35. * Execute after running tests.
  36. *
  37. * @param {string} name
  38. * @param {Function} fn
  39. */
  40. after: function(name, fn) {
  41. suites[0].afterAll(name, fn);
  42. },
  43. /**
  44. * Execute before each test case.
  45. *
  46. * @param {string} name
  47. * @param {Function} fn
  48. */
  49. beforeEach: function(name, fn) {
  50. suites[0].beforeEach(name, fn);
  51. },
  52. /**
  53. * Execute after each test case.
  54. *
  55. * @param {string} name
  56. * @param {Function} fn
  57. */
  58. afterEach: function(name, fn) {
  59. suites[0].afterEach(name, fn);
  60. },
  61. suite: {
  62. /**
  63. * Create an exclusive Suite; convenience function
  64. * See docstring for create() below.
  65. *
  66. * @param {Object} opts
  67. * @returns {Suite}
  68. */
  69. only: function only(opts) {
  70. opts.isOnly = true;
  71. return this.create(opts);
  72. },
  73. /**
  74. * Create a Suite, but skip it; convenience function
  75. * See docstring for create() below.
  76. *
  77. * @param {Object} opts
  78. * @returns {Suite}
  79. */
  80. skip: function skip(opts) {
  81. opts.pending = true;
  82. return this.create(opts);
  83. },
  84. /**
  85. * Creates a suite.
  86. * @param {Object} opts Options
  87. * @param {string} opts.title Title of Suite
  88. * @param {Function} [opts.fn] Suite Function (not always applicable)
  89. * @param {boolean} [opts.pending] Is Suite pending?
  90. * @param {string} [opts.file] Filepath where this Suite resides
  91. * @param {boolean} [opts.isOnly] Is Suite exclusive?
  92. * @returns {Suite}
  93. */
  94. create: function create(opts) {
  95. var suite = Suite.create(suites[0], opts.title);
  96. suite.pending = Boolean(opts.pending);
  97. suite.file = opts.file;
  98. suites.unshift(suite);
  99. if (opts.isOnly) {
  100. suite.parent._onlySuites = suite.parent._onlySuites.concat(suite);
  101. }
  102. if (typeof opts.fn === 'function') {
  103. opts.fn.call(suite);
  104. suites.shift();
  105. } else if (typeof opts.fn === 'undefined' && !suite.pending) {
  106. throw new Error(
  107. 'Suite "' +
  108. suite.fullTitle() +
  109. '" was defined but no callback was supplied. Supply a callback or explicitly skip the suite.'
  110. );
  111. } else if (!opts.fn && suite.pending) {
  112. suites.shift();
  113. }
  114. return suite;
  115. }
  116. },
  117. test: {
  118. /**
  119. * Exclusive test-case.
  120. *
  121. * @param {Object} mocha
  122. * @param {Function} test
  123. * @returns {*}
  124. */
  125. only: function(mocha, test) {
  126. test.parent._onlyTests = test.parent._onlyTests.concat(test);
  127. return test;
  128. },
  129. /**
  130. * Pending test case.
  131. *
  132. * @param {string} title
  133. */
  134. skip: function(title) {
  135. context.test(title);
  136. },
  137. /**
  138. * Number of retry attempts
  139. *
  140. * @param {number} n
  141. */
  142. retries: function(n) {
  143. context.retries(n);
  144. }
  145. }
  146. };
  147. };