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

headless-test

v1.0.3

Published

Run Mocha tests in a headless browser

Downloads

22

Readme

Tests

Runs tests in a headless browser (Chrome-only via Puppeteer for now). Useful to test Web Components, polyfills, or anything else that needs real browser APIs.

Uses Mocha and provides the Chai assertion library.

Usage

Install headless-test via your favourite package manager.

Command-Line or CI

Run and pass a number of files (including tests) which will be loaded in-order as ESM:

headless-test your-code.js your-tests.js

This will exit with non-zero if the Mocha tests fail. Easy!

Note that your-tests.js should look like a totally normal Mocha test file:

// no imports required for Mocha or Chai, they'll be available globally
suite('check stuff', () => {
  test('does a thing', async () => {
    // ...
    await 'foo';
    assert(true, 'this passes');
  });
});

You can also specifiy -b to switch Mocha to BDD mode, or see --help for other options.

This works by hosting a webserver in the current directory (with dhost) so you can load other dependencies.

Extras

CSS can also be included in the page for tests. For example:

headless-test your-code.js your-css.css your-tests.js

API

You can also use headless-test programatically. The most common use case is to pass a URL (e.g., if you're already running a dev server) and specify load to load specific resources as ESM into the test environment.

const headlessTest = require('headless-test');

const p = headlessTest('http://localhost:8080', {
  load: ['your-code.js', 'your-tests.js'],
  driver: {
    // options passed to `mocha.setup`
    ui: 'tdd',
  },
});

p.then(() => /* something */);

Instead of passing a URL, you can also pass a http.Server to read its URL (e.g., if you're running a dev server in the same process).

Virtual Resources

You can also specify virtual script files, if your tests aren't able to be found on your local web server. They'll still be run in the same origin, but won't be able to import each other. For example:

const p = headlessTest('http://localhost:1234', {
  load: [
    {code: 'console.info("hello"); suite("tests", () => { /* stuff */ });'},
    {code: testCode},
  ],
});

If you're not loading any real script files, it's valid to pass null for the URL.

Notes

Don't use this for integration tests. You should be running Mocha locally for that, and having it start Puppeteer to click on things.

Dependencies

This package has a few direct dependencies, although those dependencies have a huge number of transitive dependencies. Here's the short list:

  • mocha is the default test driver
  • chai provides an assertion library to your tests
  • puppeteer includes headless Chromium

Included for the CLI only:

  • dhost provides a never-caching static web server for files
  • mri parses command-line arguments
  • chalk to make things pretty, because it's included by transitive deps anyway ¯‍\‍_‍(‍ツ‍)‍_‍/‍¯