Skip to main content

Additional Examples

Metadata​

You may want to pass dictionaries as arguments to your Cadence code. The most common is metadata with \{String:String\} type


_28
import {executeScript} from "@onflow/flow-js-testing"
_28
_28
const main = async () => {
_28
const basePath = path.resolve(__dirname, "../cadence")
_28
_28
// Init framework
_28
await init(basePath)
_28
// Start emulator
_28
await emulator.start()
_28
_28
const code = `
_28
pub fun main(metadata: {String: String}): String{
_28
return metadata["name"]!
_28
}
_28
`
_28
_28
// Define arguments we want to pass
_28
const args = [{name: "Boris", nickname: "The Blade"}]
_28
_28
// If something goes wrong with script execution, the method will throw an error
_28
// so we need to catch it and proce
_28
const [name, err] = await shallResolve(executeScript({code, args}))
_28
console.log(name, err)
_28
_28
await emulator.stop()
_28
}
_28
_28
main()

If you need to pass an array of dictionaries, it's not that different. Just replace the args variable above with multiple values:


_10
const args = [
_10
// This is array of dictionaries
_10
[
_10
{name: "Boris", nickname: "The Blade"},
_10
{name: "Franky", nickname: "Four-Fingers"},
_10
],
_10
]

Or maybe you want to pass dictionary with type {String: [String]}:


_10
const args = [
_10
{
_10
names: ["Alice", "Bob", "Charlie"],
_10
},
_10
]

Framework will try to resolve the types to the best of its abilities. If you encounter an error for your use case, please create an issue here: https://github.com/onflow/flow-js-testing/issues