Skip to content

Testing

Proving that your contract does what it should, before it holds anyone’s SIGNA. Tests run in a fraction of a second, inside the browser, without a blockchain, and you can run them again after every change.

A test file (.test.ts) sits next to the contract. Each test:

  1. sets up a scenario: a list of transactions, each with a block height, a sender and an amount (and optionally a message);
  2. loads the contract into a simulated chain and runs the scenario;
  3. checks the outcome: contract variables, map entries, the transactions the contract sent.

Tests are written in TypeScript with the familiar describe / it / expect style of Vitest, and use the signum-smartc-testbed package to drive the contract. The Studio runs them itself; there is nothing to install.

Add File → Test (.test.ts) creates a starter file. If the folder has a contract, the starter already imports it:

import { describe, it, expect, beforeEach } from "vitest";
import { SimulatorTestbed, type TransactionObj } from "signum-smartc-testbed";
import ContractCode from "./my-contract.smart.c?raw";
// Transactions sent to the contract. Block 1 activates it.
const Scenario: TransactionObj[] = [
{ blockheight: 1, amount: 2_0000_0000n, sender: 10n, recipient: 1n },
];
describe("my-contract", () => {
let testbed: SimulatorTestbed;
beforeEach(() => {
testbed = new SimulatorTestbed(Scenario).loadContract(ContractCode).runScenario();
});
it("activates", () => {
expect(testbed.getTransactions().length).toBeGreaterThan(0);
});
});

Things to know:

  • Import the contract as text with the ?raw suffix: ./tip-jar.smart.c?raw. You can also import your own .ts helper files from the same project.
  • Amounts are in planck and written as bigint: 1_0000_0000n is 1 SIGNA. A transaction must carry at least the contract’s activation amount, or the contract doesn’t run.
  • Who is who: the first contract you load gets the account id 1n, and its creator is 555n unless you pass another one: loadContract(code, { creator: 777n }).
  • Messages: pass a method code and its arguments with messageHex: asHexMessage([1n, 100n]) (import asHexMessage from signum-smartc-testbed), or text with messageText.
Call Returns
getContractMemoryValue("totalTips") A contract variable (struct fields as currentTx_amount)
getContractMapValue(key1, key2) A map entry, 0n if unset
getContractMapValues(key1) All entries under key1
getTransactions() Every transaction on the simulated chain
getTransactionsSentByContract(blockheight) What the contract sent in a block
getAccount(id) An account and its balance
sendTransactionAndGetResponse([tx]) Sends more transactions, returns the contract’s answer

The assertions available are toBe, toEqual, toStrictEqual, toBeDefined, toBeUndefined, toBeNull, toBeTruthy, toBeFalsy, toBeGreaterThan, toBeLessThan, toContain, toHaveLength, toMatchObject and toThrow, each with .not. Besides describe and it (or test) there are .skip, .only, it.todo and the beforeAll / afterAll / beforeEach / afterEach hooks.

For every method of your contract, aim for at least:

  1. the happy path: correct input gives the expected state;
  2. the permission check: someone who may not call it changes nothing;
  3. invalid input: zero, negative or out-of-range values;
  4. edge cases: limits, off-by-one, an empty balance.

The Tip Jar tests are a small example of 1 and 2.

  • Run in the toolbar runs the whole file.
  • The run icons in the margin run a single test or all tests of one describe.
  • The Results panel lists every test as passed, failed, skipped or timed out. A failed assertion shows what was expected and what was received; click it to go to the line.
  • The workflow rail shows the outcome for the project: ✓ 3 green, 1 failed or no tests.

After a run, the Studio shows values at the end of the lines of the test: what a variable held, what a call returned. Click a value to inspect it in the Value panel. When a line ran several times (in a loop or a shared helper), the panel shows the last run.

Select a test and click Simulate: the simulator replays the transactions this test sent, so you can step through the contract line by line. Assertions don’t re-evaluate while you step. See Simulating & debugging.

To set breakpoints in the test code with your browser’s DevTools, tick Debug run (DevTools) and press Run. Open DevTools first; it can’t attach to a run that has already started. Your test file appears in the Sources panel under its project path. In this mode tests run in the page itself, so a contract that loops forever freezes the tab.