agentleFS
brendadeeznuts1111brendadeeznuts1111/betting-brain-v3/.cursor/rules/test-setup-patterns.mdc

betting-brain-v3 / rules

Cursor rule8 starsChanged 14 months agoMITView on GitHub

Test setup and configuration patterns

  • Reads credentials

The file

We split this test-setup-patterns.mdc into the rules it gives an agent

4 rules. About 0 are common, 4 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈4

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use global analytics stub consistently

    line 154
  2. Distinctive:

    Use fresh stub per test

    line 157
  3. Distinctive:

    Create duplicate analytics stubs

    line 161
  4. Distinctive:

    Leave console mocks active

    line 163

≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
test-setup-patterns.mdc
---
version: "1.1.0"
globs: "tests/**/*.ts,tests/**/*.js"
description: "Test setup and configuration patterns"
lastUpdated: "2025-10-08"
dependencies: ["quality-standards", "testing-patterns"]
---

# Test Setup Patterns

## Global Test Configuration

### Test Setup File: `tests/setup/test-setup.ts`

```typescript

import { vi, beforeAll, beforeEach, afterEach, expect } from 'bun:test';
import { createAnalyticsEngineStub } from '../utils/analytics-engine-stub';

// Global analytics stub instance
let globalAnalyticsStub: ReturnType<typeof createAnalyticsEngineStub> | null = null;
let currentTestName: string = '';

export function getGlobalAnalyticsStub() {
  return globalAnalyticsStub;
}

export function setCurrentTestName(name: string) {
  currentTestName = name;
}
```

## Auto-Flush Pattern

### Analytics Tests Only
```typescript

afterEach(function () {
  if (currentTestName.toLowerCase().includes('analytics') && globalAnalyticsStub) {
    globalAnalyticsStub.flush();
  }
  vi.restoreAllMocks();
});
```

### Test Name Detection
```typescript

// In analytics tests, always set test name
test('should track analytics calls with stub', async () => {
  setCurrentTestName('should track analytics calls with stub');
  // Test implementation
});
```

## Mock Environment Pattern

### Create Mock Environment
```typescript

import { createMockEnv } from '../utils/test-helpers';

describe('My Analytics Test', () => {
  let mockEnv: any;
  let analytics: any;

  beforeEach(() => {
    const mock = createMockEnv();
    mockEnv = mock.env;
    analytics = mock.analytics;
  });
});
```

### Use Global Stub
```typescript
// ✅ CORRECT: Use global stub
const analytics = getGlobalAnalyticsStub();

// ❌ WRONG: Create duplicate stubs
const analytics1 = createAnalyticsEngineStub();
const analytics2 = createAnalyticsEngineStub();
```

## Test Isolation Patterns

### Fresh Stub Per Test
```typescript
beforeEach(() => {
  // Create fresh analytics stub for each test
  globalAnalyticsStub = createAnalyticsEngineStub();
});
```

### Console Mocking
```typescript
beforeEach(() => {
  vi.spyOn(console, 'log').mockImplementation(() => {});
  vi.spyOn(console, 'error').mockImplementation(() => {});
  vi.spyOn(console, 'warn').mockImplementation(() => {});
});
```

### Cleanup After Tests
```typescript
afterEach(() => {
  // Auto-flush analytics on analytics tests
  if (currentTestName.toLowerCase().includes('analytics') && globalAnalyticsStub) {
    globalAnalyticsStub.flush();
  }
  
  // Restore all mocks
  vi.restoreAllMocks();
});
```

## Test File Organization

### Analytics Test Files
```
tests/unit/analytics-*.test.ts
tests/unit/analytics-testing-example.test.ts
tests/unit/analytics-tracking.test.ts
```

### Test Utilities
```
tests/utils/analytics-engine-stub.ts
tests/utils/test-helpers.ts
tests/setup/test-setup.ts
```

## Configuration Files

### Main Test Config
```toml
# bunfig.toml
[test]
coverage = false  # coverage off by default
coverageThreshold = 0.8   # 80% for production code
```

### Analytics Test Config
```toml
# bunfig.analytics.toml
[test]
coverage = true
coverageThreshold = 0  # No threshold for analytics tests
```

## Best Practices

### ✅ Do These
- Use global analytics stub consistently
- Set test name for auto-flush detection
- Mock console methods to reduce noise
- Use fresh stub per test
- Clean up mocks after each test

### ❌ Don't Do These
- Create duplicate analytics stubs
- Forget to set test name for auto-flush
- Leave console mocks active
- Share state between tests
- Skip cleanup in afterEach

## Error Handling

### Unhandled Promise Rejections
```typescript
process.on('unhandledRejection', (reason, promise) => {
  console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});
```

### Test Environment Setup
```typescript
beforeAll(() => {
  process.env.NODE_ENV = 'test';
});
```

## Related Rules

- [Analytics Testing](mdc:.cursor/rules/analytics-testing.mdc) - Core analytics testing patterns
- [Coverage Thresholds](mdc:.cursor/rules/coverage-thresholds.mdc) - Dual threshold system
- [Analytics Stub API](mdc:.cursor/rules/analytics-stub-api.mdc) - AnalyticsEngineStub API reference

## Related Files

- [test-setup.ts](mdc:tests/setup/test-setup.ts) - Global test configuration
- [analytics-engine-stub.ts](mdc:tests/utils/analytics-engine-stub.ts) - Analytics stub
- [test-helpers.ts](mdc:tests/utils/test-helpers.ts) - Helper functions
- [bunfig.toml](mdc:bunfig.toml) - Main test configuration
- [bunfig.analytics.toml](mdc:bunfig.analytics.toml) - Analytics configuration

---

**Status:** Production Ready ✅  
**Last Updated:** 2025-10-07  
**Enforcement:** Required for all test files

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.