create-jest-runner
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.jsconst createJestRunner = ;moduleexports = ;
2) Create your run file
module {};
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 testsconfig
: Jest Project config used by this fileglobalConfig
: Jest global config
You can return one of the following values:
testResult
: Needs to be an object of type https://github.com/facebook/jest/blob/master/types/TestResult.js#L131-L157Promise<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.jsconst createJestRunner = ;moduleexports = ;
// run.jsconst fs = ;const pass fail = ; module { const start = +; const contents = fs; const end = +; if contents return ; const errorMessage = 'Company policies require ⚔️ 🏃 in every file'; return ;};
Add your runner to Jest config
Once you have your Jest runner you can add it to your Jest config.
In your package.json
Or in jest.config.js
moduleexports = runner: '/path/to/my-runner'
Run Jest
yarn jest