TestDriver(context)fromtestdriverai/vitest/hooks. Use this in Vitest tests. It connects and disconnects for you.new TestDriver()fromtestdriverai. Use this outside Vitest’s per-test hooks, when you manage the connection yourself.
TestDriver(context, options)
Creates a client for the current Vitest test.Parameters
TestContext
required
The context Vitest passes to your test function, as in
async (context) => { ... }. TestDriver throws if it’s missing.TestDriverOptions
Client options, such as
os, preview, cache, or keepAlive. See Options for the full list.What it does
- Connects automatically. It authenticates and connects to a sandbox in the background. Your first command waits for the connection.
- Disconnects automatically when the test finishes.
- Returns one client per test. Calling it again in the same test returns the same client. A retried test gets a new one.
- Reads
TD_OS. If you don’t passos, it uses theTD_OSenvironment variable, then Linux.
Returns
A connectedTestDriver client.
new TestDriver(apiKey, options)
Creates a client that you connect and disconnect yourself.Parameters
string
Your TestDriver API key. Defaults to
TD_API_KEY, which TestDriver loads from .env. Create one at console.testdriver.ai/team/api-keys.Example
Share one sandbox across a test file by connecting inbeforeAll:
tests/dashboard.test.mjs
connect(options)
Connects the client to a sandbox. By default, it creates a new sandbox.connect() authenticates first if needed. You don’t need to call auth() yourself.
Parameters
ConnectOptions
Returns
object
Errors
ThrowsAlready connected if the client is already connected. Create a new client to connect again.
Example
disconnect()
Disconnects from the sandbox. The sandbox keeps running for thekeepAlive time, then shuts down.
auth()
Authenticates with the TestDriver API and resolves to a session token.connect() calls it when needed.
getInstance()
Returns the connected sandbox’s details, ornull if the client isn’t connected.
getSessionId()
Returns the current session ID, ornull. Include it when you report a problem.
getLastSandboxId()
Returns the most recent sandbox, ornull if there isn’t one. It reads from the connected client, or from .testdriver/last-sandbox.
setLogging(enabled)
Turns console logging on or off while the test runs.getEmitter()
Returns the client’s event emitter, so you can react to what TestDriver does.Related
- Options: every option you can pass to the client
- Launch apps: start Chrome, extensions, and desktop apps once connected
- Run tests: reuse a sandbox between runs
- Events: listen for commands, logs, and errors

