Skip to main content
Version: Next

Template Code

The Flow JS Testing Framework is essentially a set of helper methods. They can be used in an 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.

getTemplate(file, addressMap, byAddress)

Returns Cadence template as string with addresses replaced using addressMap

NameTypeOptionalDescription
filestringrelative (to the place from where the script was called) or absolute path to the file containing the code
addressMapAddressMapobject to use for address mapping of existing deployed contracts. Default: {}
byAddressbooleanwhether addressMap is {name:address} or {address:address} type. Default: false

Returns

TypeDescription
stringcontent of a specified file

Usage


_12
import path from "path"
_12
import {init, getTemplate} from "@onflow/flow-js-testing"
_12
_12
const main = async () => {
_12
const basePath = path.resolve(__dirname, "../cadence")
_12
init(basePath)
_12
_12
const template = await getTemplate("../cadence/scripts/get-name.cdc")
_12
console.log({template})
_12
}
_12
_12
main()

getContractCode(name, addressMap)

Returns Cadence template from file with name in _basepath_/contracts folder

Arguments

NameTypeOptionalDescription
namestringname of the contract template
addressMapAddressMapobject to use for address mapping of existing deployed contracts

Returns

TypeDescription
stringCadence template code for specified contract

Usage


_23
import path from "path"
_23
import {init, emulator, getContractCode} from "@onflow/flow-js-testing"
_23
_23
const main = async () => {
_23
const basePath = path.resolve(__dirname, "../cadence")
_23
_23
await init(basePath)
_23
await emulator.start()
_23
_23
// Let's assume we need to import MessageContract
_23
await deployContractByName({name: "MessageContract"})
_23
const MessageContract = await getContractAddress("MessageContract")
_23
const addressMap = {MessageContract}
_23
_23
const contractTemplate = await getContractCode("HelloWorld", {
_23
MessageContract,
_23
})
_23
console.log({contractTemplate})
_23
_23
await emulator.stop()
_23
}
_23
_23
main()

getTransactionCode(name, addressMap)

Returns Cadence template from file with name in _basepath_/transactions folder

Arguments

NameTypeOptionalDescription
namestringname of the transaction template
addressMapAddressMapobject to use for address mapping of existing deployed contracts

Returns

TypeDescription
stringCadence template code for specified transaction

Usage


_24
import path from "path"
_24
import {init, emulator, getTransactionCode} from "@onflow/flow-js-testing"
_24
_24
const main = async () => {
_24
const basePath = path.resolve(__dirname, "../cadence")
_24
_24
await init(basePath)
_24
await emulator.start()
_24
_24
// Let's assume we need to import MessageContract
_24
await deployContractByName({name: "MessageContract"})
_24
const MessageContract = await getContractAddress("MessageContract")
_24
const addressMap = {MessageContract}
_24
_24
const txTemplate = await getTransactionCode({
_24
name: "set-message",
_24
addressMap,
_24
})
_24
console.log({txTemplate})
_24
_24
await emulator.stop()
_24
}
_24
_24
main()

getScriptCode(name, addressMap)

Returns Cadence template from file with name in _basepath_/scripts folder

Arguments

NameTypeOptionalDescription
namestringname of the script template
addressMapAddressMapobject to use for address mapping of existing deployed contracts

Returns

TypeDescription
stringCadence template code for specified script

Usage


_24
import path from "path"
_24
import {init, emulator, getScriptCode} from "@onflow/flow-js-testing"
_24
_24
const main = async () => {
_24
const basePath = path.resolve(__dirname, "../cadence")
_24
_24
await init(basePath)
_24
await emulator.start()
_24
_24
// Let's assume we need to import MessageContract
_24
await deployContractByName({name: "MessageContract"})
_24
const MessageContract = await getContractAddress("MessageContract")
_24
const addressMap = {MessageContract}
_24
_24
const scriptTemplate = await getScriptCode({
_24
name: "get-message",
_24
addressMap,
_24
})
_24
_24
console.log({scriptTemplate})
_24
await emulator.stop()
_24
}
_24
_24
main()

Examples

If you don't have any contract dependencies, you can use those methods without specifying address map as second parameter.


_19
import path from "path"
_19
import {
_19
init,
_19
getContractCode,
_19
getTransactionCode,
_19
getScriptCode,
_19
} from "@onflow/flow-js-testing"
_19
_19
const main = async () => {
_19
const basePath = path.resolve(__dirname, "../cadence")
_19
await init(basePath)
_19
_19
const contractWallet = await getContractCode({name: "Wallet"})
_19
const txGetCapability = await getTransactionCode({name: "get-capability"})
_19
const scriptGetBalance = await getScriptCode({name: "get-balance"})
_19
_19
console.log({contractWallet, txGetCapability, scriptGetBalance})
_19
}
_19
main()