@arut.ji/jest-runner-groups
v1.0.2
Published
Tag and run groups of tests with Jest
Downloads
19
Readme
@arut.ji/jest-runner-groups
A test runner that allows you to tag your tests and execute specific groups of tests with Jest.
Installation
npm i -D @arut.ji/jest-runner-groupsRequirements
- Jest 30 or higher
- Node.js 18.14.0 or higher (or 20.x, 22.x, or 24+)
Usage
To use this runner you need to tag your tests, add this runner to your jest config and update your test command to specify which groups to run.
Tag your tests
To properly tag your tests, you need to add a docblock with the @group tag to every test file you have. For example, your test should look like the following to belong to the unit/classes/foo group:
/**
* Tests Foo class
*
* @group unit/classes/foo
*/
import Foo from '../src/Foo';
describe('Foo class', () => {
it('...', () => {
...
});
...
});Your tests may have multiple groups per file:
/**
* Admin dashboard tests
*
* @group admin/pages/dashboard
* @group puppeteer
* @group acceptance
*/
describe('Dashboard page', () => {
...
});Update Jest config
To make Jest use this runner, you need to update your Jest config and add @arut.ji/jest-runner-groups runner to it. For example, if your jest config is in the package.json file:
{
"name": "my-package",
"version": "1.0.0",
"jest": {
"runner": "@arut.ji/jest-runner-groups"
}
}Or in the jest.config.js file:
module.exports = {
...
runner: "@arut.ji/jest-runner-groups"
};Or in the jest.config.ts file (TypeScript):
import type { Config } from 'jest';
const config: Config = {
...
runner: "@arut.ji/jest-runner-groups"
};
export default config;Note: There is a confusion between runner and testRunner options in the jest configuration. The main difference between them is that jest uses
runnerto find and execute all tests, andtestRunnerto execute a particular test file. So, if you want to usejest-circus, then add it astestRunneralong with"runner": "@arut.ji/jest-runner-groups"option and they will work together.
Run groups of tests
Once you update your tests and jest config, you can start running tests in groups by using --group argument. Just specify a group or groups that you want to run like this:
// using jest executable:
jest --group=unit
// or via npm:
npm test -- --group=unitYou can use multiple --group arguments to specify multiple groups to run:
npm test -- --group=unit/classes --group=unit/servicesAlso pay attention that if you specify a prefix of a group, then all tests that have a group that starts with that prefix will be executed. In other words, if you run npm test -- --group=unit command, then all tests that have a group that starts with unit will be executed.
Exclude groups
If you want to exclude a subgroup from being executed, add minus character to the beginning of its name. The following example shows how to run all tests in the foo group, but exclude foo/baz group:
jest --group=foo --group=-foo/bazKnowing which groups are running
When you run your tests using @arut.ji/jest-runner-groups, you can check which group is currently running by checking the current process environment variables. This can be handy if you want to use different fixtures for different groups or skip a certain functionality for a specific group.
Each group is added with the JEST_GROUP_ prefix and all non-word characters in the group name are replaced with underscores. For example, if you run the following command:
npm test -- --group=unit/classes --group=unit/servicesThen you can check groups in your jest tests:
/**
* Admin dashboard tests
*
* @group unit/classes
* @group unit/services
* @group unit/utility
*/
it("...", () => {
expect(process.env.JEST_GROUP_UNIT_CLASSES).toBeTruthy();
expect(process.env.JEST_GROUP_UNIT_SERVICES).toBeTruthy();
expect(process.env.JEST_GROUP_UNIT_UTILITY).not.toBeTruthy();
});Migration from jest-runner-groups
If you're migrating from the original jest-runner-groups package:
Update your package name in
package.json:{ "devDependencies": { "@arut.ji/jest-runner-groups": "^1.0.0" } }Update your Jest configuration to use the new package name:
{ "jest": { "runner": "@arut.ji/jest-runner-groups" } }Ensure you're using Jest 30 or higher (this package requires Jest 30+)
Changes from v3.x
- TypeScript implementation: The package is now written in TypeScript with full type definitions
- Jest 30+ required: This version requires Jest 30 or higher
- No jest-docblock dependency: The package now includes its own docblock parser, removing the need for
jest-docblockas a peer dependency - ESM support: The package is built as an ES module
Contribute
Want to help or have a suggestion? Open a new ticket and we can discuss it or submit a pull request.
License
MIT
