Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 17 additions & 110 deletions bun.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions bunfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[test]
# Load these modules before running tests
preload = ["./src/test-setup.ts"]
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@
"twitter-api-v2": "^1.23.2"
},
"devDependencies": {
"@vitest/coverage-v8": "3.2.4",
"@types/bun": "^1.2.18",
"prettier": "3.6.1",
"tsup": "8.5.0",
"typescript": "^5.8.3",
"vitest": "3.2.4"
"typescript": "^5.8.3"
},
"scripts": {
"build": "tsup",
"dev": "tsup --watch",
"test": "tsup && elizaos test",
"test:coverage": "vitest run --coverage",
"test": "bun test",
"test:unit": "bun test src/__tests__/**/*.test.ts",
"test:e2e": "bun test src/__tests__/e2e/*.test.ts",
"test:all": "bun test src/__tests__",
"test:coverage": "bun test --coverage",
"lint": "prettier --write ./src",
"clean": "rm -rf dist .turbo node_modules .turbo-tsconfig.json tsconfig.tsbuildinfo",
"format": "prettier --write ./src",
Expand Down
47 changes: 47 additions & 0 deletions src/__mocks__/@elizaos/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Mock of @elizaos/core for tests
import { mock } from 'bun:test';

// Define types and enums that external code depends on
export type UUID = `${string}-${string}-${string}-${string}-${string}`;

export enum ChannelType {
TEXT = "text",
VOICE = "voice",
VIDEO = "video",
GROUP = "group",
FEED = "feed"
}

// Create mock functions
const createUniqueUuid = mock<(runtime: unknown, id: string) => UUID>(
(_runtime, id) => `uuid-${id}` as UUID,
);
const mockError = mock();
const mockDebug = mock();
const mockInfo = mock();
const mockWarn = mock();

// Export logger mock
export const logger = {
error: mockError,
debug: mockDebug,
info: mockInfo,
warn: mockWarn,
};

// Export event types constants
export const EventType = {
WORLD_JOINED: 'WORLD_JOINED',
MESSAGE_CREATED: 'MESSAGE_CREATED',
MESSAGE_RECEIVED: 'MESSAGE_RECEIVED',
CHANNEL_CREATED: 'CHANNEL_CREATED',
ENTITY_CREATED: 'ENTITY_CREATED',
ENTITY_UPDATED: 'ENTITY_UPDATED',
ENTITY_DELETED: 'ENTITY_DELETED',
AGENT_CREATED: 'AGENT_CREATED',
AGENT_UPDATED: 'AGENT_UPDATED',
AGENT_DELETED: 'AGENT_DELETED',
};

// Export main function
export { createUniqueUuid };
65 changes: 56 additions & 9 deletions src/__tests__/TESTING_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ Unit tests mock external dependencies and test individual components:

```bash
# Run all unit tests
npm test
bun test

# Run specific test file
npm test MessageService.test.ts
bun test src/services/__tests__/MessageService.test.ts

# Run with coverage
npm test -- --coverage
# Run specific pattern
bun test --pattern "MessageService"

# Run in watch mode
npm test -- --watch
bun test --watch
```

### E2E Tests
Expand All @@ -59,10 +59,10 @@ End-to-end tests require real Twitter API credentials:

```bash
# Run E2E tests (requires .env.test file)
npm test -- --run e2e
bun test src/__tests__/e2e

# Skip E2E tests if no credentials
npm test -- --run --exclude="**/e2e/**"
# Skip E2E tests (run only unit tests)
bun test --pattern "^(?!.*e2e).*$"
```

## Test Coverage
Expand Down Expand Up @@ -156,7 +156,7 @@ console.log("Profile:", profile);
### 2. Enable Debug Logging

```bash
DEBUG=elizaos:* npm test
DEBUG=elizaos:* bun test
```

### 3. Common Issues
Expand Down Expand Up @@ -198,6 +198,53 @@ With Twitter API v2 only:
- ❌ Media upload (requires additional implementation)
- ❌ Unlike/unretweet (requires additional implementation)
- ❌ Fetch retweeters list

## Bun Test Setup

This project uses Bun as the test runner instead of Vitest. Here's how the test environment is configured:

### Test Runner Configuration

- Tests use `bun:test` imports for test utilities (`test`, `describe`, `expect`, etc.)
- The test setup is configured in `bunfig.toml` and `src/test-setup.ts`

### Mocking

- Bun's native mocking system is used with `mock()` and `mock.module()` instead of Vitest's `vi.mock()`
- External dependencies are mocked via module mocks in `test-setup.ts`:

```typescript
// Example from test-setup.ts
import { mock } from 'bun:test';

mock.module('@elizaos/core', () => {
return {
createUniqueUuid: (runtime: any, id: string) => `uuid-${id}`,
logger: { /* mock methods */ },
// Other mocked exports...
};
});
```

- Additional manual mocks are located in `src/__mocks__/`

### TypeScript Compatibility

- UUID string template literals use a utility function for type compatibility:

```typescript
// Type for UUID template strings
type UuidString = `${string}-${string}-${string}-${string}-${string}`;

// Helper function to cast any string to UUID format for type compatibility
const asUuid = (id: string): UuidString => id as UuidString;
```

### Test Patterns

- Unit tests are in `__tests__` directories next to the code they test
- E2E tests are in `__tests__/e2e/` and require valid Twitter API credentials
- The tests use the `.env.test` file for test configuration
- ❌ Trends API
- ❌ Direct message conversations (requires additional permissions)

Expand Down
Loading