Documentation
    Preparing search index...
    • Validates that a function throws an error or rejects when executed.

      Expects the provided function to fail. If the function executes successfully without throwing an error or rejecting, this validator will throw an exception. Supports both synchronous and asynchronous functions.

      Processing cost: The task is invoked once and its settlement is classified with constant bookkeeping; no retry or repeated execution occurs.

      Reuse: Each invocation must evaluate its supplied task or request to observe current behavior; prior results cannot replace these effectful checks.

      Resource ownership: The returned promise retains invocation-local state until settlement. The callback owns its external resources, and this validator provides no cancellation or timeout.

      Type Parameters

      • T

      Parameters

      • title: string

        Descriptive title used in error messages when no error occurs

      • task: () => T

        The function that should throw an error or reject

      Returns T extends Promise<any> ? Promise<void> : void

      Void or Promise based on the input type

      // Synchronous error validation
      TestValidator.error("should reject invalid email", () =>
      validateEmail("invalid-email"),
      );

      // Asynchronous error validation
      await TestValidator.error(
      "should reject unauthorized access",
      async () => await api.functional.getSecretData(),
      );

      // Validate input validation
      TestValidator.error("should throw on empty string", () =>
      processRequiredInput(""),
      );

      Error when the task function does not throw an error or reject

      contracts/common.md#principled-implementation The task is run inside a try so that only its own throw counts, a promise is awaited and a rejection counts as the expected error, and a task that neither throws nor rejects raises the failure outside the try, so the failure cannot be swallowed.

      contracts/common.md#clear-and-simple-design One function with a synchronous and an asynchronous path selected by the returned value.

      contracts/common.md#prohibited-implementation-shortcuts Any error satisfies it, as documented, and no error type is special-cased.

      contracts/common.md#meaningful-documentation The comment states that an exception is required, the return type rule, and the thrown error, with examples.