Testing
This guide covers writing and running tests for your node implementation.
Unit Tests
Create comprehensive tests for your node:
// ts/src/nodes/your-node/tests/your-node.test.ts
import { describe, it, expect, beforeEach } from 'vitest';
import { YourNode, YourNodeMode } from '../common.js';
describe('YourNode', () => {
let node: YourNode;
beforeEach(() => {
node = new YourNode(YourNodeMode.NATIVE);
});
it('should initialize with correct ID', () => {
expect(node.getId()).toBe('your_node_native');
});
it('should sync state successfully', async () => {
const result = await node.syncState();
expect(result).not.toBeInstanceOf(Error);
});
it('should validate swap support', () => {
const inputToken = { chain_id: '1', address: '0x...', decimals: 18 };
const outputToken = { chain_id: '137', address: '0x...', decimals: 6 };
const isSupported = node.isSwapEnabled(inputToken, outputToken);
expect(typeof isSupported).toBe('boolean');
});
it('should process swap request', async () => {
const request = {
input_token: { chain_id: '1', address: '0x...', decimals: 18 },
output_token: { chain_id: '137', address: '0x...', decimals: 6 },
amount_in: '1000000000000000000',
amount_out_min: '0',
slippage_tolerance: 100,
recipient_address: '0x...',
sender_address: '0x...',
exact_out: false,
generate_deposit_address: false,
timeout_ms: 300000
};
const result = await node.processRequest(request);
expect(result).not.toBeInstanceOf(Error);
});
});
Integration Tests
Test your node with real API calls:
// ts/src/nodes/your-node/tests/integration.test.ts
import { describe, it, expect } from 'vitest';
import { YourNode, YourNodeMode } from '../common.js';
describe('YourNode Integration', () => {
it('should work with real API', async () => {
const node = new YourNode(YourNodeMode.NATIVE);
// Test sync
const syncResult = await node.syncState();
expect(syncResult).not.toBeInstanceOf(Error);
// Test with real tokens
const tokens = node.getSupportedTokens();
expect(tokens.length).toBeGreaterThan(0);
// Test swap validation
if (tokens.length >= 2) {
const isSupported = node.isSwapEnabled(tokens[0], tokens[1]);
expect(typeof isSupported).toBe('boolean');
}
});
});
Running Tests
# Run all tests
npm test
# Run tests for a specific node
npm test -- your-node
# Run tests in watch mode
npm test -- --watch
# Run tests with coverage
npm test -- --coverage
Test Examples from Existing Nodes
NEAR Tests
Location:
ts/src/nodes/near/test-end-to-end-comprehensive.test.tsts/src/nodes/near/test-same-chain-swaps.test.ts
Covers:
- End-to-end swap flows
- Same-chain swaps
- Cross-chain swaps
- Address validation
- Asset ID resolution
Meson Tests
Location: ts/src/nodes/meson/test-end-to-end-comprehensive.test.ts
Covers:
- Price quotes
- Transaction data generation
- Limits validation
- Error handling
GasZip Tests
Location: ts/src/nodes/gaszip/test_all_chains.test.ts
Covers:
- All supported chains
- Native token bridging
- Capacity validation
- Chain ID mapping
Mayan Tests
Location: ts/src/nodes/mayan/flows/*
Covers:
- End-to-end examples
- Transaction status tracking
- Error scenarios
Testing Best Practices
- Test Individual Components: Test each method separately
- Mock External APIs: Use mocks for external API calls in unit tests
- Test Error Cases: Test both success and failure scenarios
- Test Edge Cases: Test boundary conditions and edge cases
- Integration Tests: Use real APIs for integration tests (with caution)
Next Steps
- Error Handling - Understand error codes
- Best Practices - Follow development guidelines