
Multiple approaches on running a subset of Jest tests within a project.
I’ve come across two primary methods for running subsets of Jest tests within a single project. Let’s take a look.
First, why would you do this? I’ve run into a number of scenarios in which I want to run a certain set of tests. Here are a few examples:
Fortunately, Jest makes this a super simple process.
Let’s demonstrate this with a super simple project. Say I have a single index.js file with two functions — method1() returns true and method2() returns false.
index.js
exports.method1 = function () {
return true;
};
exports.method2 = function () {
return false;
};And (for whatever reason) I want to test these methods in isolation in two different test suites, like this:
__tests__/suite1/index.test.js
const { method1 } = require("../../index.js");
describe("method1()", () => {
it("returns true", () => {
expect(method1()).toEqual(true);
});
});__tests__/suite2/index.test.js
const { method2 } = require("../../index.js");
describe("method2()", () => {
it("returns false", () => {
expect(method2()).toEqual(false);
});
});Now let’s look at how we could run these in isolation, assuming Jest is installed.
The more flexible approach is to use the CLI. Assuming you have a test string in your package.json:
{
// ...
"scripts": {
"test": "jest"
},
"dependencies": {
"jest": "^27.0.6"
}
}You can target a single file or directory as the first argument with the jest command. If using an npm script, that looks like this:
npm test -- __tests__/suite1Pay close attention to the -- here. This is necessary for passing arguments to the npm script. If you’re running jest globally, you don’t need them, but could instead run jest __tests__/suite1.
That will run only tests found within that path, returning something like the following output:
$ jest __tests__/suite1/
PASS __tests__/suite1/index.test.js
method1()
✓ returns true (28 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.212 s, estimated 1 s
Ran all test suites matching /__tests__\/suite1\//i.This method is really great for being quick and flexible. If you aren’t running the same grouping of tests repeatedly, this is a good approach.
A more permanent option is to build a dynamic configuration file that looks for an environment variable to determine which suites to run.
Let’s say we were looking for an environment variable called JEST_SUITE and it should be set to 1 or 2. We could do something like this:
jest.config.js
module.exports = {
testMatch: [
`<rootDir>/__tests__/suite${process.env.JEST_SUITE}/*.(spec|test).[jt]s`,
],
};Now you can run the second test suite like this:
$ jest
PASS __tests__/suite2/index.test.js
method2()
✓ returns false (28 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.227 s
Ran all test suites.Dynamic config is a more permanent solution. This is best used when you are intentionally splitting up tests for a specific purpose and for an extended period of time.
I’ve set up a playground that includes both scenarios within the same project. Take a look below: