common.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. mocha.options.hasOnly = true;
  71. opts.isOnly = true;
  72. return this.create(opts);
  73. },
  74. /**
  75. * Create a Suite, but skip it; convenience function
  76. * See docstring for create() below.
  77. *
  78. * @param {Object} opts
  79. * @returns {Suite}
  80. */
  81. skip: function skip (opts) {
  82. opts.pending = true;
  83. return this.create(opts);
  84. },
  85. /**
  86. * Creates a suite.
  87. * @param {Object} opts Options
  88. * @param {string} opts.title Title of Suite
  89. * @param {Function} [opts.fn] Suite Function (not always applicable)
  90. * @param {boolean} [opts.pending] Is Suite pending?
  91. * @param {string} [opts.file] Filepath where this Suite resides
  92. * @param {boolean} [opts.isOnly] Is Suite exclusive?
  93. * @returns {Suite}
  94. */
  95. create: function create (opts) {
  96. var suite = Suite.create(suites[0], opts.title);
  97. suite.pending = Boolean(opts.pending);
  98. suite.file = opts.file;
  99. suites.unshift(suite);
  100. if (opts.isOnly) {
  101. suite.parent._onlySuites = suite.parent._onlySuites.concat(suite);
  102. mocha.options.hasOnly = true;
  103. }
  104. if (typeof opts.fn === 'function') {
  105. opts.fn.call(suite);
  106. suites.shift();
  107. } else if (typeof opts.fn === 'undefined' && !suite.pending) {
  108. throw new Error('Suite "' + suite.fullTitle() + '" was defined but no callback was supplied. Supply a callback or explicitly skip the suite.');
  109. }
  110. return suite;
  111. }
  112. },
  113. test: {
  114. /**
  115. * Exclusive test-case.
  116. *
  117. * @param {Object} mocha
  118. * @param {Function} test
  119. * @returns {*}
  120. */
  121. only: function (mocha, test) {
  122. test.parent._onlyTests = test.parent._onlyTests.concat(test);
  123. mocha.options.hasOnly = true;
  124. return test;
  125. },
  126. /**
  127. * Pending test case.
  128. *
  129. * @param {string} title
  130. */
  131. skip: function (title) {
  132. context.test(title);
  133. },
  134. /**
  135. * Number of retry attempts
  136. *
  137. * @param {number} n
  138. */
  139. retries: function (n) {
  140. context.retries(n);
  141. }
  142. }
  143. };
  144. };