All files / lib/internal/test_runner test.js

99.71% Statements 691/693
97.02% Branches 196/202
97.77% Functions 44/45
99.71% Lines 691/693

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 69465x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 527x 527x 515x 515x 12x 5x 5x 5x 5x 12x 527x 65x 274x 274x 274x 274x 274x 274x 274x 274x 95x 95x 274x 274x 10x 10x 274x 274x 3x 3x 274x 274x 5x 5x 274x 274x 4x 4x 274x 274x 4x 4x 274x 274x 57x 57x 57x 57x 57x 274x 274x 3x 3x 274x 274x 3x 3x 274x 65x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 154x 154x 624x 624x 77x 77x 624x 624x 83x 83x 624x 624x 83x 83x 83x 83x 83x 83x 83x 83x 606x 541x 541x 541x 541x 541x 541x 541x 541x 541x 541x 541x 541x 624x 624x 624x 12x 12x 12x 624x 624x 27x 24x 27x 3x 3x 27x 624x 624x 585x 585x 624x 611x 624x 24x 24x 24x 599x 624x 13x 13x 599x 624x 31x 31x 599x 599x 599x 599x 599x 599x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 624x 1029x 1029x 624x 624x 286x 286x 624x 624x 541x 286x 286x 284x 284x 541x 624x 624x 541x 541x 624x 624x 1055x 1055x 1055x 1055x 632x 632x 632x 632x 632x 632x 94x 94x 538x 538x 538x 538x 538x 632x 632x 632x 24x 24x 514x 632x 64x 64x 514x 514x 514x 514x 514x 1055x 624x 624x 541x 12x 537x 43x 43x 529x 370x 370x 541x 541x 412x 412x 541x 541x 541x 541x 541x 541x 541x 2x 2x 2x 2x 541x 541x 541x 541x 127x 127x 515x 541x 2x 2x 2x 2x 2x 2x 2x 2x 515x 515x 515x 541x 624x 624x 9x 624x 624x 624x 49x 12x 12x 37x 49x 20x 20x 20x 20x 49x 49x 49x 49x 49x 624x 624x 18x 18x 18x 18x 18x 18x 624x 624x 131x 9x 9x 122x 122x 122x 122x 131x 624x 624x 437x 16x 16x 421x 421x 421x 437x 624x 624x 4x 4x 4x 624x 624x 4x 4x 4x 624x 624x 16x 16x 624x 624x 509x 509x 509x 509x 286x 286x 286x 286x 286x 286x 223x 223x 509x 624x 624x 949x 21x 21x 949x 3x 3x 3x 949x 624x 624x 274x 274x 274x 624x 624x 125x 125x 125x 32x 32x 32x 10x 10x 125x 125x 10x 10x 10x 10x 125x 624x 624x 490x 458x 458x 490x 490x 490x 8x 8x 8x 482x 482x 482x 490x 13x 9x 478x 478x 478x 478x 490x 31x 31x 31x 31x 31x 31x 31x 3x 3x 3x 3x 3x 31x 24x 17x 490x 447x 447x 447x 391x 408x 482x 15x 15x 15x 393x 490x 13x 9x 389x 389x 486x 74x 21x 5x 21x 16x 16x 74x 53x 53x 74x 463x 463x 463x 463x 490x 624x 624x 635x 635x 635x 635x 635x 24x 24x 635x 635x 635x 635x 635x 515x 515x 515x 32x 32x 32x 515x 515x 117x 117x 515x 635x 635x 11x 11x 11x 11x 11x 635x 635x 635x 635x 541x 541x 541x 541x 541x 635x 624x 624x 624x 180x 180x 624x 624x 624x 624x 514x 514x 514x 514x 514x 514x 514x 514x 514x 514x 514x 514x 64x 499x 450x 450x 514x 514x 514x 514x 514x 624x 624x 514x 514x 514x 514x 514x 514x 35x 514x 10x 10x 514x 514x 398x 513x 116x 116x 514x 514x 514x 514x 3x 3x 514x 624x 65x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 32x 32x 32x 18x 32x 32x 18x 65x 65x 65x 65x 176x 176x 65x 65x 65x 51x 51x 51x 51x 51x 51x 51x 51x 2x 51x 51x 1x 1x 51x 51x 51x 65x 65x 101x 101x 65x 65x 51x 51x 51x 51x 51x 51x 1x 1x 1x 1x 50x 50x 50x 50x 49x 51x 51x 51x 51x 49x 48x 51x 2x 2x 2x     2x 50x 50x 51x 65x 65x 65x  
'use strict';
const {
  ArrayPrototypePush,
  ArrayPrototypeReduce,
  ArrayPrototypeShift,
  ArrayPrototypeSlice,
  ArrayPrototypeUnshift,
  FunctionPrototype,
  MathMax,
  Number,
  ObjectSeal,
  PromisePrototypeThen,
  PromiseResolve,
  ReflectApply,
  SafeMap,
  SafePromiseAll,
  SafePromiseRace,
  Symbol,
} = primordials;
const { AsyncResource } = require('async_hooks');
const { once } = require('events');
const { AbortController } = require('internal/abort_controller');
const {
  codes: {
    ERR_INVALID_ARG_TYPE,
    ERR_TEST_FAILURE,
  },
  AbortError,
} = require('internal/errors');
const { getOptionValue } = require('internal/options');
const { TapStream } = require('internal/test_runner/tap_stream');
const { createDeferredCallback, isTestFailureError } = require('internal/test_runner/utils');
const {
  createDeferredPromise,
  kEmptyObject,
} = require('internal/util');
const { isPromise } = require('internal/util/types');
const {
  validateAbortSignal,
  validateNumber,
  validateOneOf,
  validateUint32,
} = require('internal/validators');
const { setTimeout } = require('timers/promises');
const { TIMEOUT_MAX } = require('internal/timers');
const { cpus } = require('os');
const { bigint: hrtime } = process.hrtime;
const kCallbackAndPromisePresent = 'callbackAndPromisePresent';
const kCancelledByParent = 'cancelledByParent';
const kParentAlreadyFinished = 'parentAlreadyFinished';
const kSubtestsFailed = 'subtestsFailed';
const kTestCodeFailure = 'testCodeFailure';
const kTestTimeoutFailure = 'testTimeoutFailure';
const kHookFailure = 'hookFailed';
const kDefaultIndent = '    ';
const kDefaultTimeout = null;
const noop = FunctionPrototype;
const isTestRunner = getOptionValue('--test');
const testOnlyFlag = !isTestRunner && getOptionValue('--test-only');
// TODO(cjihrig): Use uv_available_parallelism() once it lands.
const rootConcurrency = isTestRunner ? MathMax(cpus().length - 1, 1) : 1;
const kShouldAbort = Symbol('kShouldAbort');
const kRunHook = Symbol('kRunHook');
const kHookNames = ObjectSeal(['before', 'after', 'beforeEach', 'afterEach']);
 
 
function stopTest(timeout, signal) {
  if (timeout === kDefaultTimeout) {
    return once(signal, 'abort');
  }
  return PromisePrototypeThen(setTimeout(timeout, null, { ref: false, signal }), () => {
    throw new ERR_TEST_FAILURE(
      `test timed out after ${timeout}ms`,
      kTestTimeoutFailure
    );
  });
}
 
