> ## Documentation Index
> Fetch the complete documentation index at: https://express-quota-limiter.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Testing quota middleware in isolation with MemoryDriver

> Use the built-in MemoryDriver to write fast, isolated unit and integration tests for quota middleware without a Redis instance or external dependencies.

`express-quota-limiter` ships with a `MemoryDriver` that stores quota state in-process. Because it has no external dependencies and resets instantly with a single method call, it is the recommended storage backend for all automated tests. You can write quota tests that run as fast as regular unit tests without spinning up Redis or any other infrastructure.

## Why MemoryDriver is ideal for tests

* **No infrastructure required** — tests run anywhere Node.js runs, including CI environments with no Redis.
* **Fully synchronous internals** — the driver resolves promises immediately, keeping test execution fast.
* **Controllable state** — `driver.clear()` wipes all counters between tests, preventing state from leaking across test cases.
* **Inspectable state** — `driver.inspect(key)` returns the raw entry so you can assert on the exact remaining value without making extra HTTP requests.

**1. Create a MemoryDriver instance**

Import `MemoryDriver` and create an instance. You can optionally set a `ttlSeconds` to match your production window:

```typescript theme={null}
import { createQuotaLimiter, MemoryDriver } from "express-quota-limiter";

const driver = new MemoryDriver();
```

**2. Create middleware with createQuotaLimiter**

Pass the driver to `createQuotaLimiter` just as you would `RedisDriver` in production:

```typescript theme={null}
const middleware = createQuotaLimiter({
  storage: driver,
  limit: 5,
  keyGenerator: (req) => `test:${req.userId}`,
});
```

**3. Reset state between tests with driver.clear()**

Register an `afterEach` hook to clear the driver so each test starts with a clean slate:

```typescript theme={null}
afterEach(() => driver.clear());
```

## Full example

```typescript theme={null}
import { createQuotaLimiter, MemoryDriver } from "express-quota-limiter";

const driver = new MemoryDriver();
const middleware = createQuotaLimiter({
  storage: driver,
  limit: 5,
  keyGenerator: (req) => `test:${req.userId}`,
});

afterEach(() => driver.clear());
```

## Inspecting remaining quota

`driver.inspect(key)` returns the raw store entry for a key, including the `remaining` count and the `expiresAt` timestamp. Use it to write assertions without relying on response headers:

```typescript theme={null}
it("decrements remaining on each allowed request", async () => {
  // simulate two allowed requests
  await supertest(app).get("/api/resource").set("x-user-id", "user1");
  await supertest(app).get("/api/resource").set("x-user-id", "user1");

  const entry = driver.inspect("test:user1");
  expect(entry?.remaining).toBe(3); // started at 5, used 2
});
```

## Testing quota exceeded behavior

To verify that your route correctly returns a `429` after the limit is reached, make `limit + 1` requests and assert on the final response:

```typescript theme={null}
it("returns 429 after the limit is exhausted", async () => {
  const LIMIT = 5;

  // exhaust the quota
  for (let i = 0; i < LIMIT; i++) {
    await supertest(app).get("/api/resource").set("x-user-id", "user42");
  }

  // the next request should be blocked
  const response = await supertest(app)
    .get("/api/resource")
    .set("x-user-id", "user42");

  expect(response.status).toBe(429);
  expect(response.body.quota.remaining).toBe(0);
});
```

> [!TIP]
>
> Use [supertest](https://github.com/ladjs/supertest) to mount your Express app and send HTTP requests in tests without binding to a real port. It integrates cleanly with Jest and Vitest and lets you assert on status codes, headers, and response bodies in a single fluent chain.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.