I'm trying to follow this example on how to create a unit test for a cross-contract call.
I have a cross-contract call like this
// Class with cross-contract calls
export class CalculatorApi {
add(a: string, b: string): ContractPromise {
let args: AddArgs = { a, b };
let promise = ContractPromise.create(OTHER_CONTRACT, "addLongNumbers", args.encode(), 100000000000000);
return promise;
}
}
// function to unit test
export function calculate(a: string , b: string): void { // Need to return void according to current spec
let calculator = new CalculatorApi();
let promise = calculator.add(a, b);
promise.returnAsResult();
}
You may notice this function returns void, which is a bit confusing because the contract is returning a promise. This is because it's calling a function elsewhere and the compiler thinks it's void.
(In future releases this will be changed.)
And a test like this
// VS Code complains about the async word
it("add one digit", async function() {
const params = { a: "1", b: "99" };
const result = await contract.calculate(params);
expect(result).toBe("100");
});
However, the above test complains about the syntax with the following error:
An async function or method must return a 'Promise'. Make sure you have a declaration for 'Promise' or include 'ES2015' in your '--lib' option.ts(2697)
I'm not sure, but maybe it has something to do with my tsconfig.json file, which looks like this:
{
"extends": "../node_modules/assemblyscript/std/assembly.json",
"include": ["./**/*.ts"]
}
I tried to add the following, but it didn't work
"compilerOptions": {
"lib": ["es2020"]
}