context.js 1.8 KB

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