class TestContext {
  #test;
 
  constructor(test) {
    this.#test = test;
  }
 
  get signal() {
    return this.#test.signal;
  }
 
  get name() {
    return this.#test.name;
  }
 
  diagnostic(message) {
    this.#test.diagnostic(message);
  }
 
  runOnly(value) {
    this.#test.runOnlySubtests = !!value;
  }
 
  skip(message) {
    this.#test.skip(message);
  }
 
  todo(message) {
    this.#test.todo(message);
  }
 
  test(name, options, fn) {
    // eslint-disable-next-line no-use-before-define
    const subtest = this.#test.createSubtest(Test, name, options, fn);
 
    return subtest.start();
  }
 
  beforeEach(fn, options) {
    this.#test.createHook('beforeEach', fn, options);
  }
 
  afterEach(fn, options) {
    this.#test.createHook('afterEach', fn, options);
  }
}
 
class Test extends AsyncResource {
  #abortController;
  #outerSignal;
 
  constructor(options) {
    super('Test');
 
    let { fn, name, parent, skip } = options;
    const { concurrency, only, timeout, todo, signal } = options;
 
    if (typeof fn !== 'function') {
      fn = noop;
    }
 
    if (typeof name !== 'string' || name === '') {
      name = fn.name || '<anonymous>';
    }
 
    if (!(parent instanceof Test)) {
      parent = null;
    }
 
    if (parent === null) {
      this.concurrency = rootConcurrency;
      this.indent = '';
      this.indentString = kDefaultIndent;
      this.only = testOnlyFlag;
      this.reporter = new TapStream();
      this.runOnlySubtests = this.only;
      this.testNumber = 0;
      this.timeout = kDefaultTimeout;
    } else {
      const indent = parent.parent === null ? parent.indent :
        parent.indent + parent.indentString;
 
      this.concurrency = parent.concurrency;
      this.indent = indent;
      this.indentString = parent.indentString;
      this.only = only ?? !parent.runOnlySubtests;
      this.reporter = parent.reporter;
      this.runOnlySubtests = !this.only;
      this.testNumber = parent.subtests.length + 1;
      this.timeout = parent.timeout;
    }
 
    switch (typeof concurrency) {
      case 'number':
        validateUint32(concurrency, 'options.concurrency', 1);
        this.concurrency = concurrency;
        break;
 
      case 'boolean':
        if (concurrency) {
          this.concurrency = isTestRunner ? MathMax(cpus().length - 1, 1) : Infinity;
        } else {
          this.concurrency = 1;
        }
        break;
 
      default:
        if (concurrency != null)
          throw new ERR_INVALID_ARG_TYPE('options.concurrency', ['boolean', 'number'], concurrency);
    }
 
    if (timeout != null && timeout !== Infinity) {
      validateNumber(timeout, 'options.timeout', 0, TIMEOUT_MAX);
      this.timeout = timeout;
    }
 
    if (testOnlyFlag && !this.only) {
      skip = '\'only\' option not set';
    }
 
    if (skip) {
      fn = noop;
    }
 
    this.#abortController = new AbortController();
    this.#outerSignal = signal;
    this.signal = this.#abortController.signal;
 
    validateAbortSignal(signal, 'options.signal');
    this.#outerSignal?.addEventListener('abort', this.#abortHandler);
 
 
    this.fn = fn;
    this.name = name;
    this.parent = parent;
    this.cancelled = false;
    this.skipped = !!skip;
    this.isTodo = !!todo;
    this.startTime = null;
    this.endTime = null;
    this.passed = false;
    this.error = null;
    this.diagnostics = [];
    this.message = typeof skip === 'string' ? skip :
      typeof todo === 'string' ? todo : null;
    this.activeSubtests = 0;
    this.pendingSubtests = [];
    this.readySubtests = new SafeMap();
    this.subtests = [];
    this.hooks = {
      before: [],
      after: [],
      beforeEach: [],
      afterEach: [],
    };
    this.waitingOn = 0;
    this.finished = false;
  }
 
