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

jest-puppeteer-performance-tester

v0.2.0

Published

Performance testing with Jest and Puppeteer

Downloads

4

Readme

jest-puppeteer performance tester

It is just an extension on jest matchers that you can test your project's performance using Jest and Puppeteer. It also allows you to assert metrics for specific tasks. If they don't meet your expectations your test suite will fail.

Prerequisites

Make sure you have installed all the following dependencies in your project:

yarn add --dev jest puppeteer jest-puppeteer

Installing

All you need to do is to install jest-puppeteer-performance-tester.

yarn add --dev jest-puppeteer-performance-tester

Then add it to your jest.config.js file.

{
  "setupFilesAfterEnv": ["jest-puppeteer-performance-tester"]
}

NOTE: You'll also need to set the preset to jest-puppeteer in order to write tests for Puppeteer in Jest environment.

{
  "preset": "jest-puppeteer"
}

Usage

Jest's expect is extended with the method .toMatchAverageMetrics which accept a Metrics typed key/value object as a parameter. The expect method itself accepts an Array or a Function of type MetricsMatcher. The Array is actually a Tuple of up to three items, where the first one is always the test body you want to run your assertions agains. The second item in the Array is the "reset" function or if you want to omit that then it's the options configuration.
The options is a key/value object where you can pass your desired configuration. Currently we only have the repeats property which indicated how many time to repeat the test function before calculating the average results. By default repeats is 1.
See the exmaples for better view.

Object definitions

Metrics

Metrics also found in the Puppeteer API doc is just this:

  • Timestamp <number> The timestamp when the metrics sample was taken.
  • Documents <number> Number of documents in the page.
  • Frames <number> Number of frames in the page.
  • JSEventListeners <number> Number of events in the page.
  • Nodes <number> Number of DOM nodes in the page.
  • LayoutCount <number> Total number of full or partial page layout.
  • RecalcStyleCount <number> Total number of page style recalculations.
  • LayoutDuration <number> Combined durations of all page layouts.
  • RecalcStyleDuration <number> Combined duration of all page style recalculations.
  • ScriptDuration <number> Combined duration of JavaScript execution.
  • TaskDuration <number> Combined duration of all tasks performed by the browser.
  • JSHeapUsedSize <number> Used JavaScript heap size.
  • JSHeapTotalSize <number> Total JavaScript heap size.

MetricsMatcher

MetricsMatcher is a Function or a Tuple Array with the length of 1-3 which is passed to the Jest's expect function. There are three types of properties that the .toMatchAverageMetrics needs.

  1. <function> The actual portion of the test, you want to run the performance against.
  2. optional <function> A function where you can write a set of actions to reach the state of the page to be ready for the test to be repeated.
  3. optional <object> A set of options you may want to change.
    • repeats <number> Indicated how many times you want your Test function to run. Defaults to 1.

Or you can just pass a function which will be considered as the first item of MetricsMatcher and the rest will work with their default behaviours.

Example

Let's test the performance of the Google search bar typing speed

import { MetricsMatcher } from "jest-puppeteer-performance-tester"

describe("Google Performance", () => {
  test("Types into the search bar", async () => {
    await page.goto("https://google.com", { waitUntil: "networkidle0" })
    await expect<MetricsMatcher>(async () => {
      await page.type(`input[name="q"]`, `Hello World!`)
    }).toMatchAverageMetrics({
      TaskDuration: 0.06,
    })
  })

  test("Types into the search bar and repeat the proccess 10 times", async () => {
    await page.goto("https://google.com", { waitUntil: "networkidle0" })
    await expect<MetricsMatcher>([
      async () => {
        await page.type(`input[name="q"]`, `Hello World!`)
      },
      async () => {
        await page.click(`[aria-label~="clear" i]`)
      },
      {
        repeats: 10,
      },
    ]).toMatchAverageMetrics({
      TaskDuration: 0.06,
    })
  })
})