npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@swimlane/cy-dom-diff

v2.0.0

Published

matching chunks of DOM against HTML; including dynamic content.

Downloads

2,422

Readme

cy-dom-diff

cy-dom-diff allows matching chunks of DOM against HTML; including dynamic content.

Introduction

cy-dom-diff consist of two parts. First is a Cypress command that matches a DOM element against a regular expression. When a DOM element is matched its HTML is normalized to produce consistent diffable output while maintaining sematic meaning. This includes:

  • whitespace and newlines are normalized
  • tags and attributes are printed on individual lines
  • comments are removed
  • tags and attributes can be ignored through configuration

In the second part cy-dom-diff provides a template function to generate a regular expression from a HTML string. This HTML string is passed through the same normalization as the DOM element (described above) as well as allowing embedded regular expression for dynamic content.

Installation and Setup

npm install --save-dev @swimlane/cy-dom-diff

Import the Cypress commands in cypress/support/index.js or cypress/support/commands.js

import '@swimlane/cy-dom-diff/commands';

Usage

In a basic example the following will assert that the element matches the regexp generated by the dom template tag. Most of the HTML string within the tagged template string is treated literally. However, regular expressions within the embedded expression are matches as expected.

import { dom } from '@swimlane/cy-dom-diff';

const TIME = /\d?\d:\d?\d\:\d?\d/;
const NUMBER = /[\+\-]?\d*\.?\d+(?:[Ee][\+\-]?\d+)?/;

cy.get('#clock').domMatch(dom`
    <span>The current time is:</span>
    <span class="clock">${TIME} ${/[AP]/}M</span>
    <span class="offset">${NUMBER}</span> hrs
  `);

cy.domMatch Cypress command

cy.domMatch will run the domMatch assertions (see below) and add a Cypress runner log with a diff.

cy.domDiff Cypress command

cy.domDiff is used in the same way as cy.domMatch but will only add a Cypress runner log with a diff. It will not fail if the DOM does not match and will not retry.

domMatch Chai assertion

At its lowest level cy-dom-diff adds a Chai assertion for matching the DOM. This will not generate a Cypress log except to show that the assertion passed or failed. Use cy.domMatch to get better logging.

import { dom } from '@swimlane/cy-dom-diff';

const TIME = /\d?\d:\d?\d\:\d?\d/;
const NUMBER = /[\+\-]?\d*\.?\d+(?:[Ee][\+\-]?\d+)?/;

cy.get('#clock').should('domMatch', dom`
  <span>The current time is:</span>
  <span class="clock">${TIME} ${/[AP]/}M</span>
  <span class="offset">${NUMBER}</span> hrs
`);

// OR

cy.get('#clock').then($el => {
  expect($el).to.domMatch(dom`
    <span>The current time is:</span>
    <span class="clock">${TIME} ${/[AP]/}M</span>
    <span class="offset">${NUMBER}</span> hrs
  `);
});

Credits

cy-dom-diff is a Swimlane open-source project; we believe in giving back to the open-source community by sharing some of the projects we build for our application. Swimlane is an automated cyber security operations and incident response platform that enables cyber security teams to leverage threat intelligence, speed up incident response and automate security operations.