  hasConcurrency() {
    return this.concurrency > this.activeSubtests;
  }
 
  addPendingSubtest(deferred) {
    this.pendingSubtests.push(deferred);
  }
 
  async processPendingSubtests() {
    while (this.pendingSubtests.length > 0 && this.hasConcurrency()) {
      const deferred = ArrayPrototypeShift(this.pendingSubtests);
      await deferred.test.run();
      deferred.resolve();
    }
  }
 
  addReadySubtest(subtest) {
    this.readySubtests.set(subtest.testNumber, subtest);
  }
 
  processReadySubtestRange(canSend) {
    const start = this.waitingOn;
    const end = start + this.readySubtests.size;
 
    for (let i = start; i < end; i++) {
      const subtest = this.readySubtests.get(i);
 
      // Check if the specified subtest is in the map. If it is not, return
      // early to avoid trying to process any more tests since they would be
      // out of order.
      if (subtest === undefined) {
        return;
      }
 
      // Call isClearToSend() in the loop so that it is:
      // - Only called if there are results to report in the correct order.
      // - Guaranteed to only be called a maximum of once per call to
      //   processReadySubtestRange().
      canSend = canSend || this.isClearToSend();
 
      if (!canSend) {
        return;
      }
 
      if (i === 1 && this.parent !== null) {
        this.reporter.subtest(this.indent, this.name);
      }
 
      // Report the subtest's results and remove it from the ready map.
      subtest.finalize();
      this.readySubtests.delete(i);
    }
  }
 
