test.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 'use strict';
  2. var Runnable = require('./runnable');
  3. var utils = require('./utils');
  4. var errors = require('./errors');
  5. var createInvalidArgumentTypeError = errors.createInvalidArgumentTypeError;
  6. var isString = utils.isString;
  7. const {MOCHA_ID_PROP_NAME} = utils.constants;
  8. module.exports = Test;
  9. /**
  10. * Initialize a new `Test` with the given `title` and callback `fn`.
  11. *
  12. * @public
  13. * @class
  14. * @extends Runnable
  15. * @param {String} title - Test title (required)
  16. * @param {Function} [fn] - Test callback. If omitted, the Test is considered "pending"
  17. */
  18. function Test(title, fn) {
  19. if (!isString(title)) {
  20. throw createInvalidArgumentTypeError(
  21. 'Test argument "title" should be a string. Received type "' +
  22. typeof title +
  23. '"',
  24. 'title',
  25. 'string'
  26. );
  27. }
  28. this.type = 'test';
  29. Runnable.call(this, title, fn);
  30. this.reset();
  31. }
  32. /**
  33. * Inherit from `Runnable.prototype`.
  34. */
  35. utils.inherits(Test, Runnable);
  36. /**
  37. * Resets the state initially or for a next run.
  38. */
  39. Test.prototype.reset = function () {
  40. Runnable.prototype.reset.call(this);
  41. this.pending = !this.fn;
  42. delete this.state;
  43. };
  44. /**
  45. * Set or get retried test
  46. *
  47. * @private
  48. */
  49. Test.prototype.retriedTest = function (n) {
  50. if (!arguments.length) {
  51. return this._retriedTest;
  52. }
  53. this._retriedTest = n;
  54. };
  55. /**
  56. * Add test to the list of tests marked `only`.
  57. *
  58. * @private
  59. */
  60. Test.prototype.markOnly = function () {
  61. this.parent.appendOnlyTest(this);
  62. };
  63. Test.prototype.clone = function () {
  64. var test = new Test(this.title, this.fn);
  65. test.timeout(this.timeout());
  66. test.slow(this.slow());
  67. test.retries(this.retries());
  68. test.currentRetry(this.currentRetry());
  69. test.retriedTest(this.retriedTest() || this);
  70. test.globals(this.globals());
  71. test.parent = this.parent;
  72. test.file = this.file;
  73. test.ctx = this.ctx;
  74. return test;
  75. };
  76. /**
  77. * Returns an minimal object suitable for transmission over IPC.
  78. * Functions are represented by keys beginning with `$$`.
  79. * @private
  80. * @returns {Object}
  81. */
  82. Test.prototype.serialize = function serialize() {
  83. return {
  84. $$currentRetry: this._currentRetry,
  85. $$fullTitle: this.fullTitle(),
  86. $$isPending: Boolean(this.pending),
  87. $$retriedTest: this._retriedTest || null,
  88. $$slow: this._slow,
  89. $$titlePath: this.titlePath(),
  90. body: this.body,
  91. duration: this.duration,
  92. err: this.err,
  93. parent: {
  94. $$fullTitle: this.parent.fullTitle(),
  95. [MOCHA_ID_PROP_NAME]: this.parent.id
  96. },
  97. speed: this.speed,
  98. state: this.state,
  99. title: this.title,
  100. type: this.type,
  101. file: this.file,
  102. [MOCHA_ID_PROP_NAME]: this.id
  103. };
  104. };