Testing

Testing LaserEyes Apps

Learn how to effectively test your LaserEyes applications with comprehensive testing strategies and best practices.

Testing Challenges

Wallet Dependencies

Tests need to interact with wallet extensions and handle complex wallet state management.

Blockchain Interactions

Tests may need to interact with the blockchain and handle network variability.

Network Variability

Blockchain networks can have variable performance and response times.

Test Environment

Setting up a proper test environment requires careful configuration and mocking.

Testing Approaches

Unit Testing

Component Testing

Test individual functions and components in isolation

Integration

Integration Testing

Test interactions between components

E2E

End-to-End Testing

Test complete user flows on testnet

Unit Testing

Example Unit Test

// Example unit test for a utility function
import { formatBitcoinAmount } from '../utils/formatters'

describe('formatBitcoinAmount', () => {
  test('formats satoshis as BTC with 8 decimal places', () => {
    expect(formatBitcoinAmount('1000000')).toBe('0.01000000')
    expect(formatBitcoinAmount('123456789')).toBe('1.23456789')
    expect(formatBitcoinAmount('0')).toBe('0.00000000')
  })
  
  test('handles string and number inputs', () => {
    expect(formatBitcoinAmount(1000000)).toBe('0.01000000')
    expect(formatBitcoinAmount('1000000')).toBe('0.01000000')
  })
  
  test('handles invalid inputs', () => {
    expect(formatBitcoinAmount('')).toBe('0.00000000')
    expect(formatBitcoinAmount('invalid')).toBe('0.00000000')
    expect(formatBitcoinAmount(null)).toBe('0.00000000')
    expect(formatBitcoinAmount(undefined)).toBe('0.00000000')
  })
})

Mocking LaserEyes

Mock Setup

// Create a mock for useLaserEyes hook
jest.mock('@omnisat/lasereyes-react', () => ({
  useLaserEyes: () => ({
    connect: jest.fn(),
    disconnect: jest.fn(),
    connected: false,
    address: '',
    balance: '0',
    sendBTC: jest.fn(),
    getInscriptions: jest.fn(),
    getMetaBalances: jest.fn(),
  }),
  LaserEyesProvider: ({ children }) => children,
}))

Component Test

describe('WalletConnect', () => {
  test('renders connect button when not connected', () => {
    const mockUseLaserEyes = useLaserEyes as jest.Mock
    mockUseLaserEyes.mockReturnValue({
      connect: jest.fn(),
      disconnect: jest.fn(),
      connected: false,
      address: '',
      balance: '0',
    })
    
    render(<WalletConnect />)
    
    const connectButton = screen.getByText('Connect Wallet')
    expect(connectButton).toBeInTheDocument()
  })
})

Testing with Testnet

Testnet Testing Considerations

  • You'll need a wallet that supports Testnet
  • You'll need Testnet coins (available from faucets)
  • Testnet can be unstable at times
  • These tests are more like integration tests than unit tests
// Configure LaserEyes to use Testnet for testing
import { LaserEyesProvider } from '@omnisat/lasereyes-react'
import { TESTNET } from '@omnisat/lasereyes-core'

const renderWithTestnet = (ui) => {
  return render(
    <LaserEyesProvider
      config={{ 
        network: TESTNET,
        dataSources: {
          maestro: {
            apiKey: process.env.TEST_MAESTRO_API_KEY,
          },
          mempool: {
            url: 'https://mempool.space/testnet/api',
          }
        }
      }}
    >
      {ui}
    </LaserEyesProvider>
  )
}

Testing Tools

Jest

Popular testing framework for running unit and integration tests with excellent mocking capabilities.

React Testing Library

Testing utilities that encourage good testing practices by working with actual DOM nodes.

Mock Service Worker

API mocking library that lets you capture and mock network requests.

Cypress

End-to-end testing framework that makes it easy to set up, write, and debug tests.