Skip to main content

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

// Test VIP customer
{
  "customer": {
    "tags": ["VIP"]
  }
}

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

Test Cart Conditions

// 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:
{
  "facts": {
    "debug_total": "cart.totalAmount"
  },
  "conditions": {
    "$debug_total": { "gte": 100 }
  }
}

Test Conditions in Isolation

Simplify rules to test one condition at a time:
// Test just the customer condition
{
  "conditions": {
    "customer.tags": { "includes": "VIP" }
  }
}

Verify Selector Output

Test selectors separately:
{
  "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:
{
  "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:
## 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

I