Back to list
FractionEstate

testing

by FractionEstate

0🍴 0📅 Jan 5, 2026

SKILL.md


name: testing description: >- Test Midnight smart contracts using simulators and unit tests. Use when setting up test environments, writing contract tests, or debugging circuit behavior. Triggers on testing, simulator, unit test, or test framework questions.

Testing Midnight Contracts

Test Compact smart contracts using simulators and test frameworks.

Quick Start

import { ContractSimulator } from '@midnight-ntwrk/compact-simulator';

// Create simulator
const simulator = new ContractSimulator(compiledContract);

// Call circuit
const result = await simulator.call('increment', {});

// Check state
expect(simulator.ledger.counter).toBe(1n);

Reference Files

Test Environment

┌──────────────────────────────────────────────┐
│              Test Environment                │
├──────────────────────────────────────────────┤
│  ┌────────────────┐  ┌────────────────┐     │
│  │   Contract     │  │    Contract    │     │
│  │   Simulator    │  │    Artifacts   │     │
│  └────────────────┘  └────────────────┘     │
│           │                  │               │
│           └────────┬─────────┘               │
│                    ▼                         │
│           ┌────────────────┐                 │
│           │   Test Suite   │                 │
│           │   (Jest/Vitest)│                 │
│           └────────────────┘                 │
└──────────────────────────────────────────────┘

Installation

npm install -D @midnight-ntwrk/compact-simulator vitest

Basic Test Structure

import { describe, it, expect, beforeEach } from 'vitest';
import { ContractSimulator } from '@midnight-ntwrk/compact-simulator';
import { setNetworkId, NetworkId } from '@midnight-ntwrk/midnight-js-network-id';

describe('MyContract', () => {
  let simulator: ContractSimulator;

  beforeEach(() => {
    setNetworkId(NetworkId.Undeployed);
    simulator = new ContractSimulator(compiledContract);
  });

  it('should initialize with zero', async () => {
    expect(simulator.ledger.counter).toBe(0n);
  });

  it('should increment counter', async () => {
    await simulator.call('increment', {});
    expect(simulator.ledger.counter).toBe(1n);
  });
});

Testing Patterns

State Verification

it('should update ledger state', async () => {
  // Initial state
  expect(simulator.ledger.message).toBe('');

  // Call circuit
  await simulator.call('setMessage', { input: 'Hello' });

  // Verify state
  expect(simulator.ledger.message).toBe('Hello');
});

Error Testing

it('should reject invalid input', async () => {
  await expect(simulator.call('withdraw', { amount: 1000n })).rejects.toThrow('Assertion failed');
});

Privacy Testing

it('should not reveal private inputs', async () => {
  const result = await simulator.call('checkBalance', {
    balance: 1000n,
    required: 500n,
  });

  // Result is boolean, not actual balance
  expect(result).toBe(true);
  // Ledger should not contain balance
  expect(simulator.ledger.balance).toBeUndefined();
});

Best Practices

  • ✅ Test each circuit function independently
  • ✅ Verify state changes after each call
  • ✅ Test error conditions and edge cases
  • ✅ Mock witnesses for privacy testing
  • ✅ Use NetworkId.Undeployed for testing
  • ❌ Don't test proof generation (use simulator)
  • ❌ Don't rely on network in unit tests

Test Categories

CategoryTests
UnitIndividual circuit functions
IntegrationMulti-circuit workflows
StateLedger state transitions
ErrorAssertion failures
PrivacyData not leaked

Running Tests

# Run all tests
npm test

# Run with coverage
npm test -- --coverage

# Run specific test file
npm test -- counter.test.ts

# Watch mode
npm test -- --watch

Score

Total Score

45/100

Based on repository quality metrics

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

0/10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

0/5
タグ

1つ以上のタグが設定されている

0/5

Reviews

💬

Reviews coming soon