1. create-jest-runner
A simple way of creating a Jest runner
create-jest-runner
Package: create-jest-runner
Created by: jest-community
Last modified: Mon, 06 Mar 2023 13:44:33 GMT
Version: 0.12.3
License: MIT
Downloads: 1,004,600
Repository: https://github.com/jest-community/create-jest-runner

Install

npm install create-jest-runner
yarn add create-jest-runner

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

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code