Skip to content

@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.

Install via npm
npm install --save-dev @onerepo/plugin-jest

Create a root level jest config with the appropriate references to your workspace Jest configs:

jest.config.js
/** @type {import('jest').Config} */
export default {
projects: ['<rootDir>/apps/*/jest.config.js', '<rootDir>/modules/*/jest.config.js'],
};
jest(opts): Plugin

Include the jest plugin in your oneRepo plugin setup:

onerepo.config.ts
import { jest } from '@onerepo/plugin-jest';
export default {
plugins: [jest()],
};
ParameterType
optsOptions
type Options: {
config: string;
name: string | string[];
passWithNoTests: boolean;
};

Options for configuring the Jest plugin for oneRepo.

onerepo.config.js
export default {
plugins: [
jest({
// optional configuration
}),
],
});
  • Default: {}
optional config: string;

Specify the main Jest configuration file, if different from <repo>/jest.config.js. This can be relative to the repository root.

onerepo.config.js
export default {
plugins: [
jest({
config: 'configs/jest.config.js'
}),
],
});
optional name: string | string[];
  • Default: 'jest'

Rename the default command name. This configuration is recommended, but not provided, to avoid potential conflicts with other commands.

onerepo.config.js
export default {
plugins: [
jest({
name: ['test', 'jest']
}),
],
});
optional passWithNoTests: boolean;
  • Default: true

Automatically include Jests’s flag --passWithNoTests when running.

onerepo.config.js
export default {
plugins: [
jest({
passWithNoTests: false,
}),
],
});
./onerepo.config.ts
import type { Config } from 'onerepo';
export default {
tasks: {
'pre-commit': {
serial: ['$0 jest'],
},
'pre-merge': {
serial: ['$0 jest -a'],
},
},
} satisfies Config;

Aliases: one test

Run tests using Jest.

Terminal window
one jest [options...] -- [passthrough]
one jest [options...]

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 --)

OptionTypeDescription
--affectedbooleanSelect all affected Workspaces. If no other inputs are chosen, this will default to true.
--all, -abooleanRun across all Workspaces
--inspectbooleanBreak for the the Node inspector to debug tests.
--prettyboolean, default: trueControl Jest’s --colors flag.
--stagedbooleanUse 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.
--watchbooleanShortcut for jest --watch mode.
--workspaces, -warrayList of Workspace names to run against
Advanced options
OptionTypeDescription
--configstring, default: "./jest.config.js"Path to the jest.config file, relative to the repo root.
--from-refstringGit ref to start looking for affected files or Workspaces
--passWithNoTestsboolean, default: trueAllows the test suite to pass when no files are found. See plugin configuration to disable.
--show-advancedbooleanPair with --help to show advanced options.
--through-refstringGit ref to start looking for affected files or Workspaces

Run only tests related to modified files.

Terminal window
one jest ,test

Runs jest in —watch mode against the currently affected files.

Terminal window
one jest ,test --watch

Run Jest in watch mode against a particular file.

Terminal window
one jest ,test --watch -- path/to/test.ts

Pass any other Jest CLI options after the argument separator.

Terminal window
one jest ,test -- --runInBand --detectOpenHandles