Shared utilities for building Cypress component testing mount adapters
npx @tessl/cli install tessl/npm-cypress--mount-utils@3.0.0Shared utilities for building Cypress component testing mount adapters. This package provides the core infrastructure needed to mount, test, and clean up components across different JavaScript frameworks within the Cypress component testing environment.
npm install @cypress/mount-utilsimport {
ROOT_SELECTOR,
getContainerEl,
setupHooks,
checkForRemovedStyleOptions
} from "@cypress/mount-utils";For CommonJS:
const {
ROOT_SELECTOR,
getContainerEl,
setupHooks,
checkForRemovedStyleOptions
} = require("@cypress/mount-utils");import {
ROOT_SELECTOR,
getContainerEl,
setupHooks,
checkForRemovedStyleOptions
} from "@cypress/mount-utils";
// Set up lifecycle hooks (typically called once)
setupHooks(() => {
// Optional cleanup callback before each test
getContainerEl().innerHTML = "";
});
// In your mount function
function mount(component, options = {}) {
// Validate options don't use deprecated style options
checkForRemovedStyleOptions(options);
// Get the container element for mounting
const container = getContainerEl();
// Mount your component
container.innerHTML = `<div id="component-root"></div>`;
return cy.wrap(document.querySelector("#component-root"));
}@cypress/mount-utils is designed as a foundation library for Cypress component testing adapters. It provides:
Core functionality for locating and accessing the DOM element where components are mounted.
/**
* CSS selector constant for the root DOM element where components are mounted
*/
const ROOT_SELECTOR: "[data-cy-root]";
/**
* Gets the root element used to mount the component
* @returns The root element for mounting components
* @throws Error if the root element is not found
*/
function getContainerEl(): HTMLElement;The ROOT_SELECTOR constant defines the CSS selector used to locate the mounting container. The getContainerEl() function searches for this element and throws a descriptive error if not found, guiding users to add the proper root element to their component-index.html file.
Integration with Cypress test lifecycle to ensure proper setup and cleanup between tests.
/**
* Utility function to register CT side effects and run cleanup code during the "test:before:run" Cypress hook
* @param optionalCallback - Optional callback to be called before the next test runs
*/
function setupHooks(optionalCallback?: Function): void;The setupHooks() function registers essential Cypress lifecycle hooks. It:
cy.visit() from being used in component tests (throws error)Usage Example:
// Basic setup
setupHooks();
// With cleanup callback
setupHooks(() => {
// Clear previous test artifacts
getContainerEl().innerHTML = "";
// Reset any global state
window.myGlobalState = {};
});Validation to ensure deprecated style-related mounting options are not used.
/**
* Validates mounting options and throws error if deprecated style-related options are detected
* @param mountingOptions - Object containing mounting configuration options
* @throws Uses Cypress.utils.throwErrByPath() if removed style options are found
*/
function checkForRemovedStyleOptions(mountingOptions: Record<string, any>): void;This function checks for deprecated style injection options (cssFile, cssFiles, style, styles, stylesheet, stylesheets) and throws descriptive errors directing users to the Cypress 11.0.0 migration guide.
Usage Example:
function mount(component, options = {}) {
// Validate options before mounting
checkForRemovedStyleOptions(options);
// Proceed with mounting...
}Legacy functions maintained for backward compatibility that provide migration guidance.
/**
* @deprecated Removed as of Cypress 11.0.0
* @see https://on.cypress.io/migration-11-0-0-component-testing-updates
* @throws Always throws deprecation error via Cypress.utils.throwErrByPath()
*/
function cleanupStyles(): void;
/**
* @deprecated Removed as of Cypress 11.0.0
* @see https://on.cypress.io/migration-11-0-0-component-testing-updates
* @throws Always throws deprecation error via Cypress.utils.throwErrByPath()
*/
function injectStylesBeforeElement(
options: Partial<StyleOptions & { log: boolean }>,
document: Document,
el: HTMLElement | null
): void;These functions exist solely to provide helpful error messages for users upgrading from older versions of Cypress.
/**
* @deprecated Type definition for style injection options, removed as of Cypress 11.0.0
*/
type StyleOptions = unknown;Cypress global objectdata-cy-root attribute in component-index.htmlThe library provides detailed error messages for common issues:
getContainerEl() throws error with setup instructionscheckForRemovedStyleOptions() throws errors with migration guidancesetupHooks() prevents cy.visit() in component testsThis package is used by all first-party Cypress mount adapters:
getContainerEl() for DOM cleanup and mountinggetContainerEl(), setupHooks(), and checkForRemovedStyleOptions()Third-party mount adapters can use these utilities to ensure consistent behavior with the official adapters.