Writing Tests with Blueprint
Overview
Test toolkit (usually sandbox) is already included in the TypeScript SDK named Blueprint. You can create a demo project and launch the default test with two steps:
- Create a new Blueprint project:
npm create ton@latest MyProject
- Run a test:
cd MyProject
npx blueprint test
As a result you'll see the corresponding output in the terminal window:
% npx blueprint test
> MyProject@0.0.1 test
> jest
PASS tests/Main.spec.ts
Main
✓ should deploy (127 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.224 s, estimated 2 s
Ran all test suites.
Basic Usage
Testing of smart contracts allows for the coverage of security, optimization of gas spending, and examination of edge cases. Writing tests in Blueprint (based on Sandbox) works through defining arbitrary actions with the contract and comparing test results with the expected result, for example:
it('should execute with success', async () => { // description of the test case
const res = await main.sendMessage(sender.getSender(), toNano('0.05')); // performing an action with contract main and saving result in res
expect(res.transactions).toHaveTransaction({ // configure the expected result with expect() function
from: main.address, // set expected sender for transaction we want to test matcher properties from
success: true // set the desirable result using matcher property success
});
printTransactionFees(res.transactions); // print table with details on spent fees
});