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:2
Create middleware with createQuotaLimiter
Pass the driver to
createQuotaLimiter just as you would RedisDriver in production:3
Reset state between tests with driver.clear()
Register an
afterEach hook to clear the driver so each test starts with a clean slate:Full example
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:
Testing quota exceeded behavior
To verify that your route correctly returns a429 after the limit is reached, make limit + 1 requests and assert on the final response: