Skip to main content

Ensure <Actual>

The interaction to Ensure verifies if the resolved value of the provided Answerable meets the specified Expectation. If not, it throws an AssertionError.

Use Ensure to verify the state of the system under test.

Basic usage with static values

import { actorCalled } from '@serenity-js/core'
import { Ensure, equals } from '@serenity-js/assertions'

await actorCalled('Erica').attemptsTo(
Ensure.that('Hello world!', equals('Hello world!'))
)

Composing expectations with and

import { actorCalled } from '@serenity-js/core'
import { and, Ensure, startsWith, endsWith } from '@serenity-js/assertions'

await actorCalled('Erica').attemptsTo(
Ensure.that('Hello world!', and(startsWith('Hello'), endsWith('!'))
)

Overriding the type of Error thrown upon assertion failure

import { actorCalled, TestCompromisedError } from '@serenity-js/core'
import { and, Ensure, startsWith, endsWith } from '@serenity-js/assertions'
import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest'

await actorCalled('Erica')
.whoCan(CallAnApi.at('https://example.com'))
.attemptsTo(
Send.a(GetRequest.to('/api/health')),
Ensure.that(LastResponse.status(), equals(200))
.otherwiseFailWith(TestCompromisedError, 'The server is down, please cheer it up!')
)

Hierarchy

  • Interaction
    • Ensure

Index

Methods

staticthat

  • that<Actual_Type>(actual: Answerable<Actual_Type>, expectation: Expectation<Actual_Type>): Ensure<Actual_Type>
  • Creates an interaction to Ensure, which verifies if the resolved value of the provided Answerable meets the specified Expectation. If not, it immediately throws an AssertionError.


    Type parameters

    • Actual_Type

    Parameters

    • actual: Answerable<Actual_Type>

      An Answerable describing the actual state of the system.

    • expectation: Expectation<Actual_Type>

      An Expectation you expect the actual value to meet

    Returns Ensure<Actual_Type>

staticeventually

  • eventually<Actual_Type>(actual: Answerable<Actual_Type>, expectation: Expectation<Actual_Type>): EnsureEventually<Actual_Type>
  • Creates an interaction to EnsureEventually, which verifies if the resolved value of the provided Answerable meets the specified Expectation within the expected timeframe.

    If the expectation is not met by the time the timeout expires, the interaction throws an AssertionError. EnsureEventually ignores retries the evaluation if resolving the actual results in an OptionalNotPresentError, but rethrows any other errors.


    Type parameters

    • Actual_Type

    Parameters

    • actual: Answerable<Actual_Type>

      An Answerable describing the actual state of the system.

    • expectation: Expectation<Actual_Type>

      An Expectation you expect the actual value to meet

    Returns EnsureEventually<Actual_Type>

performAs

  • performAs(actor: UsesAbilities & AnswersQuestions & CollectsArtifacts): Promise<void>
  • @inheritDoc

    Parameters

    • actor: UsesAbilities & AnswersQuestions & CollectsArtifacts

    Returns Promise<void>

otherwiseFailWith

  • otherwiseFailWith(typeOfRuntimeError: new (message: string, cause?: Error) => RuntimeError, message?: string): Interaction
  • Overrides the default AssertionError thrown when the actual value does not meet the expectation.


    Parameters

    • typeOfRuntimeError: new (message: string, cause?: Error) => RuntimeError

      A constructor function producing a subtype of RuntimeError to throw, e.g. TestCompromisedError

    • optionalmessage: string

      The message explaining the failure

    Returns Interaction

instantiationLocation

  • instantiationLocation(): FileSystemLocation
  • Returns the location where this Activity was instantiated.


    Returns FileSystemLocation

toString

  • toString(): string
  • Generates a human-friendly description to be used when reporting this Activity.

    Note: When this activity is reported, token #actor in the description will be replaced with the name of the actor performing this Activity.

    For example, #actor clicks on a button becomes Wendy clicks on a button.


    Returns string