context.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 'use strict';
  2. /**
  3. * Module dependencies.
  4. */
  5. var JSON = require('json3');
  6. /**
  7. * Expose `Context`.
  8. */
  9. module.exports = Context;
  10. /**
  11. * Initialize a new `Context`.
  12. *
  13. * @api private
  14. */
  15. function Context () {}
  16. /**
  17. * Set or get the context `Runnable` to `runnable`.
  18. *
  19. * @api private
  20. * @param {Runnable} runnable
  21. * @return {Context}
  22. */
  23. Context.prototype.runnable = function (runnable) {
  24. if (!arguments.length) {
  25. return this._runnable;
  26. }
  27. this.test = this._runnable = runnable;
  28. return this;
  29. };
  30. /**
  31. * Set test timeout `ms`.
  32. *
  33. * @api private
  34. * @param {number} ms
  35. * @return {Context} self
  36. */
  37. Context.prototype.timeout = function (ms) {
  38. if (!arguments.length) {
  39. return this.runnable().timeout();
  40. }
  41. this.runnable().timeout(ms);
  42. return this;
  43. };
  44. /**
  45. * Set test timeout `enabled`.
  46. *
  47. * @api private
  48. * @param {boolean} enabled
  49. * @return {Context} self
  50. */
  51. Context.prototype.enableTimeouts = function (enabled) {
  52. this.runnable().enableTimeouts(enabled);
  53. return this;
  54. };
  55. /**
  56. * Set test slowness threshold `ms`.
  57. *
  58. * @api private
  59. * @param {number} ms
  60. * @return {Context} self
  61. */
  62. Context.prototype.slow = function (ms) {
  63. this.runnable().slow(ms);
  64. return this;
  65. };
  66. /**
  67. * Mark a test as skipped.
  68. *
  69. * @api private
  70. * @return {Context} self
  71. */
  72. Context.prototype.skip = function () {
  73. this.runnable().skip();
  74. return this;
  75. };
  76. /**
  77. * Allow a number of retries on failed tests
  78. *
  79. * @api private
  80. * @param {number} n
  81. * @return {Context} self
  82. */
  83. Context.prototype.retries = function (n) {
  84. if (!arguments.length) {
  85. return this.runnable().retries();
  86. }
  87. this.runnable().retries(n);
  88. return this;
  89. };
  90. /**
  91. * Inspect the context void of `._runnable`.
  92. *
  93. * @api private
  94. * @return {string}
  95. */
  96. Context.prototype.inspect = function () {
  97. return JSON.stringify(this, function (key, val) {
  98. return key === 'runnable' || key === 'test' ? undefined : val;
  99. }, 2);
  100. };