Run Jest test suites in groups

j_d picture j_d · May 4, 2018 · Viewed 11.7k times · Source

I am writing extensive tests for a new API via Jest and supertest. Prior to running the tests, I am setting up a test database and populating it with users:

Test command

jest --forceExit --config src/utils/testing/jest.config.js

File jest.config.js

module.exports = {
  rootDir: process.cwd(),

  // Sets up testing database with users
  globalSetup: './src/utils/testing/jest.setup.js',

  // Ensures connection to database for all test suites
  setupTestFrameworkScriptFile: './src/utils/testing/jest.db.js',

}

So I am starting with a database of some users to test on. The problem is this:

Some of my tests rely upon the success of other tests. In this application, users can upload images, and group them into packs. So my grouping endpoint suite depends upon the success of my image upload suite, and so on.

I am well aware many people might say this is bad practice, and that tests should not rely upon other tests. That being said, I would really rather keep all my tests via supertest, and not get into dependency injection, etc. I don't want to have to meticulously set up testing conditions (for example, creating a bunch of user images artificially before running the tests), because: (1) this is just duplication of logic, and (2) it increases the possibility of something breaking.

Is there any way to group jest suites? For example, to run suites in order:

jest run creationSuite
jest run modificationSuite

This way, all my "creationSuite" tests could be run simultaneously, and success of all would then trigger the "modificationSuite" to run, etc., in a fail-fast manner.

Alternatively, specifying inside a test suite dependencies on other test suites would be great:

describe('Grouping endpoint', () => {
    // Somehow define dependencies
    this.dependsOn(uploadSuite)

Answer

Eugene Manuilov picture Eugene Manuilov · Oct 21, 2019

You can use jest-runner-groups to define and run tests in groups. Once it's installed and added to the Jest configuration, you can tag your tests using docblock notation, like here:

/**
 * Foo tests
 *
 * @group group1/subgroup1
 * @group unit/foo
 */

describe( 'Foo class', () => {
    ...
} );
/**
 * Bar tests
 *
 * @group group1/subgroup2
 * @group unit/bar
 */

describe( 'Bar class', () => {
    ...
} );

Update your jest configuration to specify a new runner:

// jest.config.js
module.exports = {
    ...
    runner: "groups"
};

Then to run a specific group, you need to use --group= argument:

// Using the Jest executable
jest --group=mygroup

// Or npm
npm test -- --group=mygroup

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

// Will execute tests in the unit/bar and unit/foo groups
npm test -- --group=unit/bar --group=unit/foo

// Will execute tests in the unit group (including unit/bar and unit/foo groups)
npm test -- --group=unit