Icon HelpCircleForumIcon Link

⌘K

Icon HelpCircleForumIcon Link
Testing in Ts

Icon LinkTesting in TS

As noted in the testing intro , you are free to test your Sway and TS-SDK code with any JS framework available. Below we have an example of how to load and test a contract using Vitest, but the general principles and steps are the same for any testing harness.

Here is a simple Sway program that takes an input and then returns it:

contract;
 
abi DemoContract {
    fn return_input(input: u64) -> u64;
}
 
impl DemoContract for Contract {
    fn return_input(input: u64) -> u64 {
        input
    }
}

Here is JavaScript code testing the above program using a conventional Vitest setup:

import { generateTestWallet } from '@fuel-ts/account/test-utils';
import { safeExec } from '@fuel-ts/errors/test-utils';
import type { BN } from 'fuels';
import {
  ContractFactory,
  Provider,
  toHex,
  BaseAssetId,
  Wallet,
  FUEL_NETWORK_URL,
  Address,
} from 'fuels';
 
import storageSlots from '../contract/out/release/demo-contract-storage_slots.json';
 
import { DemoContractAbi__factory } from './contract-types';
import bytecode from './contract-types/DemoContractAbi.hex';
import type { PredicateAbiInputs } from './predicate-types';
import { PredicateAbi__factory } from './predicate-types';
import { ScriptAbi__factory } from './script-types';
 
let gasPrice: BN;
 
/**
 * @group node
 */
describe('ExampleContract', () => {
  beforeAll(async () => {
    const provider = await Provider.create(FUEL_NETWORK_URL);
    ({ minGasPrice: gasPrice } = provider.getGasConfig());
  });
  it('with imported storage slots', async () => {
    const provider = await Provider.create(FUEL_NETWORK_URL);
    const wallet = await generateTestWallet(provider, [[500_000, BaseAssetId]]);
 
    import storageSlots from './contract/out/debug/demo-contract-storage_slots.json';
 
    const contract = await DemoContractAbi__factory.deployContract(bytecode, wallet, {
      storageSlots,
      gasPrice,
    });
 
    expect(contract.id).toBeTruthy();
  });
  it('should return the input', async () => {
    const provider = await Provider.create(FUEL_NETWORK_URL);
    const wallet = await generateTestWallet(provider, [[500_000, BaseAssetId]]);
 
    // Deploy
    const factory = new ContractFactory(bytecode, DemoContractAbi__factory.abi, wallet);
    const contract = await factory.deployContract({ gasPrice });
    const contractId = contract.id;
 
    // Call
    const { value } = await contract.functions.return_input(1337).call();
 
    // Assert
    expect(value.toHex()).toEqual(toHex(1337));
 
    // You can also make a call using the factory
    import { DemoContractAbi__factory } from './types';
 
    const contractInstance = DemoContractAbi__factory.connect(contractId, wallet);
    const { value: v2 } = await contractInstance.functions.return_input(1337).call();
    expect(v2.toHex()).toBe(toHex(1337));
  });
 
  it('deployContract method', async () => {
    const provider = await Provider.create(FUEL_NETWORK_URL);
    const wallet = await generateTestWallet(provider, [[500_000, BaseAssetId]]);
 
    import { DemoContractAbi__factory } from './types';
    import bytecode from './types/DemoContractAbi.hex';
 
    // Deploy
    const contract = await DemoContractAbi__factory.deployContract(bytecode, wallet, { gasPrice });
 
    // Call
    const { value } = await contract.functions.return_input(1337).call();
 
    // Assert
    expect(value.toHex()).toEqual(toHex(1337));
  });
});
Icon InfoCircle

Note: The TS-SDK has recently migrated to Vitest however it follows a very similar API to Jest, and the above example applies to Jest also.