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

create-jest-runner

v0.12.3

Published

A simple way of creating a Jest runner

Downloads

902,472

Readme

create-jest-runner

Actions Status

A highly opinionated way for creating Jest Runners

Install

yarn add create-jest-runner

Usage

create-jest-runner takes care of handling the appropriate parallelization and creating a worker farm for your runner.

You simply need two files:

  • Entry file: Used by Jest as an entrypoint to your runner.
  • Run file: Runs once per test file, and it encapsulates the logic of your runner

1) Create your entry file

// index.js
const { createJestRunner } = require('create-jest-runner');
module.exports = createJestRunner(require.resolve('./run'));

createJestRunner(pathToRunFile, config?: { getExtraOptions })

  • pathToRunFile: path to your run file. This must be an absolute path or a file:// URL.
  • config: Optional argument for configuring the runner.
    • getExtraOptions: () => object used for passing extra options to the runner. It needs to be a serializable object because it will be send to a different Node process.

2) Create your run file

module.exports = options => {};

Run File API

This file should export a function that receives one parameter with the options

options: { testPath, config, globalConfig }

  • testPath: Path of the file that is going to be tests
  • config: Jest Project config used by this file
  • globalConfig: Jest global config
  • extraOptions: The return value of the { getExtraOptions } argument of createJestRunner(...) the entry file.

You can return one of the following values:

  • testResult: Needs to be an object of type https://github.com/facebook/jest/blob/4d3c1a187bd429fd8611f6b0f19e4aa486fa2a85/packages/jest-test-result/src/types.ts#L103-L135
  • Promise<testResult|Error>: needs to be of above type.
  • Error: good for reporting system error, not failed tests.

Example of a runner

This runner "blade-runner" makes sure that these two emojis ⚔️ 🏃 are present in every file

// index.js
const { createJestRunner } = require('create-jest-runner');
module.exports = createJestRunner(require.resolve('./run'));
// run.js
const fs = require('fs');
const { pass, fail } = require('create-jest-runner');

/** @type {import('create-jest-runner').RunTest} */
const runTest = ({ testPath }) => {
  const start = Date.now();
  const contents = fs.readFileSync(testPath, 'utf8');
  const end = Date.now();

  if (contents.includes('⚔️🏃')) {
    return pass({ start, end, test: { path: testPath } });
  }
  const errorMessage = 'Company policies require ⚔️ 🏃 in every file';
  return fail({
    start,
    end,
    test: { path: testPath, errorMessage, title: 'Check for ⚔️ 🏃' },
  });
};

module.exports = runTest;

Create runner from binary

yarn create jest-runner my-runner

# Or with npm
npm init jest-runner my-runner

Note: You will have to update the package name in package.json of the generated runner.

Add your runner to Jest config

Once you have your Jest runner you can add it to your Jest config.

In your package.json

{
  "jest": {
    "runner": "/path/to/my-runner"
  }
}

Or in jest.config.js

module.exports = {
  runner: '/path/to/my-runner',
};

Run Jest

yarn jest