Icon LinkTesting with Jest

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 Jest, 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 ExampleContract {
fn return_input(input: u64) -> u64;
}
 
impl ExampleContract for Contract {
fn return_input(input: u64) -> u64 {
	input
}
}

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

import { generateTestWallet } from '@fuel-ts/wallet/test-utils';
import fs from 'fs';
import { ContractFactory, Provider, toHex, NativeAssetId } from 'fuels';
import path from 'path';
 
import { ExampleContractAbi__factory } from './example-contract-types';
 
describe('ExampleContract', () => {
	it('should return the input', async () => {
const provider = new Provider('http://127.0.0.1:4000/graphql');
const wallet = await generateTestWallet(provider, [[1_000, NativeAssetId]]);
 
// Deploy
const bytecode = fs.readFileSync(path.join(__dirname, '../out/debug/example-contract.bin'));
const factory = new ContractFactory(bytecode, ExampleContractAbi__factory.abi, wallet);
const contract = await factory.deployContract();
 
// 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
const contractInstance = ExampleContractAbi__factory.connect(contract.id, wallet);
const { value: v2 } = await contractInstance.functions.return_input(1337).call();
expect(v2.toHex()).toBe(toHex(1337));
	});
});