Asentum

Build on Asentum

Testing Contracts

Fast feedback for builders · Estimated read time: 6 minutes

TL;DR

Two layers of tests get you where you need to be: in-process unit tests that run the VM directly (no network, millisecond feedback) and integration tests that deploy against a real chain (live testnet or a local node) via the SDK. Both layers use standard JavaScript test runners: Vitest, Jest, or Node's built-in runner.

Unit tests in-process

The @asentum/vm package is the same VM the nodes run. You can deploy, call methods and check storage in memory, with no network and no node. It is not on npm yet: build it from the AsentumChain repo (packages/vm, then pnpm --filter @asentum/vm build) and add it to your project as a file: dependency.

import { test, expect } from 'vitest';
import { Vm } from '@asentum/vm';

// The VM reads and writes storage through an adapter you pass in.
// Values are always strings, exactly as on chain.
class MemoryStorage {
  data = new Map();
  get(k) { return this.data.get(k); }
  set(k, v) { this.data.set(k, v); }
  delete(k) { return this.data.delete(k); }
  has(k) { return this.data.has(k); }
  keys(prefix = '') { return [...this.data.keys()].filter((k) => k.startsWith(prefix)).sort(); }
}

const source = `
({
  init() {
    assert(!storage.has('n'), 'already initialized');
    storage.set('n', '0');
  },
  inc() {
    const next = BigInt(storage.get('n')) + 1n;
    storage.set('n', next.toString());
    emit('Incremented', { n: next.toString() });
  },
  get() { return storage.get('n'); },
})`;

// msg.sender and msg.contractAddress are lowercase hex strings inside
// the VM. blockTimestamp is in milliseconds.
const context = {
  sender: '0x' + '11'.repeat(20),
  contractAddress: '0x' + '22'.repeat(20),
  value: 0n,
  blockNumber: 1n,
  blockTimestamp: 1_760_000_000_000n,
  chainId: 1423n,
};

test('counter increments', () => {
  const vm = new Vm();
  const storage = new MemoryStorage();
  const call = (method, args = []) =>
    vm.call({ source, context, storage, gasLimit: 1_000_000n }, method, args);

  vm.deploy({ source, context, storage, gasLimit: 1_000_000n });
  call('init');
  call('inc');
  const r = call('inc');

  expect(call('get').returnValue).toBe('2');   // a string, not 2n
  expect(r.events).toEqual([{ name: 'Incremented', data: { n: '2' } }]);
  expect(() => call('init')).toThrow('already initialized');
});

The VM is synchronous, so no await is needed. A method that throws (or fails an assert) makes vm.call throw. To test as a different sender, pass a different context.sender. This is the right layer for logic tests, invariants, and gas sanity checks: fast, deterministic, no network.

Integration tests against testnet

For end-to-end confidence, deploy against the live testnet using the SDK. These are slower (2-second target block time, slower under load) but exercise the full pipeline: real Dilithium3 signing, real Aura and GRANDPA consensus, real explorer receipts.

import { test, expect } from 'vitest';
import { AsentumClient, AsentumWallet } from '@asentum/sdk';
import { readFileSync } from 'node:fs';

test('end-to-end counter', async () => {
  const client = new AsentumClient('https://explorer.asentum.com');
  const wallet = AsentumWallet.create(client);
  await wallet.requestFaucet();

  const source = readFileSync('./contracts/counter.js', 'utf8');
  const c = await wallet.deploy(source);
  await (await c.send('init')).wait();
  await (await c.send('inc')).wait();
  // view returns what the method returns: here the stored string
  expect(await c.view('get')).toBe('1');
}, { timeout: 30_000 });

Using Vitest

Vitest is the recommended runner: fast, ESM-native, TypeScript out of the box. A minimal setup:

pnpm add -D vitest @asentum/sdk
pnpm add -D file:../AsentumChain/packages/vm
pnpm vitest

Jest works the same way: Asentum packages are pure ESM with no Jest-specific config needed.

Testing patterns

  • Assert events. Every send returns a SentTx; tx.wait() resolves to a receipt with an events array. Assert on them for state-transition correctness.
  • Assert reverts. Wrap a failing call in expect().rejects. The revert message comes back on the error.
  • Test access control. Instantiate multiple AsentumWallets to simulate different msg.sender values.
  • Test cross-contract. Deploy two contracts in the same test; verify that E(other).method() returns what you expect.

Running in CI

Unit tests are fine to run in CI unchanged; no network needed. For integration tests against the live testnet, use a funded CI wallet or drip from the faucet at the start of each run. Budget several seconds per tx (2-second target blocks plus latency; real block time varies with load). For heavy CI, consider running a local full node.

Read next