  createSubtest(Factory, name, options, fn, overrides) {
    if (typeof name === 'function') {
      fn = name;
    } else if (name !== null && typeof name === 'object') {
      fn = options;
      options = name;
    } else if (typeof options === 'function') {
      fn = options;
    }
 
    if (options === null || typeof options !== 'object') {
      options = kEmptyObject;
    }
 
    let parent = this;
 
    // If this test has already ended, attach this test to the root test so
    // that the error can be properly reported.
    const preventAddingSubtests = this.finished || this.buildPhaseFinished;
    if (preventAddingSubtests) {
      while (parent.parent !== null) {
        parent = parent.parent;
      }
    }
 
    const test = new Factory({ __proto__: null, fn, name, parent, ...options, ...overrides });
 
    if (parent.waitingOn === 0) {
      parent.waitingOn = test.testNumber;
    }
 
    if (preventAddingSubtests) {
      test.startTime = test.startTime || hrtime();
      test.fail(
        new ERR_TEST_FAILURE(
          'test could not be started because its parent finished',
          kParentAlreadyFinished
        )
      );
    }
 
    ArrayPrototypePush(parent.subtests, test);
    return test;
  }
 
  #abortHandler = () => {
    this.cancel(this.#outerSignal?.reason || new AbortError('The test was aborted'));
  };
 
  cancel(error) {
    if (this.endTime !== null) {
      return;
    }
 
    this.fail(error ||
      new ERR_TEST_FAILURE(
        'test did not finish before its parent and was cancelled',
        kCancelledByParent
      )
    );
    this.startTime = this.startTime || this.endTime; // If a test was canceled before it was started, e.g inside a hook
    this.cancelled = true;
    this.#abortController.abort();
  }
 
  createHook(name, fn, options) {
    validateOneOf(name, 'hook name', kHookNames);
    // eslint-disable-next-line no-use-before-define
    const hook = new TestHook(fn, options);
    ArrayPrototypePush(this.hooks[name], hook);
    return hook;
  }
 
  fail(err) {
    if (this.error !== null) {
      return;
    }
 
    this.endTime = hrtime();
    this.passed = false;
    this.error = err;
  }
 
  pass() {
    if (this.endTime !== null) {
      return;
    }
 
    this.endTime = hrtime();
    this.passed = true;
  }
 
  skip(message) {
    this.skipped = true;
    this.message = message;
  }
 
  todo(message) {
    this.isTodo = true;
    this.message = message;
  }
 
  diagnostic(message) {
    ArrayPrototypePush(this.diagnostics, message);
  }
 
  start() {
    // If there is enough available concurrency to run the test now, then do
    // it. Otherwise, return a Promise to the caller and mark the test as
    // pending for later execution.
    if (!this.parent.hasConcurrency()) {
      const deferred = createDeferredPromise();
 
      deferred.test = this;
      this.parent.addPendingSubtest(deferred);
      return deferred.promise;
    }
 
    return this.run();
  }
 
