0.2.0 • Published 4 years ago

jest-puppeteer-performance-tester v0.2.0

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

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:

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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) The actual portion of the test, you want to run the performance against.
  2. optional [function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Objects) A set of options you may want to change.

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,
    })
  })
})
0.2.0

4 years ago

0.1.1

4 years ago