Overview

The runTests method executes tests in the sandbox environment and automatically detects the appropriate test runner (e.g., npm test, pytest, cargo test, etc.). This method supports both streaming and non-streaming execution modes.

Method Signature

async runTests({
  branch,
  history,
  callbacks,
}: {
  branch?: string;
  history?: Conversation[];
  callbacks?: VibeKitStreamCallbacks;
}): Promise<AgentResponse>

Parameters

branch
string

The Git branch to run tests on. If not specified, tests will run on the current branch.

history
Conversation[]

Optional conversation history to provide context for the test execution. This can help the agent understand previous interactions and make more informed decisions about test execution.

callbacks
VibeKitStreamCallbacks

Optional callbacks for streaming updates during test execution.

Return Value

Returns a Promise<AgentResponse> containing the test execution results:

sandboxId
string

The ID of the sandbox where tests were executed

stdout
string

Standard output from the test execution

stderr
string

Standard error output from the test execution

exitCode
number

Exit code from the test execution (0 indicates success)

Examples

Basic Test Execution

import { VibeKit } from 'vibekit';

const vibekit = new VibeKit({
  agent: {
    type: 'codex',
    model: {
      provider: 'openai',
      name: 'gpt-4',
      apiKey: process.env.OPENAI_API_KEY
    }
  },
  environment: 'e2b',
  sessionId: 'test-session'
});

// Run tests on current branch
const result = await vibekit.runTests({});

console.log('Tests completed with exit code:', result.exitCode);
console.log('Test output:', result.stdout);

Run Tests on Specific Branch

// Run tests on a specific branch
const result = await vibekit.runTests({
  branch: 'feature/new-functionality'
});

if (result.exitCode === 0) {
  console.log('All tests passed!');
} else {
  console.log('Some tests failed:', result.stderr);
}

Streaming Test Execution

// Run tests with streaming updates
const result = await vibekit.runTests({
  callbacks: {
    onUpdate: (message) => {
      console.log('Test update:', message);
    },
    onError: (error) => {
      console.error('Test error:', error);
    }
  }
});

Run Tests with Context

// Run tests with conversation history for context
const history = [
  {
    role: 'user',
    content: 'I just added a new authentication feature'
  },
  {
    role: 'assistant', 
    content: 'I understand. Let me run the tests to ensure everything works correctly.'
  }
];

const result = await vibekit.runTests({
  branch: 'feature/auth',
  history: history,
  callbacks: {
    onUpdate: (message) => {
      console.log(message);
    }
  }
});

Test Runner Detection

The runTests method automatically detects and uses the appropriate test runner based on the project structure:

  • Node.js: Runs npm test or yarn test
  • Python: Runs pytest, python -m unittest, or python -m pytest
  • Rust: Runs cargo test
  • Go: Runs go test
  • And more: Supports various other testing frameworks

Error Handling

try {
  const result = await vibekit.runTests({
    branch: 'main'
  });
  
  if (result.exitCode !== 0) {
    console.log('Tests failed with errors:', result.stderr);
  }
} catch (error) {
  console.error('Failed to run tests:', error.message);
}

Notes

  • The method requires an active sandbox environment
  • Test execution is performed within the sandbox, ensuring a clean and isolated environment
  • The agent will attempt to install dependencies if they’re missing
  • Streaming callbacks provide real-time feedback during test execution
  • The method works with both Codex and Claude agents