Template Code

How to manage load Cadence 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

1
import path from "path"
2
import {init, getTemplate} from "@onflow/flow-js-testing"
3
4
const main = async () => {
5
const basePath = path.resolve(__dirname, "../cadence")
6
init(basePath)
7
8
const template = await getTemplate("../cadence/scripts/get-name.cdc")
9
console.log({template})
10
}
11
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

1
import path from "path"
2
import {init, emulator, getContractCode} from "@onflow/flow-js-testing"
3
4
const main = async () => {
5
const basePath = path.resolve(__dirname, "../cadence")
6
7
await init(basePath)
8
await emulator.start()
9
10
// Let's assume we need to import MessageContract
11
await deployContractByName({name: "MessageContract"})
12
const MessageContract = await getContractAddress("MessageContract")
13
const addressMap = {MessageContract}
14
15
const contractTemplate = await getContractCode("HelloWorld", {
16
MessageContract,
17
})
18
console.log({contractTemplate})
19
20
await emulator.stop()
21
}
22
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

1
import path from "path"
2
import {init, emulator, getTransactionCode} from "@onflow/flow-js-testing"
3
4
const main = async () => {
5
const basePath = path.resolve(__dirname, "../cadence")
6
7
await init(basePath)
8
await emulator.start()
9
10
// Let's assume we need to import MessageContract
11
await deployContractByName({name: "MessageContract"})
12
const MessageContract = await getContractAddress("MessageContract")
13
const addressMap = {MessageContract}
14
15
const txTemplate = await getTransactionCode({
16
name: "set-message",
17
addressMap,
18
})
19
console.log({txTemplate})
20
21
await emulator.stop()
22
}
23
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

1
import path from "path"
2
import {init, emulator, getScriptCode} from "@onflow/flow-js-testing"
3
4
const main = async () => {
5
const basePath = path.resolve(__dirname, "../cadence")
6
7
await init(basePath)
8
await emulator.start()
9
10
// Let's assume we need to import MessageContract
11
await deployContractByName({name: "MessageContract"})
12
const MessageContract = await getContractAddress("MessageContract")
13
const addressMap = {MessageContract}
14
15
const scriptTemplate = await getScriptCode({
16
name: "get-message",
17
addressMap,
18
})
19
20
console.log({scriptTemplate})
21
await emulator.stop()
22
}
23
24
main()

Examples

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

1
import path from "path"
2
import {
3
init,
4
getContractCode,
5
getTransactionCode,
6
getScriptCode,
7
} from "@onflow/flow-js-testing"
8
9
const main = async () => {
10
const basePath = path.resolve(__dirname, "../cadence")
11
await init(basePath)
12
13
const contractWallet = await getContractCode({name: "Wallet"})
14
const txGetCapability = await getTransactionCode({name: "get-capability"})
15
const scriptGetBalance = await getScriptCode({name: "get-balance"})
16
17
console.log({contractWallet, txGetCapability, scriptGetBalance})
18
}
19
main()