Skip to main content
Every test talks to its sandbox through a TestDriver client. There are two ways to create one:
  • TestDriver(context) from testdriverai/vitest/hooks. Use this in Vitest tests. It connects and disconnects for you.
  • new TestDriver() from testdriverai. 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 pass os, it uses the TD_OS environment variable, then Linux.

Returns

A connected TestDriver 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.
TestDriverOptions
Client options. See Options.

Example

Share one sandbox across a test file by connecting in beforeAll:
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

Throws Already 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 the keepAlive time, then shuts down.
The Vitest hook calls this for you.

auth()

Authenticates with the TestDriver API and resolves to a session token.
You rarely need it, because connect() calls it when needed.

getInstance()

Returns the connected sandbox’s details, or null if the client isn’t connected.

getSessionId()

Returns the current session ID, or null. Include it when you report a problem.

getLastSandboxId()

Returns the most recent sandbox, or null 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.
See Events for every event name.
  • 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