Skip to content
Merged
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
105 changes: 105 additions & 0 deletions .cursor/rules/asyncapi-testing.mdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
description: AsyncAPI test writing standards and patterns
globs: test/**/*.test.ts
alwaysApply: false
---

# AsyncAPI Test Writing Standards

## 1. Validate Test Specifications

**All test specs MUST be valid AsyncAPI specifications** unless intentionally testing error handling.

### Pattern: Valid Specifications

```typescript
// ✅ GOOD: Define spec as constant, then validate
const before = {
asyncapi: '3.0.0',
info: {
title: 'Test API',
version: '1.0.0',
},
channels: {
testChannel: {}
}
}
...

await parseAsyncApiAndAssertValid(before)
await parseAsyncApiAndAssertValid(after)

const { merged, diffs } = apiDiff(before, after, COMPARE_OPTIONS)
// ... assertions
```

### Pattern: Intentionally Invalid Specifications

When testing error handling or invalid specs, **clearly document with a comment**:

```typescript
// ✅ GOOD: Commented validation with explanation
const source = {
asyncapi: '3.0.0',
// Missing required 'info' field
}

// await parseAsyncApiAndAssertValid(source) // intentionally not valid source

const result = unify(source, { unify: true })
// ... test error handling
```

## 2. Use Helper Functions for Complex Specs

Create well-documented helper functions to reduce boilerplate:

```typescript
/**
* Helper function to create AsyncAPI document with schema in components
* @param schemaDefinition - The schema definition
* @param schemaFormat - Optional schema format:
* - undefined: creates regular schema (default)
* - string value: creates Multi Format Schema with schemaFormat property
*/
const createAsyncAPIWithSchema = (
schemaDefinition: unknown,
schemaFormat?: string
) => {
return {
asyncapi: '3.0.0',
info: {
title: 'Test API',
version: '1.0.0',
},
components: {
schemas: {
TestSchema: schemaFormat
? { schemaFormat, schema: schemaDefinition }
: schemaDefinition,
},
},
}
}
```

## 3. Required Fields for Valid Specs

All valid AsyncAPI 3.0.0 specs must include:

```typescript
{
asyncapi: '3.0.0', // Required
info: { // Required
title: 'Test API', // Required
version: '1.0.0', // Required
},
// ... other properties
}
```

## 4. Test Organization

- Define `COMPARE_OPTIONS` constants at the top of describe blocks
- Keep spec definitions close to where they're used
- Use descriptive variable names (`before`, `after`, or domain-specific names)
Loading
Loading