Overview
The CoW Protocol Contracts project uses Hardhat and Waffle for testing smart contracts. The test suite includes unit tests, integration tests, and provides tooling for debugging and coverage analysis.Running Tests
The basic test command runs the entire test suite:Test Commands
Standard Tests
Runs the complete test suite:Tests Ignored in Coverage
Some tests are excluded from coverage analysis due to their nature (e.g., gas benchmarks or edge cases). Run these with:MOCHA_CONF='ignored in coverage' to run the excluded test suite.
Debug Mode
For detailed test execution logs and debugging information, enable debug mode:debug package, showing:
- Contract deployments
- Transaction details
- State changes
- Event emissions
Debug output can be very verbose. Consider filtering by specific namespaces if needed.
Gas Reporting
Track gas consumption for contract operations by enabling the gas reporter:Gas Reporter Features
WhenREPORT_GAS=1 is set, the test output includes:
- Gas costs per function call
- Deployment costs for contracts
- Average, min, and max gas consumption
- Cost estimates in ETH and USD (if configured)
Configuring Gas Reporter
The gas reporter is configured through Hardhat using thehardhat-gas-reporter plugin. Common configuration options include:
- Currency conversion (ETH, USD)
- Output formatting (terminal, file)
- Excluded contracts or methods
Code Coverage
Generate a comprehensive test coverage report:- Instruments the Solidity contracts
- Runs the test suite with coverage tracking
- Generates a coverage report
Understanding Coverage Output
The coverage command produces: Console Summary: Printed to terminal with:- Statement coverage percentage
- Branch coverage percentage
- Function coverage percentage
- Line coverage percentage
- File-by-file coverage breakdown
- Uncovered lines highlighted in red
- Partially covered branches in yellow
- Fully covered code in green
Test Configuration
The project uses Mocha for test execution with configuration controlled by theMOCHA_CONF environment variable:
- Default: Standard test suite
ignored in coverage: Runs tests excluded from coverage analysiscoverage: Enables coverage-specific test configuration
Writing Tests
Tests are written using:- Hardhat: Ethereum development environment
- Waffle: Ethereum testing library
- Chai: Assertion library with Waffle matchers
- Ethers.js: Ethereum library for contract interaction
Common Test Patterns
Testing Best Practices
- Isolate tests: Each test should be independent
- Use fixtures: Leverage Waffle fixtures for common setup
- Test edge cases: Include boundary conditions and error cases
- Gas awareness: Monitor gas costs for critical operations
- Coverage targets: Aim for high coverage but focus on meaningful tests
Continuous Integration
The project includes a CI profile infoundry.toml for consistent test execution:
Troubleshooting
Tests Failing
-
Rebuild contracts:
-
Clear Hardhat cache:
- Check node versions: Ensure compatibility with project requirements
Coverage Issues
-
Out of memory: Increase Node.js heap size:
- Timeout errors: Some tests may need longer timeouts during coverage instrumentation
Next Steps
- Benchmark gas performance of your contracts
- Review deployment procedures for production
- Explore the contract architecture documentation
