@onerepo/plugin-jest
Jest is a good framework for headless testing with oneRepo. Jest works well with monorepos because it allows a single runner that can use multiple project configurations. Each workspace in your repository can be a Jest project, enabling Jest to determine which tests to run.
The added benefit of Jest is that as you are working, you can run this single command and it will automatically test appropriate files related to your changes across all workspaces. There’s no need to determine which workspaces to run – and can all be done with --watch
mode at the same time.
Installation
Section titled InstallationCreate a root level jest config with the appropriate references to your workspace Jest configs:
Configuration
Section titled Configurationjest()
Section titled jest()Include the jest
plugin in your oneRepo plugin setup:
Parameters
Section titled ParametersParameter | Type |
---|---|
opts | Options |
Options
Section titled OptionsOptions for configuring the Jest plugin for oneRepo.
- Default:
{}
Type declaration
Section titled Type declarationconfig?
Section titled config?Specify the main Jest configuration file, if different from <repo>/jest.config.js
. This can be relative to the repository root.
name?
Section titled name?- Default:
'jest'
Rename the default command name. This configuration is recommended, but not provided, to avoid potential conflicts with other commands.
passWithNoTests?
Section titled passWithNoTests?- Default:
true
Automatically include Jests’s flag --passWithNoTests
when running.
Recommended tasks
Section titled Recommended tasksCommands
Section titled Commandsone jest
Section titled one jestAliases: one test
Run tests using Jest.
This test commad will automatically attempt to run only the test files related to the changes in your working state. If you have un-committed changes, only those related to files that are in a modified state will be run. If there are no un-committed changes, test files related to those modified since your git merge-base will be run. By passing specific filepaths as extra passthrough arguments an argument separator (two dasshes --
), you can further restrict the tests to those files and paths.
Additionally, any other Jest CLI options can be passed as passthrough arguments as well after an argument separator (two dashes --
)
Option | Type | Description |
---|---|---|
--affected | boolean | Select all affected Workspaces. If no other inputs are chosen, this will default to true . |
--all, -a | boolean | Run across all Workspaces |
--inspect | boolean | Break for the the Node inspector to debug tests. |
--pretty | boolean , default: true | Control Jest’s --colors flag. |
--staged | boolean | Use files on the git stage to calculate affected files or Workspaces. When unset or --no-staged , changes will be calculated from the entire branch, since its fork point. |
--watch | boolean | Shortcut for jest --watch mode. |
--workspaces, -w | array | List of Workspace names to run against |
Advanced options
Option | Type | Description |
---|---|---|
--config | string , default: "./jest.config.js" | Path to the jest.config file, relative to the repo root. |
--from-ref | string | Git ref to start looking for affected files or Workspaces |
--passWithNoTests | boolean , default: true | Allows the test suite to pass when no files are found. See plugin configuration to disable. |
--show-advanced | boolean | Pair with --help to show advanced options. |
--through-ref | string | Git ref to start looking for affected files or Workspaces |
Run only tests related to modified files.
Runs jest in —watch mode against the currently affected files.
Run Jest in watch mode against a particular file.
Pass any other Jest CLI options after the argument separator.