Additional Examples
Metadata​
You may want to pass dictionaries as arguments to your Cadence code. The most
common is metadata with \{String:String\}
type
_28import {executeScript} from "@onflow/flow-js-testing"_28_28const 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_28main()
If you need to pass an array of dictionaries, it's not that different. Just replace the args
variable above with
multiple values:
_10const 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]}:
_10const 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