cypress-smart-tests
Version:
Cypress plugin for smart test execution with dependencies, conditional tests, and hooks
188 lines (187 loc) • 5.92 kB
TypeScript
/**
* cypress-smart-tests
* A Cypress plugin for smart test execution with dependencies, conditional tests, and hooks
*/
/// <reference types="cypress" />
/// <reference types="cypress" />
/// <reference types="cypress" />
declare global {
interface Window {
failedTests: string[];
cyVariablesStore: Record<string, any>;
}
}
interface TestDependencies {
[parentTest: string]: string[];
}
interface CytestOptions {
runIf?: () => boolean;
before?: () => void | Cypress.Chainable<any>;
after?: () => void | Cypress.Chainable<any>;
tags?: string | string[];
}
interface PluginConfig {
failFast: boolean;
}
/**
* Define dependencies between tests
* @param dependencies An object mapping parent tests to their dependent tests
* @example
* ```javascript
* // Define dependencies between tests
* defineTestDependencies({
* 'Login Test': ['View Profile Test', 'Edit Profile Test'],
* 'View Profile Test': ['Edit Profile Test']
* });
* ```
*/
export declare function defineTestDependencies(dependencies: TestDependencies): void;
/**
* Configure the plugin
* @param newConfig Configuration options
* @example
* ```javascript
* // Configure the plugin to skip dependent tests when parent tests fail
* configure({
* failFast: true
* });
* ```
*/
export declare function configure(newConfig: Partial<PluginConfig>): void;
/**
* Reset the plugin state (useful for testing)
* @param {boolean} [resetVariables=false] - If true, also reset the persistent variables
* @example
* ```javascript
* // Reset the plugin state before each test suite
* beforeEach(() => {
* resetState();
* });
*
* // Reset the plugin state including persistent variables
* beforeEach(() => {
* resetState(true);
* });
* ```
*/
export declare function resetState(resetVariables?: boolean): void;
/**
* Get or set a persistent variable that doesn't reset across tests
* @param {string} name - The name of the variable
* @param {any} [value] - The value to set (if provided)
* @returns {any} The current value of the variable, or a function to get/set the variable
* @example
* ```javascript
* // Set a variable
* cyVariable('username', 'testuser');
*
* // Get a variable
* const username = cyVariable('username');
* cy.log(`Current username: ${username}`);
*
* // Update a variable
* cyVariable('username', 'newuser');
*
* // Use in a test
* cytest('User Profile Test', () => {
* const username = cyVariable('username');
* cy.visit(`/users/${username}`);
* cy.get('.user-name').should('contain', username);
* });
* ```
*/
export declare function cyVariable(name: string, value?: any): any;
/**
* Manage multiple persistent variables that don't reset across tests
* @returns {object} An object with methods to manage variables
* @example
* ```javascript
* // Add variables
* cyVariables().add('username', 'testuser');
* cyVariables().add('userId', 123);
* cyVariables().add('userPreferences', { theme: 'dark', language: 'en' });
*
* // Get variables
* const username = cyVariables().get('username');
* const userId = cyVariables().get('userId');
* const userPreferences = cyVariables().get('userPreferences');
*
* // Check if a variable exists
* if (cyVariables().has('username')) {
* cy.log('Username is set');
* }
*
* // Get all variables
* const allVariables = cyVariables().getAll();
* cy.log(`All variables: ${JSON.stringify(allVariables)}`);
*
* // Remove a variable
* cyVariables().remove('username');
*
* // Clear all variables
* cyVariables().clear();
* ```
*/
export declare function cyVariables(): {
add: (name: string, value: any) => void;
get: (name: string) => any;
has: (name: string) => boolean;
remove: (name: string) => void;
getAll: () => Record<string, any>;
clear: () => void;
};
/**
* A wrapper around Cypress's it() function that respects test dependencies
* @param name The name of the test
* @param optionsOrFn The test options or the test function
* @param fnOrUndefined The test function if options are provided
* @example
* ```javascript
* // Basic usage (similar to Cypress's it())
* cytest('Login Test', () => {
* cy.visit('/login');
* cy.get('#username').type('testuser');
* cy.get('#password').type('password');
* cy.get('#login-button').click();
* cy.url().should('include', '/dashboard');
* });
*
* // Conditional test execution
* cytest('Feature X Test',
* { runIf: () => Cypress.env('ENABLE_FEATURE_X') === true },
* () => {
* cy.log('Testing Feature X');
* cy.visit('/feature-x');
* cy.get('.feature-x-element').should('be.visible');
* }
* );
*
* // Test with setup and cleanup hooks
* cytest('User Profile Test', {
* before: () => {
* cy.log('Setting up test data');
* cy.request('POST', '/api/users', { name: 'Test User' })
* .then(response => {
* cy.wrap(response.body).as('testUser');
* });
* },
* after: () => {
* cy.log('Cleaning up test data');
* cy.get('@testUser').then(user => {
* cy.request('DELETE', `/api/users/${user.id}`);
* });
* }
* }, () => {
* cy.get('@testUser').then(user => {
* cy.visit(`/users/${user.id}`);
* cy.get('.user-name').should('contain', user.name);
* });
* });
* ```
*/
export declare function cytest(name: string, optionsOrFn: CytestOptions | (() => void | Cypress.Chainable<any>), fnOrUndefined?: () => void | Cypress.Chainable<any>): Mocha.Test;
export declare namespace cytest {
var skip: (name: string, optionsOrFn?: CytestOptions | (() => void | Cypress.Chainable<any>) | undefined, fnOrUndefined?: (() => void | Cypress.Chainable<any>) | undefined) => Mocha.Test;
var only: (name: string, optionsOrFn: CytestOptions | (() => void | Cypress.Chainable<any>), fnOrUndefined?: (() => void | Cypress.Chainable<any>) | undefined) => Mocha.Test;
}
export {};