> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# includeSource

- **Type:** `string[]`
- **Default:** `[]`

In-source testing is where the test code lives within the same file as the source code, similar to [Rust's module tests](https://doc.rust-lang.org/book/ch11-03-test-organization.html#the-tests-module-and-cfgtest).

You can define a list of glob patterns that match your in-source test files via `includeSource` configuration.

```ts
import { defineConfig } from '@rstest/core';

export default defineConfig({
  includeSource: ['src/**/*.{js,ts}'],
});
```

:::tip
**In-source testing** is usually suitable for small functional functions and utilities, allowing for easy and rapid verification and debugging. For more complex functions and modules, independent test files are recommended.
:::

`includeSource` works in both Node mode and [browser mode](/guide/browser-testing.md) projects.

### Writing in-source tests

When `includeSource` defined, Rstest will run all matched files with `import.meta.rstest` inside.

You can get the Rstest test API via `import.meta.rstest`.

```ts title=src/helper.ts
export const sayHi = () => 'hi';

if (import.meta.rstest) {
  const { it, expect } = import.meta.rstest;
  it('should test source code correctly', () => {
    expect(sayHi()).toBe('hi');
  });
}
```

### For production

Put the test code inside the `if (import.meta.rstest)` block. When generating non-test output, define `import.meta.rstest` as `false` in your project's build configuration so the bundler can eliminate the test code as dead code.

- Use Rspack's [DefinePlugin](https://rspack.rs/plugins/webpack/define-plugin):

```diff title=rspack.config.ts
import { defineConfig } from '@rspack/cli';
+import { rspack } from '@rspack/core';

export default defineConfig({
+  plugins: [
+    new rspack.DefinePlugin({
+      'import.meta.rstest': false,
+    }),
+  ],
});
```

- Use Rsbuild's [source.define](https://rsbuild.rs/config/source/define) option:

```diff title=rsbuild.config.ts
import { defineConfig } from '@rsbuild/core';

export default defineConfig({
  source: {
    define: {
+      'import.meta.rstest': false,
    },
  },
});
```

### TypeScript

To get TypeScript support for `import.meta.rstest`, we recommend adding `@rstest/core/importMeta` to `compilerOptions.types` in the `tsconfig.json` for your test files:

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

> If your `tsconfig.json` already configures `compilerOptions.types`, append this type to the existing list.

Alternatively, reference the type in `src/rstestEnv.d.ts`:

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