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

cypress-html-validate

v6.1.0

Published

Cypress plugin for html-validate

Downloads

30,647

Readme

Cypress HTML-Validate plugin

Validates HTML using html-validate. It automatically fetches the active source markup from the browser and validates, failing the test if any validation errors is encountered.

Prerequisites

Installation

npm install --save-dev html-validate cypress-html-validate

Make sure you install both html-validate and the plugin cypress-html-validate. With NPM 7 or later it will be satisfied by the peer dependency but for a more consistent and deterministic experience it is suggested to include both as dependencies for your project.

Usage

With Cypress 10 or later

Import the plugin:

import htmlvalidate from "cypress-html-validate/plugin";

Add the setupNodeEvents if it doesnt already exist (either in the e2e or component block):

export default defineConfig({
  e2e: {
    setupNodeEvents(on) {
      htmlvalidate.install(on);
    },
  },
});

Import the command in the support file (default cypress/support/e2e.[jt]s for E2E-tests and cypress/support/component.[jt]s for component tests):

import "cypress-html-validate/commands";

With earlier versions (pre v10)

Install the plugin in cypress/plugins/index.js:

const htmlvalidate = require("cypress-html-validate/plugin");

module.exports = (on) => {
  htmlvalidate.install(on);
};

Import the command in the support file cypress/support/index.js:

import "cypress-html-validate/commands";

In test files:

it("should be valid", () => {
  cy.visit("/my-page.html");
  cy.htmlvalidate();
});

To automatically run after each test you can use afterEach either in the spec file or in cypress/support/index.js:

afterEach(() => {
  cy.htmlvalidate();
});

Options may optionally be passed (both plugin options and html-validate configuration):

/* plugin option to exclude an element */
cy.htmlvalidate({
  exclude: [".ignore-me"],
});

/* html-validate config to disable a rule */
cy.htmlvalidate({
  rules: {
    "input-missing-label": "off",
  },
});

/* both options */
cy.htmlvalidate(
  {
    rules: {
      "input-missing-label": "off",
    },
  },
  {
    exclude: [".ignore-me"],
  }
);

Running without a subject validates the entire document (with exception of using configuration options include and exclude). If you use it on a subject only that element and its descendants are validated:

it("should be valid", () => {
  cy.visit("/my-page.html");
  cy.get("#my-fancy-element").htmlvalidate();
});

Configuration

html-validate and the plugin can configured globally in the install function:

/* html-validate configuration */
const config = {
  rules: {
    foo: "error",
  },
};

/* plugin options */
const options = {
  exclude: [],
  include: [],
  formatter(messages) {
    console.log(messages);
  },
};

htmlvalidate.install(on, config, options);

The default configuration extends html-validate:recommended and html-validate:document (see presets). This can be overridden by explictly specifying extends: []:

htmlvalidate.install(on, {
  extends: [],
});

See html-validate documentation for full description of configuration.

If you want to override per call you can pass configuration and/or options directly to the function:

cy.htmlvalidate([config], [options]);

Options

exclude: string[]

A list of selectors to ignore errors from. Any errors from the elements or any descendant will be ignored.

include: []

A list of selectors to include errors from. If this is set to non-empty array only errors from elements or any descendants will be included.

formatter

  • type: (messages: Message[]): void

Custom formatter/reporter for detected errors. Default uses console.table(..) to log to console. Set to null to disable.

The original formatter can be imported with:

import { formatter } from "cypress-html-validate";