Assertable wraps Cypress.Chainable so that your tests are as decoupled as possible from Cypress. By using the Assertable class, you can use the same assertions in your tests, regardless of the testing framework you use. All you need to do if you wish to replace Cypress with another testing framework and keep your tests, is to replace the implementation of the Assertable class. You can also add assertions of your own, by extending Assertable class.

Example

import { Assertable, CypressHelper, then } from "@shellygo/cypress-test-utils";

class MyAssertable<T> extends Assertable<T> {
private styleFromWindow = (win: Window) => {
const styleItem = win.localStorage.getItem(`style`);
return JSON.parse(styleItem || "");
};

public shouldEqualToStoredStyle = () =>
then(
new CypressHelper().get.window().then((win) => {
const style = styleFromWindow(win);
then(this.chainable).shouldDeepNestedInclude(style);
})
);
}

class Driver {
public given = {
.
.
};
public when = {
.
.
};
public get = {
.
.
};
public then = (chainable: Cypress.Chainable<any>) => new MyAssertable(chainable);
}

Type Parameters

  • T

Hierarchy

  • Assertable

Constructors

  • Type Parameters

    • T

    Parameters

    • chainable: Chainable<T>

    Returns Assertable<T>

Properties

chainable: Chainable<T>

Methods

  • Parameters

    • chainer: string
    • Rest ...rest: any[]

    Returns Chainable<T>

  • Assert that at least one element of the selection is checked, using .is(':checked').

    Returns Chainable<T>

    Example

       then(get.elementByTestId("checkbox-selector")).shouldBeChecked()
    
  • Assert that at least one element of the selection is disabled, using .is(':disabled').

    Returns Chainable<T>

    Example

       then(get.elementByTestId("selector")).shouldBeDisabled()
    
  • Assert that at least one element of the selection is enabled, using .is(':enabled').

    Returns Chainable<T>

    Example

       then(get.elementByTestId("selector")).shouldBeEnabled()
    
  • Assert that at least one element of the selection is focused.

    Returns Chainable<T>

    Example

    then(get.elementByTestId("selector")).shouldBeFocused()
    
  • Asserts that the target is a number or a date greater than the given number or date n respectively. However, it's often best to assert that the target is equal to its expected value.

    Parameters

    • value: number

    Returns Chainable<T>

    Example

       then(get.numberOfElements("radio")).shouldBeGreaterThan(5);
    
  • Asserts that the target is a number or a date greater than or equal to the given number or date n respectively. However, it's often best to assert that the target is equal to its expected value.

    Parameters

    • value: number

    Returns Chainable<T>

    Example

      then(get.numberOfElements("radio")).shouldBeGreaterThanOrEqual(5);
    
  • Asserts that the target is a number or a n date less than or equal to the given number or date n respectively. However, it's often best to assert that the target is equal to its expected value.

    Parameters

    • value: number

    Returns Chainable<T>

    Example

       then(get.numberOfElements("radio")).shouldBeLessThen(5);
    
  • Asserts that the target is a number or a date less than or equal to the given number or date n respectively. However, it's often best to assert that the target is equal to its expected value.

    Parameters

    • value: number

    Returns Chainable<T>

    Example

    then(get.numberOfElements('list-item')).shouldBeLessThanOrEqual(5)
    
  • Assert that at least one element of the selection is selected, using .is(':selected').

    Returns Chainable<T>

    Example

    then(get.elementByTestId("selector")).shouldBeSelected()
    
  • Assert that at least one element of the selection is visible, using .is(':visible').

    Returns Chainable<T>

    Example

    then(get.elementByTestId("selector")).shouldBeVisible()
    
  • Assert that the text of the first element of the selection partially contains the given text, using .text().

    Parameters

    • text: string

    Returns Chainable<T>

    Example

    then(get.elementByTestId("selector")).shouldContainText("test")
    
  • Causes all .equal, .include, .members, .keys, and .property assertions that follow in the chain to use deep equality instead of strict (===) equality. See the deep-eql project page for info on the deep equality algorithm: https://github.com/chaijs/deep-eql.

    Parameters

    • value: any

    Returns Chainable<T>

    Example

    then(
    get.fixture("user")).shouldDeepEqual({
    name: "Jane Doe",
    id: "1234"
    })
  • Asserts that the target has a property with the given key name.

    Parameters

    • value: any

    Returns Chainable<T>

    Example

    then(
    get.fixture("user")).shouldDeepNestedInclude({
    name: "Jane Doe",
    id: "1234",
    nested: {
    attr1: "something",
    attr2: "the other thing"
    }
    })
  • Asserts that text ends with value

    Parameters

    • value: string

    Returns Chainable<string>

    Example

      then(helper.get.elementsText('selector)).shouldEndWith('test')
    
  • Asserts that the target is strictly (===) equal to the given val.

    Parameters

    • value: any

    Returns Chainable<T>

    Example

       then(get.inputValue("name-input")).shouldEqual("John")
    
  • Assert that the selection is not empty. Note that this overrides the built-in chai assertion. If the object asserted against is not a jQuery object, the original implementation will be called.

    Returns Chainable<T>

    Example

      then(get.elementByTestId("selector")).shouldExist()
    
  • Assert that the first element of the selection has the given attribute, using .attr(). Optionally, assert a particular value as well. The return value is available for chaining.

    Parameters

    • attribute: string
    • expectedValue: string

    Returns Chainable<T>

    Example

     then(get.elementByTestId("selector")).shouldHaveAttribute("test")
    
  • Asserts spy was called ate least once

    Returns Chainable<T>

    Example

    then(get.spy("onSomething")).shouldHaveBeenCalled()
    
  • Asserts spy was called exactly once

    Returns Chainable<T>

    Example

    then(get.spy("onSomething")).shouldHaveBeenCalledOnce()
    
  • Asserts spy was called exactly thrice

    Returns Chainable<T>

    Example

    then(get.spy("onSomething")).shouldHaveBeenCalledThrice()
    
  • Asserts spy was called exactly n times

    Parameters

    • n: number

      number of times spy should have been called

    Returns Chainable<T>

    Example

    then(get.spy("onSomething")).shouldHaveBeenCalledTimes(5)
    
  • Asserts spy was called exactly twice

    Returns Chainable<T>

    Example

    then(get.spy("onSomething")).shouldHaveBeenCalledTwice()
    
  • Assert spy was called at least once with the provided arguments.

    Parameters

    • Rest ...args: any[]

    Returns Chainable<T>

    Example

    then(get.spy("onSomething")).shouldHaveBeenCalledWith({ id: 1 })
    
  • Assert spy was called with matching arguments (and possibly others).

    Parameters

    • Rest ...args: any[]

    Returns Chainable<T>

    Example

    then(get.spy("onSomething")).shouldHaveBeenCalledWithMatch(match({ id: 1 }))
    

    Example

     it('should call the get method of the HTTP client with a URL with query param filter = status', () => {
    healthService.fetchHealthResults(status);
    then(get.mock.httpClientService().get).shouldHaveBeenCalledWith(
    match(baseURL),
    match.hasNested(
    'params.updates[0]',
    match({ param: 'filter', value: `status eq ${status}` })
    )
    );
    });
  • Assert that the selection has the given CSS class.

    Parameters

    • value: string

    Returns Chainable<T>

    Example

      then(get.elementByTestId("selector")).shouldHaveClass("test")
    
  • Assert that an element has a css property with the given value.

    Parameters

    • property: string
    • expectedValue: string

    Returns Chainable<T>

    Example

    then(get.elementByTestId("selector")).shouldHaveCss("color", "rgb(102, 102, 102)"")
    
  • Asserts that the target's length property is equal to the given number n..

    Parameters

    • length: number

    Returns Chainable<T>

    Example

       then(get.elementByTestId("selector")).shouldHaveLength(3)
    
  • Assert that the text of the first element of the selection is equal to the given text, using .text().

    Parameters

    • value: string

    Returns Chainable<T>

    Example

     then(get.elementByTestId("selector")).shouldHaveText("test")
    
  • Assert that the first element of the selection has the given value, using .val().

    Parameters

    • value: string

    Returns Chainable<T>

    Example

    then(get.elementByTestId("selector")).shouldHaveValue("test")
    
  • When the target is a string, .include asserts that the given string val is a substring of the target.

    Parameters

    • value: any

    Returns Chainable<T>

    Example

       then(helper.get.elementsText('selector)).shouldContain('test')
    
  • Assert that at least one element of the selection is not checked, using .is(':checked').

    Returns Chainable<T>

    Example

      then(get.elementByTestId("checkbox-selector")).shouldNotBeChecked()
    
  • Assert that no element of the selection is focused.

    Returns Chainable<T>

    Example

    then(get.elementByTestId("selector")).shouldNotBeFocused()
    
  • Assert that at least one element of the selection is not selected, using .is(':selected').

    Returns Chainable<T>

    Example

    then(get.elementByTestId("selector")).shouldNotBeSelected()
    
  • Assert that at least one element of the selection is not visible, using .is(':visible').

    Returns Chainable<T>

    Example

    then(get.elementByTestId("selector")).shouldNotBeVisible()
    
  • Assert that the selection is empty. Note that this overrides the built-in chai assertion. If the object asserted against is not a jQuery object, the original implementation will be called.

    Returns Chainable<T>

    Example

       then(get.elementByTestId("selector")).shouldNotExist()
    
  • Asserts spy was not called

    Returns Chainable<T>

    Example

    then(get.spy("onSomething")).shouldNotHaveBeenCalled()
    
  • Asserts spy was NOT called exactly n times

    Parameters

    • n: number

      number of times spy should NOT have been called

    Returns Chainable<T>

    Example

    then(get.spy("onSomething")).shouldNotHaveBeenCalledTimes(5)
    
  • When the target is a string, not.include asserts that the given string val is not a substring of the target.

    Parameters

    • value: any

    Returns Chainable<T>

    Example

       then(helper.get.elementsText('selector)).shouldNotContain('test')
    
  • When no arguments are provided, shouldThrow invokes the target function and asserts that no error is thrown. When one argument is provided, and it's a string, shouldThrow invokes the target function and asserts that no error is thrown with a message that contains that string.

    Parameters

    • Optional value: string | RegExp

    Returns Chainable<T>

    Example

    function badFn() { console.log('Illegal salmon!') }
    then(() => badFn()).shouldNotThrow()
    then(() => badFn()).shouldNotThrow('salmon')
    then(() => badFn()).shouldNotThrow(/salmon/)
  • Asserts that text starts with value

    Parameters

    • value: string

    Returns Chainable<string>

    Example

      then(helper.get.elementsText('selector)).shouldStartWith('test')
    
  • When no arguments are provided, shouldThrow invokes the target function and asserts that an error is thrown. When one argument is provided, and it's a string, shouldThrow invokes the target function and asserts that an error is thrown with a message that contains that string.

    Parameters

    • Optional value: string | RegExp

    Returns Chainable<T>

    Example

    function badFn() { throw new TypeError('Illegal salmon!') }
    then(() => badFn()).shouldThrow()
    then(() => badFn()).shouldThrow('salmon')
    then(() => badFn()).shouldThrow(/salmon/)

Generated using TypeDoc