API reference@evolu/commonAssert › assertThrows

Call Signature

function assertThrows(
  run: () => unknown,
  assertThrown: (thrown: unknown) => void,
): void;

Defined in: packages/common/src/Assert.ts:372

Asserts that a function throws the expected value.

Expected values use assertEqual semantics. To perform several or specialized assertions, pass an assertion function that receives the thrown value and returns nothing. Returning a predicate result fails. Use assertThrowsSame for SameValue or reference identity and assertThrowsInstanceOf for a runtime type.

Because JavaScript permits throwing functions, a function second argument is always treated as an assertion function. Use assertThrowsSame to assert that a particular function was thrown.

Example

import { assertThrows } from "@evolu/common";

assertThrows(
  () => {
    // oxlint-disable-next-line eslint/no-throw-literal, typescript/only-throw-error -- Exercise arbitrary thrown values.
    throw { type: "ExpectedFailure", value: 42 };
  },
  { type: "ExpectedFailure", value: 42 },
);

Call Signature

function assertThrows(run: () => unknown, expected: unknown): void;

Defined in: packages/common/src/Assert.ts:376

Asserts that a function throws the expected value.

Expected values use assertEqual semantics. To perform several or specialized assertions, pass an assertion function that receives the thrown value and returns nothing. Returning a predicate result fails. Use assertThrowsSame for SameValue or reference identity and assertThrowsInstanceOf for a runtime type.

Because JavaScript permits throwing functions, a function second argument is always treated as an assertion function. Use assertThrowsSame to assert that a particular function was thrown.

Example

import { assertThrows } from "@evolu/common";

assertThrows(
  () => {
    // oxlint-disable-next-line eslint/no-throw-literal, typescript/only-throw-error -- Exercise arbitrary thrown values.
    throw { type: "ExpectedFailure", value: 42 };
  },
  { type: "ExpectedFailure", value: 42 },
);