⚠️ Required: Your project must follow the required structure and it must be initialized to use the following functions.
Resolves name alias to a Flow address (0x prefixed) under the following conditions:
- If account with specific name has not been previously accessed framework will first create a new one and then store it under provided alias.
- Next time when you call this method, it will grab exactly the same account. This allows you to create several accounts first and then use them throughout your code, without worrying that accounts match or trying to store/handle specific addresses.
| Name | Type | Description |
|---|---|---|
alias |
string | The alias to reference or create. |
| Type | Description |
|---|---|
| Address | 0x prefixed address of aliased account |
import { getAccountAddress } from "flow-js-testing";
const main = async () => {
const Alice = await getAccountAddress("Alice");
console.log({ Alice });
};
main();Deploys contract code located inside a Cadence file. Returns the transaction result.\
Props object accepts following fields:
| Name | Type | Optional | Description |
|---|---|---|---|
name |
string | name of the file in contracts folder (with .cdc extension) and name of the contract (please note those should be the same) |
|
to |
Address | ✅ | (optional) account address, where contract will be deployed. If this is not specified, framework will create new account with randomized alias. |
addressMap |
AddressMap | ✅ | (optional) object to use for address mapping of existing deployed contracts |
args |
[Any] | ✅ | (optional) arguments, which will be passed to contract initializer. (optional) if template does not expect any arguments. |
update |
boolean | ✅ | (optional) whether to update deployed contract. Default: false |
| Type | Description |
|---|---|
| ResponseObject | Result of the deploying transaction. |
Usage:
import path from "path";
import { init, emulator, deployContractByName } from "flow-js-testing";
const main = async () => {
const basePath = path.resolve(__dirname, "../cadence");
const port = 8080;
init(basePath, port);
await emulator.start(port);
// We will deploy our contract to the address that corresponds to "Alice" alias
const to = await getAccountAddress("Alice");
// We assume there is a file on "../cadence/contracts/Wallet.cdc" path
const name = "Wallet";
// Arguments will be processed and type matched in the same order as they are specified
// inside of a contract template
const args = [1337, "Hello", { name: "Alice" }];
try {
const deploymentResult = await deployContractByName({ to, name });
console.log({ deploymentResult });
} catch (e) {
// If we encounter any errors during teployment, we can catch and process them here
console.log(e);
}
await emulator.stop();
};
main();In a bit more rare case you would want to deploy contract code not from existing template file, but rather
from string representation of it. deployContract method will help you achieve this.
Deploys contract code specified as string. Returns the transaction result.
Props object accepts the following fields:
| Name | Type | Optional | Description |
|---|---|---|---|
contractCode |
string | string representation of contract | |
name |
string | name of the contract to be deployed. Should be the same as the name of the contract provided in contractCode |
|
to |
Address | ✅ | account address, where contract will be deployed. If this is not specified, framework will create new account with randomized alias. |
addressMap |
AddressMap | ✅ | object to use for import resolver. Default: {} |
args |
[Any] | ✅ | arguments, which will be passed to contract initializer. Default: [] |
update |
boolean | ✅ | whether to update deployed contract. Default: false |
| Type | Description |
|---|---|
| ResponseObject | Result of the deploying transaction. |
import path from "path";
import { init, emulator, deployContract } from "flow-js-testing";
const main = async () => {
const basePath = path.resolve(__dirname, "../cadence");
const port = 8080;
await init(basePath, { port });
await emulator.start(port, false);
const to = await getAccountAddress("Alice");
const name = "Wallet";
const contractCode = `
pub contract Wallet{
init(amount: Int){
log(amount)
log("Thank you for the food!")
}
}
`;
const args = [1337];
try {
const deploymentResult = await deployContractByName({
to,
name,
contractCode,
args,
});
console.log({ deploymentResult });
} catch (e) {
console.log(e);
}
await emulator.stop();
};
main();While framework have automatic import resolver for Contracts you might want to know where it's currently deployed.
We provide a method getContractAddress for this.
Returns address of the account where the contract is currently deployed.
| Name | Type | Description |
|---|---|---|
name |
string | name of the contract |
| Type | Description |
|---|---|
| Address | 0x prefixed address |
import { getContractAddress } from "flow-js-testing";
const main = async () => {
const basePath = path.resolve(__dirname, "../cadence");
const port = 8080;
await init(basePath, { port });
await emulator.start(port, false);
// if we ommit "to" it will be deployed to a newly generated address with "unknown" alias
await deployContractByName({ name: "HelloWorld" });
const contract = await getContractAddress("HelloWorld");
console.log({ contract });
};
main();📣 Framework does not support contracts with identical names deployed to different accounts. While you can deploy contract to a new address, the internal system, which tracks where contracts are deployed, will only store last address.
Flow Javascript Testing Framework exposes emulator singleton allowing you to run and stop emulator instance
programmatically. There are two methods available on it.
Starts emulator on a specified port. Returns Promise.
| Name | Type | Optional | Description |
|---|---|---|---|
port |
number | ✅ | number representing a port to use for access API. Default: 8080 |
logging |
boolean | ✅ | whether log messages from emulator shall be added to the output |
| Type | Description |
|---|---|
| Promise | Promise, which resolves to true if emulator started successfully |
import { emulator, init } from "flow-js-testing";
describe("test setup", () => {
// Instantiate emulator and path to Cadence files
beforeEach(async () => {
const basePath = path.resolve(__dirname, "../cadence");
const port = 8080;
await init(basePath, { port });
// Start emulator instance on port 8080
await emulator.start(port);
});
});Stops emulator instance. Returns Promise.
This method does not expect any arguments.
| Type | Description |
|---|---|
| Promise | Promise, which resolves to true if emulator stopped without issues |
import { emulator, init } from "flow-js-testing";
describe("test setup", () => {
// Instantiate emulator and path to Cadence files
beforeEach(async () => {
const basePath = path.resolve(__dirname, "../cadence");
const port = 8080;
await init(basePath, { port });
await emulator.start(port);
});
// Stop emulator, so it could be restarted
afterEach(async () => {
await emulator.stop();
});
});Set logging flag on emulator, allowing to temporally enable/disable logging.
| Name | Type | Description |
|---|---|---|
newState |
boolean | Enable/disable logging |
Method does not return anything.
import { emulator, init } from "flow-js-testing";
describe("test setup", () => {
// Instantiate emulator and path to Cadence files
beforeEach(async () => {
const basePath = path.resolve(__dirname, "../cadence");
const port = 8080;
await init(basePath, { port });
await emulator.start(port);
});
// Stop emulator, so it could be restarted
afterEach(async () => {
await emulator.stop();
});
test("basic test", async () => {
// Turn on logging from begining
emulator.setLogging(true);
// some asserts and interactions
// Turn off logging for later calls
emulator.setLogging(false);
// more asserts and interactions here
});
});Some actions on the network will require account to have certain amount of FLOW token - transaction and storage fees, account creation, etc.
Framework provides a method to query balance with getFlowBalance and mint new tokens via mintFlow. You can find
information how to use them below.
Fetch current FlowToken balance of account specified by address
| Name | Type | Description |
|---|---|---|
address |
Address | address of the account to check |
| Type | Description |
|---|---|
| string | UFix64 amount of FLOW tokens stored in account storage represented as string |
import { init, emulator, getFlowBalance } from "flow-js-testing";
const main = async () => {
const basePath = path.resolve(__dirname, "../cadence");
const port = 8080;
await init(basePath, { port });
await emulator.start(port);
const Alice = await getAccountAddress("Alice");
try {
const result = await getFlowBalance(Alice);
console.log({ result });
} catch (e) {
console.log(e);
}
await emulator.stop();
};
main();Sends transaction to mint specified amount of FLOW token and send it to recipient.
⚠️ Required: Framework shall be initialized withinitmethod for this method to work.
| Name | Type | Description |
|---|---|---|
recipient |
Address | address of the account to check |
amount |
string | UFix64 amount of FLOW tokens to mint and send to recipient |
| Type | Description |
|---|---|
| ResponseObject | Transaction result |
import { init, emulator, mintFlow } from "flow-js-testing";
const main = async () => {
const basePath = path.resolve(__dirname, "../cadence");
const port = 8080;
await init(basePath, { port });
await emulator.start(port);
const Alice = await getAccountAddress("Alice");
const amount = "42.0";
try {
const mintResult = await mintFlow(Alice);
console.log({ mintResult });
} catch (e) {
console.log(e);
}
await emulator.stop();
};
main();For Framework to operate properly you need to initialize it first.
You can do it with provided init method.
Initializes framework variables and specifies port to use for HTTP and grpc access.
port is set to 8080 by default. grpc port is calculated to 3569 + (port - 8080) to allow multiple instances
of emulator to be run in parallel.
| Name | Type | Optional | Description |
|---|---|---|---|
bastPath |
string | path to the folder holding all Cadence template files | |
options |
object | ✅ | options object to use during initialization |
| Name | Type | Optional | Description |
|---|---|---|---|
port |
✅ | http port for access node | |
pkey |
✅ | private key for service account |
| Type | Description |
|---|---|
| Promise | Promise, which resolves to true if framework was initialized properly |
import path from "path";
import { init } from "flow-js-testing";
describe("test setup", () => {
beforeEach(async () => {
const basePath = path.resolve(__dirname, "../cadence");
await init(basePath);
// alternatively you can pass specific port
// await init(basePath, {port: 8085})
});
});In order to simplify the process even further we've created several Jest-based methods, which will help you to catch thrown errors and ensure your code works as intended.
Ensure transaction does not throw and sealed.
| Name | Type | Description |
|---|---|---|
ix |
Interaction | interaction, either in form of a Promise or function |
| Type | Description |
|---|---|
| ResponseObject | Transaction result |
import path from "path";
import {
init,
emulator,
shallPass,
sendTransaction,
getAccountAddress,
} from "js-testing-framework";
// We need to set timeout for a higher number, cause some interactions might need more time
jest.setTimeout(10000);
describe("interactions - sendTransaction", () => {
// Instantiate emulator and path to Cadence files
beforeEach(async () => {
const basePath = path.resolve(__dirname, "./cadence");
const port = 8080;
await init(basePath, { port });
return emulator.start(port);
});
// Stop emulator, so it could be restarted
afterEach(async () => {
return emulator.stop();
});
test("basic transaction", async () => {
const code = `
transaction(message: String){
prepare(singer: AuthAccount){
log(message)
}
}
`;
const Alice = await getAccountAddress("Alice");
const signers = [Alice];
const args = ["Hello, Cadence"];
const txResult = await shallPass(
sendTransaction({
code,
signers,
args,
}),
);
// Transaction result will hold status, events and error message
console.log(txResult);
});
});Ensure interaction throws an error. You might want to use this to test incorrect inputs.
| Name | Type | Description |
|---|---|---|
ix |
Interaction | transaction, either in form of a Promise or function |
| Type | Description |
|---|---|
| ResponseObject | Transaction result |
import path from "path";
import {
init,
emulator,
shallPass,
sendTransaction,
getAccountAddress,
} from "js-testing-framework";
// We need to set timeout for a higher number, cause some interactions might need more time
jest.setTimeout(10000);
describe("interactions - sendTransaction", () => {
// Instantiate emulator and path to Cadence files
beforeEach(async () => {
const basePath = path.resolve(__dirname, "./cadence");
const port = 8080;
await init(basePath, { port });
return emulator.start(port);
});
// Stop emulator, so it could be restarted
afterEach(async () => {
return emulator.stop();
});
test("basic transaction", async () => {
const code = `
transaction(message: String){
prepare(singer: AuthAccount){
panic("You shall not pass!")
}
}
`;
const Alice = await getAccountAddress("Alice");
const signers = [Alice];
const args = ["Hello, Cadence"];
const txResult = await shallRevert(
sendTransaction({
code,
signers,
args,
}),
);
// Transaction result will hold status, events and error message
console.log(txResult);
});
});Ensure interaction resolves without throwing errors.
| Name | Type | Description |
|---|---|---|
ix |
Interaction | interaction, either in form of a Promise or function |
| Type | Description |
|---|---|
| InteractionResult | Interaction result |
import path from "path";
import { init, emulator, shallPass, executeScript } from "js-testing-framework";
// We need to set timeout for a higher number, cause some interactions might need more time
jest.setTimeout(10000);
describe("interactions - sendTransaction", () => {
// Instantiate emulator and path to Cadence files
beforeEach(async () => {
const basePath = path.resolve(__dirname, "./cadence");
const port = 8080;
await init(basePath, { port });
return emulator.start(port);
});
// Stop emulator, so it could be restarted
afterEach(async () => {
return emulator.stop();
});
test("basic script", async () => {
const code = `
pub fun main():Int{
return 42
}
`;
const result = await shallResolve(
executeScript({
code,
}),
);
expect(result).toBe(42);
});
});It is often the case that you need to query current state of the network. For example, to check balance of the account, read public value of the contract or ensure that user has specific resource in their storage.
We abstract this interaction into single method called executeScript. Method have 2 different signatures.
⚠️ Required: Your project must follow the required structure it must be initialized to use the following functions.
Provides explicit control over how you pass values.
props object accepts following fields:
| Name | Type | Optional | Description |
|---|---|---|---|
code |
string | ✅ | string representation of Cadence script |
name |
string | ✅ | name of the file in scripts folder to use (sans .cdc extension) |
args |
array | ✅ | an array of arguments to pass to script. Optional if script does not expect any arguments. |
⚠️ Required: Eithercodeornamefield shall be specified. Method will throw an error if both of them are empty. Ifnamefield provided, framework will source code from file and override value passed viacodefield.
| Type | Description |
|---|---|
| ResponseObject | Script result |
import path from "path";
import { init, emulator, executeScript } from "flow-js-testing";
const main = async () => {
const basePath = path.resolve(__dirname, "../cadence");
const port = 8080;
// Init framework
init(basePath, { port });
// Start emulator
await emulator.start(port);
// Define code and arguments we want to pass
const code = `
pub fun main(message: String): Int{
log(message)
return 42
}
`;
const args = ["Hello, from Cadence"];
// If something wrong with script execution method will throw an error,
// so we need to catch it and process
try {
const result = await executeScript({ code, args });
console.log({ result });
} catch (e) {
console.error(e);
}
// Stop emulator instance
await emulator.stop();
};
main();This signature provides simplified way of executing a script, since most of the time you will utilize existing Cadence files.
| Name | Type | Optional | Description |
|---|---|---|---|
name |
string | name of the file in scripts folder to use (sans .cdc extension) |
|
args |
array | ✅ | an array of arguments to pass to script. Optional if scripts don't expect any arguments. Default: [] |
| Type | Description |
|---|---|
| ResponseObject | Script result |
import path from "path";
import { init, emulator, executeScript } from "flow-js-testing";
const main = async () => {
const basePath = path.resolve(__dirname, "../cadence");
const port = 8080;
// Init framework
init(basePath, port);
// Start emulator
await emulator.start(port, false);
// Define arguments we want to pass
const args = ["Hello, from Cadence"];
// If something wrong with script execution method will throw an error,
// so we need to catch it and process
try {
// We assume there is a file `scripts/log-message.cdc` under base path
const result = await executeScript("log-message", args);
console.log({ result });
} catch (e) {
console.error(e);
}
await emulator.stop();
};
main();Another common case is necessity to mutate network state - sending tokens from one account to another, minting new
NFT, etc. Framework provides sendTransaction method to achieve this. This method has 2 different signatures.
⚠️ Required: Your project must follow the required structure it must be initialized to use the following functions.
Send transaction to network. Provides explicit control over how you pass values.
props object accepts following fields:
| Name | Type | Optional | Description |
|---|---|---|---|
code |
string | ✅ | string representation of Cadence transaction |
name |
string | ✅ | name of the file in transaction folder to use (sans .cdc extension) |
args |
[Any] | ✅ | an array of arguments to pass to transaction. Optional if transaction does not expect any arguments. |
signers |
[Address] | ✅ | an array of Address representing transaction autorizers |
addressMap |
AddressMap | ✅ | name/address map to use as lookup table for addresses in import statements |
⚠️ Required: Eithercodeornamefield shall be specified. Method will throw an error if both of them are empty. Ifnamefield provided, framework will source code from file and override value passed viacodefield.
📣 if
signersfield not provided, service account will be used to authorize the transaction.
📣 Pass
addressMaponly in cases, when you would want to override deployed contract. Otherwide imports can be resolved automatically without explicitly passing them viaaddressMapfield
| Type | Description |
|---|---|
| ResponseObject | Interaction result |
import path from "path";
import { init, emulator, sendTransaction, getAccountAddress } from "flow-js-testing";
const main = async () => {
const basePath = path.resolve(__dirname, "../cadence");
const port = 8080;
// Init framework
await init(basePath, { port });
// Start emulator
await emulator.start(port);
// Define code and arguments we want to pass
const code = `
transaction(message: String){
prepare(signer: AuthAccount){
log(message)
}
}
`;
const args = ["Hello, from Cadence"];
const Alice = await getAccountAddress("Alice");
const signers = [Alice];
// If something wrong with transaction execution method will throw an error,
// so we need to catch it and process
try {
const tx = await sendTransaction({ code, args, signers });
console.log({ tx });
} catch (e) {
console.error(e);
}
// Stop emulator instance
await emulator.stop();
};
main();This signature provides simplified way to send a transaction, since most of the time you will utilize existing Cadence files.
| Name | Type | Optional | Description |
|---|---|---|---|
name |
string | ✅ | name of the file in transaction folder to use (sans .cdc extension) |
signers |
array | ✅ | an array of Address representing transaction autorizers |
args |
[Any] | ✅ | an array of arguments to pass to transaction. Optional if transaction does not expect any arguments. |
| Type | Description |
|---|---|
| ResponseObject | Interaction result |
import path from "path";
import { init, emulator, sendTransaction } from "flow-js-testing";
const main = async () => {
const basePath = path.resolve(__dirname, "../cadence");
const port = 8080;
// Init framework
await init(basePath, { port });
// Start emulator
await emulator.start(port);
// Define arguments we want to pass
const args = ["Hello, Cadence"];
try {
const tx = await sendTransaction("log-message", args);
console.log({ tx });
} catch (e) {
console.error(e);
}
};
main();The philosophy behind Flow JS Testing Framework is to be a set of helper methods. They can be used in opinionated way, envisioned by Flow Team. Or they can work as building blocks, allowing developers to build their own testing solution as they see fit.
Following methods used inside other framework methods, but we feel encouraged to list them here as well.
Returns Cadence template as string with addresses replaced using addressMap
| Name | Type | Optional | Description |
|---|---|---|---|
file |
string | relative (to the place from where the script was called) or absolute path to the file containing the code | |
addressMap |
AddressMap | ✅ | object to use for address mapping of existing deployed contracts. Default: {} |
byAddress |
boolean | ✅ | whether addressMap is {name:address} or {address:address} type. Default: false |
| Type | Description |
|---|---|
| string | content of a specified file |
import path from "path";
import { init, getTemplate } from "flow-js-testing";
const main = async () => {
const basePath = path.resolve(__dirname, "../cadence");
init(basePath);
const template = await getTemplate("../cadence/scripts/get-name.cdc");
console.log({ template });
};
main();Returns Cadence template from file with name in _basepath_/contracts folder
| Name | Type | Optional | Description |
|---|---|---|---|
name |
string | name of the contract template | |
addressMap |
AddressMap | ✅ | object to use for address mapping of existing deployed contracts |
| Type | Description |
|---|---|
| string | Cadence template code for specified contract |
import path from "path";
import { init, emulator, getContractCode } from "flow-js-testing";
const main = async () => {
const basePath = path.resolve(__dirname, "../cadence");
const port = 8080;
await init(basePath, { port });
await emulator.start(port);
// Let's assume we need to import MessageContract
await deployContractByName({ name: "MessageContract" });
const MessageContract = await getContractAddress("MessageContract");
const addressMap = { MessageContract };
const contractTemplate = await getContractCode("HelloWorld", {
MessageContract,
});
console.log({ contractTemplate });
await emulator.stop();
};
main();Returns Cadence template from file with name in _basepath_/transactions folder
| Name | Type | Optional | Description |
|---|---|---|---|
name |
string | name of the transaction template | |
addressMap |
AddressMap | ✅ | object to use for address mapping of existing deployed contracts |
| Type | Description |
|---|---|
| string | Cadence template code for specified transaction |
import path from "path";
import { init, emulator, getTransactionCode } from "flow-js-testing";
const main = async () => {
const basePath = path.resolve(__dirname, "../cadence");
const port = 8080;
await init(basePath, { port });
await emulator.start(port);
// Let's assume we need to import MessageContract
await deployContractByName({ name: "MessageContract" });
const MessageContract = await getContractAddress("MessageContract");
const addressMap = { MessageContract };
const txTemplate = await getTransactionCode({
name: "set-message",
addressMap,
});
console.log({ txTemplate });
await emulator.stop();
};
main();Returns Cadence template from file with name in _basepath_/scripts folder
| Name | Type | Optional | Description |
|---|---|---|---|
name |
string | name of the script template | |
addressMap |
AddressMap | ✅ | object to use for address mapping of existing deployed contracts |
| Type | Description |
|---|---|
| string | Cadence template code for specified script |
import path from "path";
import { init, emulator, getScriptCode } from "flow-js-testing";
const main = async () => {
const basePath = path.resolve(__dirname, "../cadence");
const port = 8080;
await init(basePath, { port });
await emulator.start(port);
// Let's assume we need to import MessageContract
await deployContractByName({ name: "MessageContract" });
const MessageContract = await getContractAddress("MessageContract");
const addressMap = { MessageContract };
const scriptTemplate = await getScriptCode({
name: "get-message",
addressMap,
});
console.log({ scriptTemplate });
await emulator.stop();
};
main();Object to use for address mapping of existing deployed contracts. Key shall be string and value shall be Address
const addressMap = {
Messanger: "0x01cf0e2f2f715450",
Logger: "0x179b6b1cb6755e31",
};Interaction is a Promise or function returning a promise.
const ix = async () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(1337);
});
}, 500);
};