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

mithril-tidy

v0.1.0

Published

Render Mithril templates to tidy HTML. Useful for testing pages and components with Jest.

Downloads

3

Readme

mithril-tidy

Render Mithril templates to tidy HTML. Useful for testing pages and components with Jest.

Rationale

Jest creates textual snapshots of components to make it easy to spot changes. Differences in rendered HTML are printed to the console as diffs, making it easy to accept or reject the new snapshot; see Jest's snapshot release blog post.

React's toolchain is fully set up for Jest, but for Mithril two extra steps are needed to create snapshot-ready HTML:

  1. Render the template to HTML
  2. Tidy the HTML (multiline, indented)

This utility helps to fill those 2 steps.

Installation

npm install --save-dev mithril-tidy

You may need to install command line library tidy-html5 too (on a Mac: brew install tidy-html5).

Setup

A typical Jest setup is to have a spec file for each page or component - for component component.js you'll have test file component.spec.js. Jest's convention is to put these test files inside directory __tests__/, but there is no specific need to do this; any directory (for example the conventional test) will work.

If your tests live in directory test, add to package.json to the "scripts" entry:

"test": "jest test/*"

Then run npm test. For interactive mode, run:

npm test -- --watch

API

tidy

Renders a Mithril template to (tidy) HTML so it can be used to call Jest's toMatchSnapshot.

Example

import m from "mithril";
import { tidy } from "mithril-tidy";

const page = {
  view: () =>
    m("div", [
      m("h1", "Page"),
      m("a", {
        href: "/home"
      }, "Back")
    ])
};

describe("Page component", () => {
  it("should have a title", () => {
    const cmp = m(page);
    const html = tidy(cmp);
    expect(html).toMatchSnapshot();
  });
});

This generates a new (or diffs an existing) snapshot.

The rendered HTML can also be used to inspect:

describe("Page component", () => {
  it("title should be h1 with label 'Page'", () => {
    const cmp = m(page);
    const html = tidy(cmp);
    expect(html).toContain("Page");
  });
});

Signature

tidy(vnodes, htmlTidyOptions) => String

Argument | Type | Required | Description ------------------------- | ------------------------- | -------- | --- vnodes | Mithril element | Yes | The vnodes to be rendered. htmlTidyOptions | Object | No | Overrides the default HTML Tidy configuration options. Returns | String | | Rendered and tidied HTML String.

Dependencies

Licence

MIT