Repository: nodejs/node
File: lib/internal/test_runner/test.js (JavaScript)

Code before:
    if (name === 'afterEach') {
      // afterEach hooks for the current test should run in the order that they
      // are created. However, the current test's afterEach hooks should run
      // prior to any ancestor afterEach hooks.
      ArrayPrototypeSplice(this.hooks[name], this.hooks.ownAfterEachCount, 0, hook);
      this.hooks.ownAfterEachCount++;
    } else {
      ArrayPrototypePush(this.hooks[name], hook);
    }
  }

  fail(err) {
    if (this.error !== null) {
      return;
    }

    if (this.expectFailure) {
      if (typeof this.expectFailure === 'object' &&
          this.expectFailure.match !== undefined) {
        const { match: validation } = this.expectFailure;
        try {
          const { throws } = assert;
          const errorToCheck = (err?.code === 'ERR_TEST_FAILURE' &&
            err?.failureType === kTestCodeFailure &&
            err.cause) ?
            err.cause : err;
          throws(() => { throw errorToCheck; }, validation);
        } catch (e) {
          this.passed = false;
          this.error = new ERR_TEST_FAILURE(
            'The test failed, but the error did not match the expected validation',
            kTestCodeFailure,
          );
          this.error.cause = e;
          return;
        }
      }
      this.passed = true;
    } else {
      this.passed = false;
    }

    this.error = err;
  }

  pass() {
    if (this.error == null && this.expectFailure && !this.skipped) {
      this.passed = false;
      this.error = new ERR_TEST_FAILURE(
        'test was expected to fail but passed',
        kExpectedFailure,

Review comment (bug):
nit: this is a bit difficult to read


```suggestion
          const errorToCheck = (
            err?.code === 'ERR_TEST_FAILURE' &&
            err?.failureType === kTestCodeFailure &&
            err.cause
          ) ?
            err.cause :
            err;
```

Code after:
    }
    if (name === 'afterEach') {
      // afterEach hooks for the current test should run in the order that they
      // are created. However, the current test's afterEach hooks should run
      // prior to any ancestor afterEach hooks.
      ArrayPrototypeSplice(this.hooks[name], this.hooks.ownAfterEachCount, 0, hook);
      this.hooks.ownAfterEachCount++;
    } else {
      ArrayPrototypePush(this.hooks[name], hook);
    }
  }

  fail(err) {
    if (this.error !== null) {
      return;
    }

    if (this.expectFailure) {
      if (typeof this.expectFailure === 'object' &&
          this.expectFailure.match !== undefined) {
        const { match: validation } = this.expectFailure;
        try {
          const errorToCheck = (
            err?.code === 'ERR_TEST_FAILURE' &&
            err?.failureType === kTestCodeFailure &&
            err.cause
          ) ?
            err.cause :
            err;
          // eslint-disable-next-line no-restricted-syntax
          assert.throws(() => { throw errorToCheck; }, validation);
        } catch (e) {
          this.passed = false;
          this.error = new ERR_TEST_FAILURE(
            'The test failed, but the error did not match the expected validation',
            kTestCodeFailure,
          );
          this.error.cause = e;
          return;
        }
      }
      this.passed = true;
    } else {
      this.passed = false;
    }

    this.error = err;
  }

  pass() {
    if (this.error == null && this.expectFailure && !this.skipped) {