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/config/test/include-source.md.
close
  • English
  • includeSource

    • Type: string[]
    • Default: []

    Glob patterns for files that contain in-source tests alongside source code, similar to Rust's module tests. Patterns are resolved relative to root and respect exclude.

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      includeSource: ['src/**/*.{js,ts}'],
    });

    In-source testing suits small utilities that benefit from living next to their implementation. Use dedicated test files for larger modules or suites with complex setup.

    includeSource works in both Node mode and Browser Mode projects.

    Writing in-source tests

    Rstest scans files matching includeSource and runs only the ones that reference import.meta.rstest, which exposes the Rstest test API (it, test, describe, expect, and hooks):

    src/helper.ts
    export const sayHi = () => 'hi';
    
    if (import.meta.rstest) {
      const { it, expect } = import.meta.rstest;
      it('should return hi', () => {
        expect(sayHi()).toBe('hi');
      });
    }

    For production

    Keep test code inside an if (import.meta.rstest) block. In production builds, define import.meta.rstest as false so the bundler tree-shakes the test block as dead code:

    rspack.config.ts
    import { defineConfig } from '@rspack/cli';
    +import { rspack } from '@rspack/core';
    
    export default defineConfig({
    +  plugins: [
    +    new rspack.DefinePlugin({
    +      'import.meta.rstest': false,
    +    }),
    +  ],
    });
    rsbuild.config.ts
    import { defineConfig } from '@rsbuild/core';
    
    export default defineConfig({
      source: {
        define: {
    +      'import.meta.rstest': false,
        },
      },
    });

    TypeScript

    Add @rstest/core/importMeta to compilerOptions.types in your tsconfig.json for import.meta.rstest type definitions:

    tsconfig.json
    {
      "compilerOptions": {
        "types": ["@rstest/core/importMeta"]
      }
    }

    If compilerOptions.types is already set, append @rstest/core/importMeta to the array.

    Alternatively, add a triple-slash reference in an ambient declaration file:

    src/rstestEnv.d.ts
    /// <reference types="@rstest/core/importMeta" />