@hegeldev/hegel
    Preparing search index...

    Class TestCase

    A handle to the current test case, passed to the test function: draw values from it, make assumptions, and record notes, targets and events.

    Index

    Methods

    • Reject the current test case if the condition is false. A rejected test case is neither a pass nor a failure: it is abandoned, and does not count toward testCases.

      Parameters

      • condition: boolean

      Returns void

    • A handle onto the same test case drawing from its own, independent choice stream: give each concurrent task of a test its own clone, so that what one draws does not depend on how the tasks interleave. A clone's draws and notes are printed where the clone was made; use setWorker to label them with the task they came from. Clones share the test case's outcome, so a clone must not be used once the test body has returned: drawing from it then throws.

      Returns TestCase

    • Draw a value from a generator.

      When this test case is reported (the failing example, or every test case of a verbose run), the generator prints the value as it draws it, as const draw_1 = <value>;, numbered in the order of the test's draws. Pass name to report it as const <name>_1 = <value>; instead: each use of a name is numbered in turn (<name>_1, <name>_2, …). Draws made while another is in progress — inside a composite generator, say — are part of the outer value and are not printed separately.

      The generator must be a generators.PrintableGenerator; a generator that cannot print throws a TypeError. Use drawSilent to draw from one without printing it.

      Type Parameters

      • T

      Parameters

      Returns T

    • Draw a value from a generator without printing it in the report of a failing example. Use it for values that would only clutter the report, or from a generator that cannot print. It cannot be used in an explicit example, whose values are matched to draws by the names they are printed under.

      Like every draw, this runs the generator inside a shrinking span labelled with the generator's label, so a generator drawing from component generators should draw them through this (or drawAndPrint) rather than calling their doDraw directly: that is what groups each component's choices for the shrinker.

      Type Parameters

      • T

      Parameters

      Returns T

    • Record that label happened in this test case. With the showStatistics setting on, the end of the run reports the fraction of test cases recording each event. Has no effect in an explicit example.

      Parameters

      • label: string

      Returns void

    • Record a numeric observation under label. With the showStatistics setting on, the end of the run reports the count, minimum, median, mean, 90th percentile and maximum of the values recorded under each label. value must be finite. Has no effect in an explicit example.

      Parameters

      • label: string
      • value: number

      Returns void

    • Add a message to the output of this test case, between its drawn values in the order it was recorded. The output is shown for the failing example Hegel reports, and for every test case of a verbose run.

      Parameters

      • message: string

      Returns void

    • Reject the current test case unconditionally, as assume(false) does.

      Returns never

    • Run body an engine-chosen number of times, then end the test case successfully: for workload-style tests that perform operations until the engine decides to stop. A repetition that rejects (with assume) is skipped and the next one runs. A failure in any repetition fails the test case, and shrinking reduces the number of repetitions along with the values each drew. Each repetition is reported under a // Repetition #N heading, its draws and notes indented beneath it.

      repeat never returns: code after it does not run. For an asynchronous body, use repeatAsync. In an explicit example, which has no engine to choose the number of repetitions, body runs once.

      Parameters

      • body: () => void

      Returns never

    • repeat with an asynchronous body, each repetition awaited before the next.

      Parameters

      • body: () => void | Promise<void>

      Returns Promise<never>

    • Attribute everything this handle prints from now on to concurrent worker index (a non-negative integer): each line is prefixed with [worker N +X.XXXms], the time since the test case started.

      Parameters

      • index: number

      Returns void

    • Guide generation toward test cases that maximise score. label names the quantity being maximised, so that one test can target several; each label can be targeted at most once per test case. score must be finite. Has no effect in an explicit example.

      Parameters

      • score: number
      • label: string = ""

      Returns void