  [kShouldAbort]() {
    if (this.signal.aborted) {
      return true;
    }
    if (this.#outerSignal?.aborted) {
      this.cancel(this.#outerSignal.reason || new AbortError('The test was aborted'));
      return true;
    }
  }
 
  getRunArgs() {
    const ctx = new TestContext(this);
    return { ctx, args: [ctx] };
  }
 
  async [kRunHook](hook, args) {
    validateOneOf(hook, 'hook name', kHookNames);
    try {
      await ArrayPrototypeReduce(this.hooks[hook], async (prev, hook) => {
        await prev;
        await hook.run(args);
        if (hook.error) {
          throw hook.error;
        }
      }, PromiseResolve());
    } catch (err) {
      const error = new ERR_TEST_FAILURE(`failed running ${hook} hook`, kHookFailure);
      error.cause = isTestFailureError(err) ? err.cause : err;
      throw error;
    }
  }
 
  async run() {
    if (this.parent !== null) {
      this.parent.activeSubtests++;
    }
    this.startTime = hrtime();
 
    if (this[kShouldAbort]()) {
      this.postRun();
      return;
    }
 
    try {
      const { args, ctx } = this.getRunArgs();
      if (this.parent?.hooks.beforeEach.length > 0) {
        await this.parent[kRunHook]('beforeEach', { args, ctx });
      }
      const stopPromise = stopTest(this.timeout, this.signal);
      const runArgs = ArrayPrototypeSlice(args);
      ArrayPrototypeUnshift(runArgs, this.fn, ctx);
 
      if (this.fn.length === runArgs.length - 1) {
        // This test is using legacy Node.js error first callbacks.
        const { promise, cb } = createDeferredCallback();
 
        ArrayPrototypePush(runArgs, cb);
        const ret = ReflectApply(this.runInAsyncScope, this, runArgs);
 
        if (isPromise(ret)) {
          this.fail(new ERR_TEST_FAILURE(
            'passed a callback but also returned a Promise',
            kCallbackAndPromisePresent
          ));
          await SafePromiseRace([ret, stopPromise]);
        } else {
          await SafePromiseRace([PromiseResolve(promise), stopPromise]);
        }
      } else {
        // This test is synchronous or using Promises.
        const promise = ReflectApply(this.runInAsyncScope, this, runArgs);
        await SafePromiseRace([PromiseResolve(promise), stopPromise]);
      }
 
      if (this[kShouldAbort]()) {
        this.postRun();
        return;
      }
 
      if (this.parent?.hooks.afterEach.length > 0) {
        await this.parent[kRunHook]('afterEach', { args, ctx });
      }
 
      this.pass();
    } catch (err) {
      if (isTestFailureError(err)) {
        if (err.failureType === kTestTimeoutFailure) {
          this.cancel(err);
        } else {
          this.fail(err);
        }
      } else {
        this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure));
      }
    }
 
