Testing in Ts

Icon Link在 TS 中进行测试

正如在测试介绍 中所述,您可以自由地使用任何可用的 JS 框架来测试您的 Sway 和 TS-SDK 代码。下面是一个使用 Vitest 加载和测试合约的示例,但是任何测试工具的一般原理和步骤都是相同的。

这是一个简单的 Sway 程序,它接受一个输入然后返回它:

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

以下是使用传统的 Vitest 设置测试上述程序的 JavaScript 代码:

import { generateTestWallet } from '@fuel-ts/account/test-utils';
import { safeExec } from '@fuel-ts/errors/test-utils';
import { ContractFactory, Provider, toHex, 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 baseAssetId: string;
 
/**
 * @group node
 */
describe('ExampleContract', () => {
  beforeAll(async () => {
    const provider = await Provider.create(FUEL_NETWORK_URL);
    baseAssetId = provider.getBaseAssetId();
  });
  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,
    });
 
    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();
    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);
 
 
    // Call
    const { value } = await contract.functions.return_input(1337).call();
 
    // Assert
    expect(value.toHex()).toEqual(toHex(1337));
  });
});
Icon InfoCircle

注意: TS-SDK 最近迁移到了 Vitest,但它与 Jest 具有非常相似的 API,上面的示例同样适用于 Jest。