> Documentation index: https://firebase.google.com/docs/reference/emulator-suite/rules-unit-testing/llms.txt

An object used to control the rules unit test environment. Can be used to create RulesTestContext for different authentication situations.

**Signature:**

```typescript
export interface RulesTestEnvironment 
```

## Properties

| Property | Type | Description |
|---|---|---|
| [emulators](https://firebase.google.com/docs/reference/emulator-suite/rules-unit-testing/rules-unit-testing.rulestestenvironment.md.txt#rulestestenvironmentemulators) | { database?: [HostAndPort](https://firebase.google.com/docs/reference/emulator-suite/rules-unit-testing/rules-unit-testing.hostandport.md.txt#hostandport_interface); firestore?: [HostAndPort](https://firebase.google.com/docs/reference/emulator-suite/rules-unit-testing/rules-unit-testing.hostandport.md.txt#hostandport_interface); storage?: [HostAndPort](https://firebase.google.com/docs/reference/emulator-suite/rules-unit-testing/rules-unit-testing.hostandport.md.txt#hostandport_interface); } | A readonly copy of the emulator config specified or discovered at test environment creation. |
| [projectId](https://firebase.google.com/docs/reference/emulator-suite/rules-unit-testing/rules-unit-testing.rulestestenvironment.md.txt#rulestestenvironmentprojectid) | string | The project ID specified or discovered at test environment creation. |

## Methods

| Method | Description |
|---|---|
| [authenticatedContext(user_id, tokenOptions)](https://firebase.google.com/docs/reference/emulator-suite/rules-unit-testing/rules-unit-testing.rulestestenvironment.md.txt#rulestestenvironmentauthenticatedcontext) | Create a `RulesTestContext` which behaves like an authenticated Firebase Auth user.Requests created via the returned context will have a mock Firebase Auth token attached. |
| [cleanup()](https://firebase.google.com/docs/reference/emulator-suite/rules-unit-testing/rules-unit-testing.rulestestenvironment.md.txt#rulestestenvironmentcleanup) | At the very end of your test code, call the cleanup function. Destroy all RulesTestContexts created in test environment and clean up the underlying resources, allowing a clean exit.This method does not change the state in emulators in any way. To reset data between tests, see `clearDatabase()`, `clearFirestore()` and `clearStorage()`. |
| [clearDatabase()](https://firebase.google.com/docs/reference/emulator-suite/rules-unit-testing/rules-unit-testing.rulestestenvironment.md.txt#rulestestenvironmentcleardatabase) | Clear all data in the Realtime Database emulator namespace. |
| [clearFirestore()](https://firebase.google.com/docs/reference/emulator-suite/rules-unit-testing/rules-unit-testing.rulestestenvironment.md.txt#rulestestenvironmentclearfirestore) | Clear data in the Firestore that belongs to the `projectId` in the Firestore emulator. |
| [clearStorage()](https://firebase.google.com/docs/reference/emulator-suite/rules-unit-testing/rules-unit-testing.rulestestenvironment.md.txt#rulestestenvironmentclearstorage) | Clear Storage files and metadata in all buckets in the Storage emulator. |
| [unauthenticatedContext()](https://firebase.google.com/docs/reference/emulator-suite/rules-unit-testing/rules-unit-testing.rulestestenvironment.md.txt#rulestestenvironmentunauthenticatedcontext) | Create a `RulesTestContext` which behaves like client that is NOT logged in via Firebase Auth.Requests created via the returned context will not have Firebase Auth tokens attached. |
| [withSecurityRulesDisabled(callback)](https://firebase.google.com/docs/reference/emulator-suite/rules-unit-testing/rules-unit-testing.rulestestenvironment.md.txt#rulestestenvironmentwithsecurityrulesdisabled) |   |

## RulesTestEnvironment.emulators

A readonly copy of the emulator config specified or discovered at test environment creation.

**Signature:**

```typescript
readonly emulators: {
        database?: HostAndPort;
        firestore?: HostAndPort;
        storage?: HostAndPort;
    };
```

## RulesTestEnvironment.projectId

The project ID specified or discovered at test environment creation.

**Signature:**

```typescript
readonly projectId: string;
```

## RulesTestEnvironment.authenticatedContext()

Create a `RulesTestContext` which behaves like an authenticated Firebase Auth user.

Requests created via the returned context will have a mock Firebase Auth token attached.

**Signature:**

```typescript
authenticatedContext(user_id: string, tokenOptions?: TokenOptions): RulesTestContext;
```

### Parameters

| Parameter | Type | Description |
|---|---|---|
| user_id | string | the User ID of the user. Specifies the value of "user_id" and "sub" on the token |
| tokenOptions | [TokenOptions](https://firebase.google.com/docs/reference/emulator-suite/rules-unit-testing/rules-unit-testing.md.txt#tokenoptions) | custom claims or overrides for Firebase Auth token payloads |

**Returns:**

[RulesTestContext](https://firebase.google.com/docs/reference/emulator-suite/rules-unit-testing/rules-unit-testing.rulestestcontext.md.txt#rulestestcontext_interface)

### Example

```javascript
const alice = testEnv.authenticatedContext('alice');
await assertSucceeds(getDoc(alice.firestore(), '/doc/readable/by/alice'), { ... });

```

## RulesTestEnvironment.cleanup()

At the very end of your test code, call the cleanup function. Destroy all RulesTestContexts created in test environment and clean up the underlying resources, allowing a clean exit.

This method does not change the state in emulators in any way. To reset data between tests, see `clearDatabase()`, `clearFirestore()` and `clearStorage()`.

**Signature:**

```typescript
cleanup(): Promise<void>;
```

**Returns:**

Promise\<void\>

## RulesTestEnvironment.clearDatabase()

Clear all data in the Realtime Database emulator namespace.

**Signature:**

```typescript
clearDatabase(): Promise<void>;
```

**Returns:**

Promise\<void\>

## RulesTestEnvironment.clearFirestore()

Clear data in the default Firestore database for `projectId` in the Firestore emulator.

**Signature:**

```typescript
clearFirestore(): Promise<void>;
```

**Returns:**

Promise\<void\>

## RulesTestEnvironment.clearStorage()

Clear Storage files and metadata in the active bucket in the Storage emulator.

**Signature:**

```typescript
clearStorage(): Promise<void>;
```

**Returns:**

Promise\<void\>

## RulesTestEnvironment.unauthenticatedContext()

Create a `RulesTestContext` which behaves like client that is NOT logged in via Firebase Auth.

Requests created via the returned context will not have Firebase Auth tokens attached.

**Signature:**

```typescript
unauthenticatedContext(): RulesTestContext;
```

**Returns:**

[RulesTestContext](https://firebase.google.com/docs/reference/emulator-suite/rules-unit-testing/rules-unit-testing.rulestestcontext.md.txt#rulestestcontext_interface)

### Example

```javascript
const unauthed = testEnv.unauthenticatedContext();
await assertFails(getDoc(unauthed.firestore(), '/private/doc'), { ... });

```

## RulesTestEnvironment.withSecurityRulesDisabled()

**Signature:**

```typescript
withSecurityRulesDisabled(callback: (context: RulesTestContext) => Promise<void>): Promise<void>;
```

### Parameters

| Parameter | Type | Description |
|---|---|---|
| callback | (context: [RulesTestContext](https://firebase.google.com/docs/reference/emulator-suite/rules-unit-testing/rules-unit-testing.rulestestcontext.md.txt#rulestestcontext_interface)) =\> Promise\<void\> |   |

**Returns:**

Promise\<void\>