> ## Documentation Index
> Fetch the complete documentation index at: https://docs.functionlab.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Testing

# Testing Your Functions

Learn how to test and debug Function Lab configurations.

## Preview Mode

Function Lab provides a built-in preview mode in the Shopify admin.

### Accessing Preview

1. Navigate to Apps → Function Lab
2. Select your function
3. Click "Preview" or "Test"
4. Enter test cart data

### Test Scenarios

Create test scenarios with different:

* Customer tags
* Cart contents
* Product variants
* Order totals
* Geographic locations

## Common Testing Patterns

### Test Customer Segments

```json  theme={null}
// Test VIP customer
{
  "customer": {
    "tags": ["VIP"]
  }
}

// Test first-time customer
{
  "customer": {
    "tags": [],
    "ordersCount": 0
  }
}
```

### Test Cart Conditions

```json  theme={null}
// Test minimum order value
{
  "cart": {
    "totalAmount": 150.00
  }
}

// Test quantity thresholds
{
  "cart": {
    "lines": [
      {
        "quantity": 5,
        "merchandise": {...}
      }
    ]
  }
}
```

## Debugging Strategies

### Enable Console Logging

Add console output to track execution:

```json  theme={null}
{
  "facts": {
    "debug_total": "cart.totalAmount"
  },
  "conditions": {
    "$debug_total": { "gte": 100 }
  }
}
```

### Test Conditions in Isolation

Simplify rules to test one condition at a time:

```json  theme={null}
// Test just the customer condition
{
  "conditions": {
    "customer.tags": { "includes": "VIP" }
  }
}
```

### Verify Selector Output

Test selectors separately:

```json  theme={null}
{
  "facts": {
    "selected_items": "cart.lines[?merchandise.product.handle=='t-shirt'].id"
  }
}
```

## Validation Checklist

Before activating a function:

* [ ] JSON syntax is valid
* [ ] All required fields are present
* [ ] Predicates are spelled correctly
* [ ] Paths reference valid fields
* [ ] GIDs are properly formatted
* [ ] Discount values are in correct format
* [ ] Messages are customer-friendly
* [ ] Tested with multiple cart scenarios
* [ ] Verified in Shopify preview mode

## Common Issues

### "No discount applied"

**Possible causes:**

1. Conditions not met
2. Wrong path in condition
3. Case-sensitive string comparison
4. Empty selector result

**Debug steps:**

* Check input data matches condition
* Verify field paths exist
* Test with simplified conditions
* Add facts to inspect intermediate values

### "Wrong discount amount"

**Possible causes:**

1. `discount_type` mismatch (percentage vs fixed)
2. Value format incorrect
3. `appliesToEachItem` confusion

**Debug steps:**

* Verify `discount_type` is correct
* Check value is numeric
* Test with round numbers first

### "Discount on wrong products"

**Possible causes:**

1. Selector returns unexpected results
2. Handle doesn't match
3. GID format issue

**Debug steps:**

* Test selector in facts
* Verify product handles
* Check variant IDs

## Testing Best Practices

### Start Simple

Begin with minimal configuration:

```json  theme={null}
{
  "rules": [
    {
      "conditions": {
        "customer.tags": { "includes": "TEST" }
      },
      "events": [
        {
          "discount": "product_discount",
          "params": {
            "discount_type": "percentage",
            "value": 10,
            "message": "Test Discount"
          }
        }
      ]
    }
  ]
}
```

### Add Complexity Gradually

Once basic rule works, add:

1. Additional conditions
2. Facts
3. Complex selectors
4. Multiple events

### Test Edge Cases

* Empty cart
* Single item
* Large quantity
* Zero-price items
* Customer without account

### Document Test Cases

Keep a record of test scenarios:

```markdown  theme={null}
## Test Cases

1. VIP customer with $100+ cart → 15% discount
2. First-time customer → 10% discount
3. Bulk order (10+ items) → $50 off
4. Geographic restriction (US only) → No discount for CA
```

## Performance Testing

### Monitor Execution Time

* Keep rules simple when possible
* Limit fact complexity
* Use direct targeting over selectors
* Test with realistic cart sizes

### Stress Testing

Test with:

* Large carts (20+ line items)
* Complex product structures
* Multiple rules
* Heavy fact computations

## Next Steps

* [Common Patterns](common-patterns.md) - Real-world tested examples
* [Troubleshooting](../reference/troubleshooting/README.md) - Detailed error solutions
* [Performance](../advanced/performance.md) - Optimization guide


Built with [Mintlify](https://mintlify.com).