Function addCommands

  • Add cypress commands from raw typescript functions. This allows smaller definition footprints while keeping documentation and go to definition IDE utils.

    // original
    Cypress.Commands.add('clickLink', (label) => {
    cy.get('a').contains(label).click()
    });
    // with addCommands
    export const clickLink = (label: string) =>
    cy.get('a').contains(label).click();
    // likely create object with `import * as commands from ...`
    const commands = { clickLink };
    addCommands(commands);
    // type def
    type Commands = typeof commands;
    declare global {
    namespace Cypress {
    interface Chainable extends Commands {
    // can still put types here defined in the old Cypress.Commands.add way
    getInDocument(selector: string): Chainable;
    // overwrite default type, should really accept string | number
    type(text: string | number, options?: Partial<TypeOptions>): Chainable;
    }
    }
    }

    Type Parameters

    • T extends Record<string, any>

    Parameters

    • commands: T

    Returns void