Filtering tests
Rstest provides a variety of flexible ways to filter and select which test files and test cases to run. You can precisely control the test scope via configuration files, command-line arguments, and test APIs.
Filter by file name
Run all tests:
Run a specific test file:
Use glob patterns:
Run test files with foo in the name, such as foo.test.ts, foo/index.test.ts, foo-bar/index.test.ts, etc.:
You can also specify multiple test files or glob patterns:
Filter by file path
This method also applies to filtering by file path.
When filtering by file path, it can be either an absolute path or a relative path based on the current working directory (also called the project root directory or workspace root directory)
include/exclude
When you filter files directly with rstest **/*.test.ts, rstest will further filter files based on the include and exclude configuration.
You can modify the test file scope using the include and exclude options.
For example, to match test files named index in the test/a directory:
To match test files in test/a or test/b directories:
Filter by test name
If you only want to run some test cases, you can use the --testNamePattern (-t for short) option. It will run tests with names that match the given pattern.
Match by keyword
This is the simplest way. It will run test cases or test suites whose full names contain the given keyword.
For example, to only run test cases whose names contain "bar":
If a suite name matches, all tests inside it will be run.
You can also provide multiple keywords separated by spaces. This will match test cases where all keywords appear in the full name. For example, to run tests where 'suite-bar' appears in the suite path and 'test-baz' is in the test name:
Match by full test name
You can also provide a full test name to run a specific test case.
Rstest uses > to connect test suite and test case names for better output readability. The full test name is the concatenation of the names of the test suites from the outside in and the test case name, separated by >.
For example, consider the following test structure and its output:
Output:
To run only the test-baz test case in suite-bar, you can use:
Match by regular expression
The pattern is treated as a regular expression. You can use this for more complex filtering.
For example, if you want to precisely match suite-bar > test-baz, rather than any test containing this string, you can use the regex start ^ and end $ anchors:
Filter by project name
Rstest supports defining multiple test projects via projects. You can filter to run specific projects with the --project option.
For example, to match projects whose name is @test/a or @test/b:
You can also use wildcards to match project names:
You can exclude certain projects by negation:
Combined filtering
All filtering methods can be combined. For example:
In this case, rstest will only run test cases whose names contain login in all .test.ts files under the test directory, while excluding the test/legacy directory.
Common usage
-
Run a specific file only:
rstest test/foo.test.ts -
Run tests in a specific directory only:
rstest test/api/*.test.ts -
Exclude certain tests:
rstest --exclude test/legacy/** -
Run only tests whose names contain login:
rstest -t login -
Combined filtering:
rstest test/**/*.test.ts --exclude test/legacy/** --testNamePattern login
Filter via test API
Use the .only modifier to run only certain test suites or cases.
For example, only the test cases in suite A and case A will be run:
It should be noted that the .only flag only applies to the current test file. If you want to execute specific test cases within a specific file, you can use a combination of "filter by file name" and "filter via test API".
Use .skip or .todo to skip certain test suites or cases.