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 🙏

© 2025 – Pkg Stats / Ryan Hefner

puppeteer-io

v2.0.1

Published

Library for tests with Puppeteer using Jest or Mocha

Downloads

23

Readme

Puppeteer-IO

tests: passing tests with puppeteer: 14.1.0

Puppeteer-IO is a library for Puppeteer that parallelizes code execution into two streams: the command input stream to the browser and the stream of receiving messages from the browser. Puppeteer-IO was created for writing tests, when it is necessary to check whether some code was called. For this purpose, use the console.log call to retrieve messages from the browser.

Install

npm i puppeteer-io

Usage

React-component testing example

Let's look at testing the React-component using Jest. The source code of the React-component:

import React from "react";
import PropTypes from "prop-types";

const iItem = PropTypes.shape({
    id: PropTypes.string.isRequired,
    text: PropTypes.string.isRequired,
});

export default class Select extends React.Component {
    static propTypes = {
        items: PropTypes.arrayOf(iItem).isRequired,
        onChange: PropTypes.func,
    };

    getChangeHandler() {
        return ({ target }) => {
            if (this.props.onChange) {
                this.props.onChange(this.props.items[target.selectedIndex].id);
            }
        };
    }

    toOption(item, index) {
        return <option key={`id_${index}_${item.id}`}>{item.text}</option>;
    }

    render() {
        return (
            <select onChange={this.getChangeHandler()}>
                {this.props.items.map(this.toOption)}
            </select>
        );
    }
}

Now create a page that will open in the browser to test the component.

import React from "react";
import ReactDOM from "react-dom";
import Select from "./select-component.js";

const testItems = [
    { id: "0e210d4a-ccfd-4733-a179-8b51bda1a7a5", text: "text 1" },
    { id: "ea2cecbd-206c-4118-a1c9-8d88474e5a87", text: "text 2" },
    { id: "c812a9dc-6a54-409e-adb5-8eb09337e576", text: "text 3" },
];

console.log("test-items", testItems);

function TestPage() {
    const onChange = (id) => console.log("Select: change", id);

    return (
        <div>
            <Select items={testItems} onChange={onChange} />
        </div>
    );
}

ReactDOM.render(<TestPage />, document.getElementById("application"));

And the test case.

const puppeteer = require("puppeteer");
const io = require("puppeteer-io");

test(`check that id is sent to onChange callback`, async () => {
    let browser = await puppeteer.launch();
    let page = await browser.newPage();

    await page.goto("http://localhost"); // test page url

    await io({
        page,
        async input() {
            let select = await page.$("select");

            await select.focus();
            await select.press("Enter");
            await select.press("ArrowDown");
            await select.press("Enter");
        },
        async output({ message }) {
            let [, secondItem] = await message("test-items");
            let selectedId = await message("Select: change");

            expect(selectedId).toBe(secondItem.id);
        },
    });

    await page.close();
    await browser.close();
});

Another example

Check whether the event handler was called.

index.html

<input />
<script>
    const focusHandler = () => console.log("input-onFocus");

    document
        .querySelector("input")
        .addEventListener("focus", focusHandler, false);
</script>

on-focus.test.js

const puppeteer = require("puppeteer");
const io = require("puppeteer-io");

let browser, page;

beforeAll(async () => {
    browser = await puppeteer.launch();
});

beforeEach(async () => {
    page = await browser.newPage();

    await page.goto("http://localhost");
});

afterEach(async () => {
    await page.close();
});

afterAll(async () => {
    await browser.close();
});

test(`message received`, (done) => {
    io({
        page,
        done,
        async input() {
            await page.focus("input");
        },
        async output({ message }) {
            await message("input-onFocus");
        },
    });
});

Documentation

require("puppeteer-io") returns a function that takes as argument an object contains:

  • page <Page> The page in which the code is executed.

  • done <Function> A callback that is called when both streams are executed.

  • input() <async Function> The command input stream to the browser

  • output(api) <async Function> The stream of receiving messages from the browser. This function provide an object contains methods:

    • message(content: <String>): <Promise<JSONValue>> Promise returns an object passed from the browser by console.log(id, data). You can only pass the serializable data (more).

    • error(pattern: <String|RegExp>): <Promise<String>> Promise returns error.message if the message matches the pattern.