Testing
What this is for
Section titled “What this is for”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.
How it works
Section titled “How it works”A test file (.test.ts) sits next to the contract. Each test:
- sets up a scenario: a list of transactions, each with a block height, a sender and an amount (and optionally a message);
- loads the contract into a simulated chain and runs the scenario;
- 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.
Write a test
Section titled “Write a test”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
?rawsuffix:./tip-jar.smart.c?raw. You can also import your own.tshelper files from the same project. - Amounts are in planck and written as
bigint:1_0000_0000nis 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 is555nunless you pass another one:loadContract(code, { creator: 777n }). - Messages: pass a method code and its arguments with
messageHex: asHexMessage([1n, 100n])(importasHexMessagefromsignum-smartc-testbed), or text withmessageText.
Checking the result
Section titled “Checking the result”| 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.
What to test
Section titled “What to test”For every method of your contract, aim for at least:
- the happy path: correct input gives the expected state;
- the permission check: someone who may not call it changes nothing;
- invalid input: zero, negative or out-of-range values;
- edge cases: limits, off-by-one, an empty balance.
The Tip Jar tests are a small example of 1 and 2.
Run tests
Section titled “Run tests”- 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.
See the values
Section titled “See the values”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.
Step through what a test did
Section titled “Step through what a test did”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.
Debug the test code itself
Section titled “Debug the test code itself”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.