For AI agents: the complete documentation index is available at https://rstest.rs/llms.txt, the full documentation bundle is available at https://rstest.rs/llms-full.txt, and this page is available as Markdown at https://rstest.rs/guide/basic/test-filter.md.
close
  • English
  • Filtering tests

    Select which test files, projects, and test cases Rstest runs via CLI filters, configuration options, watch shortcuts, or test API modifiers.

    Filter by file name

    Run all tests:

    rstest

    Positional filters accept substrings, relative or absolute file paths, and directory paths (multiple filters match any of them):

    rstest foo
    rstest test/foo.test.ts test/bar.test.ts
    rstest packages/core

    Wrap a filter in quotes for exact whole-path matching (for example, to avoid matching foo.test.tsx when targeting foo.test.ts). Because the shell strips one layer of quotes, pass two layers:

    rstest '"test/foo.test.ts"'

    Rstest does not parse positional filters as glob patterns. Use --include for glob patterns, quoted to prevent shell expansion:

    rstest --include "test/**/*.test.ts"

    include/exclude

    Positional filters apply on top of the files matched by the include and exclude configuration. You can also override include and exclude from the CLI:

    # Match test files named index under test/a
    rstest index --include "test/a/*.test.ts"
    
    # Match test files under test/a or test/b
    rstest --include "test/a/*.test.ts" --include "test/b/*.test.ts"
    
    # Exclude a directory
    rstest --exclude "test/legacy/**"

    By default, Rstest exits with an error when no matching test files are found. Pass --passWithNoTests (or enable passWithNoTests in your config) to exit with code 0 instead.

    Filter by test name

    Use --testNamePattern (-t for short) or the testNamePattern config option to run only tests whose names match a string or regular expression.

    Match by keyword

    Pass a keyword to run any test case or suite whose name contains it. If a suite name matches, all tests inside that suite run:

    rstest -t bar

    Rstest compiles -t / testNamePattern into a single RegExp. When the pattern does not contain >, Rstest joins outer-to-inner suite names and the test name with a single space (suite-foo suite-bar test-baz). As a result, a space-separated string matches that exact consecutive sequence across suite and test boundaries:

    # Matches "suite-foo > suite-bar > test-baz",
    # not "suite-foo > suite-bar > suite-bar1 > test-baz"
    rstest -t 'suite-bar test-baz'

    To match non-adjacent keywords across nested suites, join them with .*:

    rstest -t 'suite-bar.*test-baz'

    Match by full test name

    In terminal output, Rstest joins outer-to-inner suite names and the test case name with >. When a pattern contains >, Rstest matches against that >-delimited full name.

    Consider the following test file and its output:

    foo.test.ts
    import { describe, test } from '@rstest/core';
    
    describe('suite-foo', () => {
      describe('suite-bar', () => {
        test('test-baz', () => {
          // ...
        });
    
        describe('suite-bar1', () => {
          test('test-baz', () => {
            // ...
          });
        });
      });
    });

    Output:

    ✓ foo.test.ts
      ✓ suite-foo > suite-bar > test-baz
      ✓ suite-foo > suite-bar > suite-bar1 > test-baz

    To run only the test-baz test directly inside suite-bar:

    rstest foo.test.ts -t 'suite-foo > suite-bar > test-baz'

    Match by regular expression

    Because the pattern is evaluated as a regular expression, you can use anchors such as ^ and $ to match a full test name exactly instead of any substring match:

    rstest foo.test.ts -t '^suite-foo > suite-bar > test-baz$'

    You can also set a RegExp directly in rstest.config.ts via testNamePattern, such as /^(?!.*slow).*$/ to exclude tests containing slow.

    Filter by project name

    In a multi-project workspace configured with projects, use --project to select projects by their name.

    Match specific project names:

    rstest --project '@test/a' --project '@test/b'

    Match project names with * wildcards:

    rstest --project '@test/*'

    Exclude projects with a leading !:

    rstest --project '!@test/a'

    Failed tests, changes, and shards

    • Re-run failed test files: Pass -f or --onlyFailures (see onlyFailures) to re-run only test files that failed in the previous full run.
    • Run tests related to source files: Pass --related (or --findRelatedTests) with source file paths to run tests that depend on them; see Run related tests.
    • Run tests for Git changes: Pass --changed to run tests affected by local or committed Git changes; see Run changed tests.
    • Split tests across CI jobs: Pass --shard <index>/<count> to distribute test files across shards; see Sharding tests.
    • Interactive watch filtering: In watch mode, press t to filter by test name pattern, p to filter by file filters, f to re-run failed files, or a to clear active filters and re-run all tests; see CLI shortcuts.

    Combined filtering

    File, project, and test name filters can be combined:

    rstest --include "test/**/*.test.ts" --exclude "test/legacy/**" --testNamePattern login

    This command runs only tests whose names contain login across .test.ts files under test/, excluding test/legacy/.

    Filter via test API

    Control which suites and test cases run directly in test code with test and describe modifiers:

    • .only: runs only the marked suites or test cases inside the current file (combine with a CLI file filter to isolate one file).
    • .skip / .todo: skips suites or test cases, or marks them as todo.
    • .skipIf(condition) / .runIf(condition): conditionally skips or runs suites or test cases.
    • context.skip(): skips a test dynamically during execution.
    import { describe, test } from '@rstest/core';
    
    describe.only('suite A', () => {
      test('runs only this suite in the file', () => {});
    });
    
    describe.skip('suite B', () => {});
    test.todo('pending case');
    test.skipIf(process.platform === 'win32')('POSIX only', () => {});
    test.runIf(Boolean(process.env.CI))('CI only', () => {});
    
    test('skips dynamically', (context) => {
      if (!process.env.API_TOKEN) {
        context.skip();
      }
    });