betting-brain-v3 / rules
Cursor rule8 starsChanged 14 months agoMITView on GitHub
Test setup and configuration patterns
- Reads credentials
Where files are headingNew Cursor rule files have more rules about planning and tracking tasks, and fewer about code style.
Of the Cursor rule files we read, the 1,533 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repository27 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- .cursor/rules/99-floor.mdcCursor rule
- .cursor/rules/ai-friendly-testing.mdcCursor rule
- .cursor/rules/analytics-stub-api.mdcCursor rule
- .cursor/rules/analytics-testing.mdcCursor rule
- .cursor/rules/api-patterns.mdcCursor rule
- .cursor/rules/browser-extension.mdcCursor rule
- .cursor/rules/bun-runtime.mdcCursor rule
- .cursor/rules/cache-optimization.mdcCursor rule
- .cursor/rules/ci-integration.mdcCursor rule
- .cursor/rules/ci-patterns.mdcCursor rule
- .cursor/rules/cloudflare-infrastructure.mdcCursor rule
- .cursor/rules/cloudflare-workers.mdcCursor rule
- .cursor/rules/code-searchability.mdcCursor rule
- .cursor/rules/coverage-thresholds.mdcCursor rule
- .cursor/rules/database-patterns.mdcCursor rule
- .cursor/rules/documentation.mdcCursor rule
- .cursor/rules/endpoint-routing.mdcCursor rule
- .cursor/rules/file-naming.mdcCursor rule
- .cursor/rules/mcp-integration.mdcCursor rule
- .cursor/rules/process-management.mdcCursor rule
- .cursor/rules/production-security.mdcCursor rule
- .cursor/rules/quality-standards.mdcCursor rule
- .cursor/rules/root-organization.mdcCursor rule
- .cursor/rules/security-patterns.mdcCursor rule
- .cursor/rules/testing.mdcCursor rule
- .cursor/rules/testing-patterns.mdcCursor rule
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
- Distinctive: line 154
Use global analytics stub consistently
- Distinctive: line 157
Use fresh stub per test
- Distinctive: line 161
Create duplicate analytics stubs
- Distinctive: line 163
Leave console mocks active
≈ “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.
Files
---
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
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
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.

