Files
cypress/npm/mount-utils
Adam Stone-Lord 4c11731ee1 chore: optimize task execution (#27848)
* Revert "chore: simplify build script (#27547)"

This reverts commit 0a86ec686e.

* Revert "chore: upgrade lerna to 6, cache build step (#26913)"

This reverts commit 9e60aeba8f.

* [run ci]

* chore: upgrade lerna to 6, cache build step (#26913)

* chore: update build-npm-modules script

* chore: update build-npm-modules script

* chore: update build-npm-modules script

* chore: update build-npm-modules script

* chore: update lerna to 6

* [run ci]

* try caching build step

* we can't clean without building after

* add dependencies on scripts for npm packages

* update commands

* add config for data-context build step

* fix output configurations for npm packages, add gitignores

* revert changes to config and data-context build steps

* fix outputs

* run with cache

* fix outputs for cli

* actually fix outputs

* test with cache

---------

Co-authored-by: astone123 <adams@cypress.io>

* chore: simplify build script (#27547)

* chore: simplify build script

* update CI workflows

* fix workflows

* empty commit because Percy weirdness

* chore: add driver, reporter, config as implicit dependencies for runner package (#27559)

* run all workflows on branch

* chore: parallelize test-binary-against-recipes CI step (#27570)

* chore: fix some easy circular dependencies in dep graph (#27612)

* chore: remove gulp tasks from postinstall (#27616)

* empty commit

* chore: minor improvements to node_modules_install (#27647)

* chore: fix cypress:open and dev scripts

* run with cache [run ci]

* exclude mochaawesome assets from .yarnclean [run ci]

* bump cache again [run ci]

* run cached [run ci]

* chore: do not cache cli build step [run ci]

* update workflow command and docs for build-cli [run ci]

* fix commands that use scope [run ci]

* use different branch for publish repo [run ci]

* percy weirdness? [run ci]

* fix postbuild cli script [run ci]

* try to remove typescript from production binary [run ci]

* fix circular dependency [run ci]

* try removing ts from node_modules [run ci]

* remove typescript resolution [run ci]

* remove redundant target scripts

* update to lerna scoped commands

* remove unneeded yarn in lerna command

* try to fix Electron install in Windows workflow

---------

Co-authored-by: Jordan <jordan@jpdesigning.com>
Co-authored-by: Dave Kasper <dave.m.kasper@gmail.com>
2023-10-04 12:25:00 -05:00
..

@cypress/mount-utils

Note

this package is not meant to be used outside of cypress component testing.

This library exports some shared types and utility functions designed to build adapters for components frameworks.

It is used in:

What is a Mount Adapter?

All Component Tests require a component to be mounted. This is generally done with a custom command, cy.mount by default.

Requirements

All the functionality used to create the first party Mount adapters is available to support third parties adapters. At minimum, a Mount Adapter must:

  • Receive a component as the first argument. This could be class, function etc - depends on the framework.
  • Return a Cypress Chainable (for example using cy.wrap) that resolves whatever is idiomatic for your framework
  • Call getContainerEl to access the root DOM element

In addition, we recommend that Mount Adapters:

  • call setupHooks to register the required lifecycle hooks for @cypress/mount-utils to work

Example Mount Adapter: Web Components

Here's a simple yet realistic example of Mount Adapter targeting Web Components. It uses utilities from this package (@cypress/mount-utils) This is recommended, so your adapter behaves similar to the first party Mount Adapters.

import {
  ROOT_SELECTOR,
  setupHooks,
  getContainerEl
} from "@cypress/mount-utils";

Cypress.on("run:start", () => {
  // Consider doing a check to ensure your adapter only runs in Component Testing mode.
  if (Cypress.testingType !== "component") {
    return;
  }

  Cypress.on("test:before:run", () => {
    // Do some cleanup from previous test - for example, clear the DOM.
    getContainerEl().innerHTML = "";
  });
});

function maybeRegisterComponent<T extends CustomElementConstructor>(
  name: string,
  webComponent: T
) {
  // Avoid double-registering a Web Component.
  if (window.customElements.get(name)) {
    return;
  }

  window.customElements.define(name, webComponent);
}

export function mount(
  webComponent: CustomElementConstructor
): Cypress.Chainable {
  // Get root selector defined in `cypress/support.component-index.html
  const $root = document.querySelector(ROOT_SELECTOR)!;

  // Change to kebab-case to comply with Web Component naming convention
  const name = webComponent.name
    .replace(/([a-z09])([A-Z])/g, "$1-$2")
    .toLowerCase();

  /// Register Web Component
  maybeRegisterComponent(name, webComponent);

  // Render HTML containing component.
  $root.innerHTML = `<${name} id="root"></${name}>`;

  // Log a messsage in the Command Log.
  Cypress.log({
    name: "mount",
    message: [`<${name} ... />`],
  });

  // Return a `Cypress.Chainable` that returns whatever is idiomatic
  // in the framework your mount adapter targets.
  return cy.wrap(document.querySelector("#root"), { log: false });
}

// Setup Cypress lifecycle hooks.
setupHooks();

Usage:

// User will generally register a `cy.mount` command in `cypress/support/component.js`:

import { mount } from '@package/cypress-web-components'

Cypress.Commands.add('mount', mount)

// Test
export class WebCounter extends HTMLElement {
  constructor() {
    super();
  }

  connectedCallback() {
    this.innerHTML = `
      <div>
        <button>Counter</button>
      </div>`;
  }
}


describe('web-component.cy.ts', () => {
  it('playground', () => {
    cy.mount(WebCounter)
  })
})

For more robust, production ready examples, check out our first party adapters.

Compatibility

@cypress/mount-utils cypress
<= v1 <= v9
>= v2 >= v10

Changelog

Changelog