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

@yathomasi/cypress-parallel

v2.0.2

Published

[![Build status](https://github.com/yathomasi/cypress-parallel/actions/workflows/build.yml/badge.svg)](https://github.com/yathomasi/cypress-parallel/actions/workflows/build.yml) [![Npm package weekly downloads](https://badgen.net/npm/dw/@yathomasi/cypress

Downloads

3,617

Readme

cypress-parallel

Build status Npm package weekly downloads

Divides your test files into equal buckets and runs a single bucket. This is ideal for parallizing Cypress tests in a CI environment, without relying on external services, such as Cypress' Dashboard Service.

Table of Contents

Installation

$ npm install @yathomasi/cypress-parallel

How it works

  1. It will search through your project for test files
  2. A knapsack containing file weights is read (defaults to knapsack.json)
  3. Tests are divided into N buckets
  4. The Ith bucket is executed by passing --spec to Cypress with said bucket of files
  5. The knapsack is overwritten with potentially new weights

N and I is determined either by a flag --node or by environment variables for some CI providers.

The overwritten knapsack can then be comitted back into VCS. This will allow the library to always divide your tests somewhat evenly among the nodes.

Usage

Below are all the configuration options and some extended explanation for some of them.

$ npx cypress-parallel --help
Usage: cypress-parallel [options]

Options:
  -v, --version                    output the version number
  --cypress-run-command <cmd>      specifies the command to run cypress (in non-interactive mode), defaults to 'npx cypress
                                   run' or 'yarn cypress run' depending on how invoked
  --node <index>:<count>           specifies number of buckets and which to run
  --knapsack <path>                specifies the path to the knapsack file (default: "knapsack.json")
  --read-knapsack <path>           specifies the path to the knapsack file to read
  --write-knapsack <path>          specifies the path to the knapsack file to write
  --disable-knapsack-output        disables knapsack output (default: false)
  --unweighed-strategy <strategy>  strategy to utilize for unweighed test files ('estimate' (default) | 'distribute')
                                   (default: "estimate")
  -h, --help                       display help for command

Unrecognized arguments are passed along to Cypress, so arguments such as -e / --env can be used as shown below

$ npx cypress-parallel --env foo=bar

--node <index>:<total>

The utility will automatically pick up node configuration for some CI providers. Otherwise you can specify node index and total node count using --node, as shown below.

$ npx cypress-parallel --node 1:5

--knapsack <file>

Specifies the location of the knapsack file. Defaults to knapsack.json.

--read-knapsack <file>

Optionally specified the location of the knapsack file to read.

--write-knapsack <file>

Optionally specified the location of the knapsack file to write.

--disable-knapsack-output

Disables outputting knapsack data to the file system. This is always disabled when you specify --reporter or --reporter-options to Cypress. If you require custom options and still want to obtain the knapsack output, you need to configure cypress-multi-reporters with @yathomasi/cypress-parallel/knapsack-reporter yourself.

--unweighed-strategy "estimate" | "distribute"

What strategy to utilize if encountering a test file that isn't contained in the knapsack. The "estimate" strategy will estimate expected execution time based off of file length (line numbers). The "distribute" strategy will merely distribute unknown files evenly amongst the nodes.

Custom stragies can be implemented using cusmiconfig, as shown below.

module.export = {
  /** @type {import("@yathomasi/cypress-parallel").UnweighedStrategy} */
  unweighedStrategy(weighedFiles, unweighedFiles, nodeCount) {
    // Implement me.
  },
};

How to handle knapsack.json

The knapsack contains every test file's weight and is instrumental for dividing tests evenly among nodes. This file needs to be available during the run and it should ideally be kept up-to-date. One way of handling this is to check it into version control somewhat regularly. Another way of handling it is to make your CI provider cache the data and re-surface it on subsequent runs. This last approach requires you to combine the knapsack data for every node before caching it though, which may or may not be so easy depending on your provider.

CI configuration

Below is an example of how to configure Gitlab CI to parallelize Cypress tests. Contributions of similar examples for other providers are welcome.

Gitlab CI

This example illustrate two things, 1) running tests in parallel and 2) combining knapsack data into a single, downloadable artifact. The latter is completely optional and you need to decide for yourself how you want to handle this.

test:
  stage: Test (1)
  parallel: 5
  artifacts:
    when: always
    paths:
      - knapsack-$CI_NODE_INDEX.json
    expire_in: 1 day
  script:
    - npx cypress-parallel --write-knapsack "knapsack-$CI_NODE_INDEX.json"

knapsack:
  stage: Test (2)
  script:
    - cat knapsack-*.json | jq -sS add | tee knapsack.json
  artifacts:
    when: always
    paths:
      - knapsack.json
    expire_in: 1 day

Other providers

If your provider does not provide a keyword such as Gitlab's parallel, then you can always simply just create N explicit jobs, similar to that shown below.

test_1:
  stage: Test
  script:
    - npx cypress-parallel --node 1:5

test_2:
  stage: Test
  script:
    - npx cypress-parallel --node 2:5

test_3:
  stage: Test
  script:
    - npx cypress-parallel --node 3:5

test_4:
  stage: Test
  script:
    - npx cypress-parallel --node 4:5

test_5:
  stage: Test
  script:
    - npx cypress-parallel --node 5:5