    // Clean up the test. Then, try to report the results and execute any
    // tests that were pending due to available concurrency.
    this.postRun();
  }
 
  postRun() {
    let failedSubtests = 0;
 
    // If the test was failed before it even started, then the end time will
    // be earlier than the start time. Correct that here.
    if (this.endTime < this.startTime) {
      this.endTime = hrtime();
    }
    this.startTime ??= this.endTime;
 
    // The test has run, so recursively cancel any outstanding subtests and
    // mark this test as failed if any subtests failed.
    for (let i = 0; i < this.subtests.length; i++) {
      const subtest = this.subtests[i];
 
      if (!subtest.finished) {
        subtest.cancel();
        subtest.postRun();
      }
 
      if (!subtest.passed) {
        failedSubtests++;
      }
    }
 
    if (this.passed && failedSubtests > 0) {
      const subtestString = `subtest${failedSubtests > 1 ? 's' : ''}`;
      const msg = `${failedSubtests} ${subtestString} failed`;
 
      this.fail(new ERR_TEST_FAILURE(msg, kSubtestsFailed));
    }
 
    this.#outerSignal?.removeEventListener('abort', this.#abortHandler);
 
    if (this.parent !== null) {
      this.parent.activeSubtests--;
      this.parent.addReadySubtest(this);
      this.parent.processReadySubtestRange(false);
      this.parent.processPendingSubtests();
    }
  }
 
  isClearToSend() {
    return this.parent === null ||
      (
        this.parent.waitingOn === this.testNumber && this.parent.isClearToSend()
      );
  }
 
  finalize() {
    // By the time this function is called, the following can be relied on:
    // - The current test has completed or been cancelled.
    // - All of this test's subtests have completed or been cancelled.
    // - It is the current test's turn to report its results.
 
    // Report any subtests that have not been reported yet. Since all of the
    // subtests have finished, it's safe to pass true to
    // processReadySubtestRange(), which will finalize all remaining subtests.
    this.processReadySubtestRange(true);
 
    // Output this test's results and update the parent's waiting counter.
    if (this.subtests.length > 0) {
      this.reporter.plan(this.subtests[0].indent, this.subtests.length);
    } else {
      this.reporter.subtest(this.indent, this.name);
    }
 
    this.report();
    this.parent.waitingOn++;
    this.finished = true;
  }
 
  report() {
    // Duration is recorded in BigInt nanoseconds. Convert to seconds.
    const duration = Number(this.endTime - this.startTime) / 1_000_000_000;
    const message = `- ${this.name}`;
    let directive;
 
    if (this.skipped) {
      directive = this.reporter.getSkip(this.message);
    } else if (this.isTodo) {
      directive = this.reporter.getTodo(this.message);
    }
 
    if (this.passed) {
      this.reporter.ok(this.indent, this.testNumber, message, directive);
    } else {
      this.reporter.fail(this.indent, this.testNumber, message, directive);
    }
 
    this.reporter.details(this.indent, duration, this.error);
 
    for (let i = 0; i < this.diagnostics.length; i++) {
      this.reporter.diagnostic(this.indent, this.diagnostics[i]);
    }
  }
}
 
class TestHook extends Test {
  #args;
  constructor(fn, options) {
    if (options === null || typeof options !== 'object') {
      options = kEmptyObject;
    }
    const { timeout, signal } = options;
    super({ __proto__: null, fn, timeout, signal });
  }
  run(args) {
    this.#args = args;
    return super.run();
  }
  getRunArgs() {
    return this.#args;
  }
}
 
class ItTest extends Test {
  constructor(opt) { super(opt); } // eslint-disable-line no-useless-constructor
  getRunArgs() {
    return { ctx: { signal: this.signal, name: this.name }, args: [] };
  }
}
class Suite extends Test {
  constructor(options) {
    super(options);
 
    try {
      const { ctx, args } = this.getRunArgs();
      this.buildSuite = PromisePrototypeThen(
        PromiseResolve(this.runInAsyncScope(this.fn, ctx, args)),
        undefined,
        (err) => {
          this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure));
        });
    } catch (err) {
      this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure));
    }
    this.fn = () => {};
    this.buildPhaseFinished = true;
  }
 
  getRunArgs() {
    return { ctx: { signal: this.signal, name: this.name }, args: [] };
  }
 
  async run() {
    try {
      this.parent.activeSubtests++;
      await this.buildSuite;
      this.startTime = hrtime();
 
      if (this[kShouldAbort]()) {
        this.subtests = [];
        this.postRun();
        return;
      }
 
 
      const hookArgs = this.getRunArgs();
      await this[kRunHook]('before', hookArgs);
      const stopPromise = stopTest(this.timeout, this.signal);
      const subtests = this.skipped || this.error ? [] : this.subtests;
      const promise = SafePromiseAll(subtests, (subtests) => subtests.start());
 
      await SafePromiseRace([promise, stopPromise]);
      await this[kRunHook]('after', hookArgs);
      this.pass();
    } catch (err) {
      if (isTestFailureError(err)) {
        this.fail(err);
      } else {
        this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure));
      }
    }
 
    this.postRun();
  }
}
 
module.exports = { kDefaultIndent, kSubtestsFailed, kTestCodeFailure, Test, Suite, ItTest };