npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@saritvakrat/jest-runner-groups

v3.2.10

Published

Tag and run groups of tests with Jest

Downloads

5

Readme

jest-runner-groups

Version Downloads/week License

A test runner that allows you to tag your tests and execute specific groups of tests with Jest. This runner is based on the original repository that was archived

Installation

npm i -D jest-runner-groups

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 groups runner to it. For example, if your jest config is in the package.json file:

{
    "name": "my-package",
    "version": "1.0.0",
    "dependencies": {
    },
    "jest": {
        "runner": "jest-runner-grouped-tests"
    }
}

Or in the jest.config.js file:

module.exports = {
    ...
    runner: "jest-runner-grouped-tests"
};

Note: There is a confusion between runner and testRunner options in the jest configuration. The main difference between them is that jest uses runner to find and execute all tests, and testRunner to execute a particular test file. So, if you want to use jest-circus, then add it as testRunner along with "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=unit

You can use multiple --group arguments to specify multiple groups to run:

npm test -- --group=unit/classes --group=unit/services

Also 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 beginnig 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/baz

Knowing which groups are running

When you run your tests using 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/services

Then 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();
} );

Run group of tests using a regular expressions

You can use --regex argument to specify multiple groups by a regular expression:

jest --regex=foo/.\*

You can use multiple --regex arguments:

jest --regex=foo/.\* --regex=bar\|baz

Or use both --regex and --group:

jest --regex=foo/.\* --group=bar

Exclude groups using regular expression

If you want to exclude tests from being executed, add minus character to the beginnig of your regular expression.

The following example shows how to run all tests with a group's name contains foo, but exclude all groups that foo/baz/.* match their name:

jest --regex=foo --regex=-foo/baz/.\*

Must Include Groups

The mustIncludeGroups functionality allows you to specify groups that must be included for a test to be run. If any of these groups are missing from the test's tags, the test will be excluded from the run. This is useful for ensuring certain essential groups are always included in the test execution criteria.

To use the mustIncludeGroups functionality, add the ! prefix to the group name in the command line argument. For example, if you want to run tests that must include the core group:

jest --group=unit --group=!core

You can combine mustIncludeGroups with other group and regex arguments for more refined control:

jest --group=unit --group=!core --regex=foo/.*

Contribute

Want to help or have a suggestion? Open a new ticket and we can discuss it or submit a pull request.

License

MIT