Skip to main content

Overview

Staxiq smart contracts are tested using Vitest with the Clarinet SDK, providing a fast, type-safe testing environment for Clarity contracts.

Unit Tests

Test individual contract functions in isolation

Integration Tests

Test complete user workflows and interactions

Cost Analysis

Measure gas costs for contract operations

Coverage Reports

Track which contract paths are tested

Test Setup

Dependencies

The test environment uses:
package.json

Vitest Configuration

vitest.config.ts
The clarinet environment provides a simulated Stacks blockchain for testing.

Running Tests

Basic Test Run

Run all tests once:

Detailed Test Report

Run with coverage and cost analysis:
Output includes:
  • Test results
  • Contract execution costs
  • Code coverage percentage
  • Gas usage per function

Watch Mode

Auto-run tests when files change:
Use watch mode during development for instant feedback on code changes.

Writing Tests

Test Structure

The test file structure:
tests/staxiq-user-profile.test.ts

Example Tests

Here are comprehensive tests for the user profile contract:

Test Coverage

Generate a coverage report:
Output shows:
Aim for >90% coverage to ensure contract reliability.

Gas Cost Analysis

Vitest can report execution costs:
Example output:
Lower costs mean cheaper transactions for users. Optimize expensive functions if possible.

Best Practices

1. Test All Paths

Test both success and failure cases:

2. Use Descriptive Test Names

3. Setup and Teardown

Use beforeEach for common setup:

4. Test Edge Cases

  • Boundary values (0, max uint)
  • Empty strings
  • Multiple users simultaneously
  • Sequential operations

Continuous Integration

Add tests to your CI/CD pipeline:
.github/workflows/test.yml

Debugging Tests

Log Contract State

Inspect Transaction Events


Next Steps

Deployment

Deploy tested contracts to testnet or mainnet

Integration

Integrate contracts into your frontend

Vitest Docs

Learn more about Vitest testing framework

Clarinet SDK

Explore Clarinet SDK documentation