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.
shallPass(ix)
Ensure transaction did not throw and was sealed.
Arguments
Name | Type | Description |
---|
ix | Interaction | interaction, either in form of a Promise or function |
Returns
Usage
_49import path from "path"
_49} from "@onflow/flow-js-testing"
_49// We need to set timeout for a higher number, because some transactions might take up some time
_49jest.setTimeout(10000)
_49describe("interactions - sendTransaction", () => {
_49 // Instantiate emulator and path to Cadence files
_49 beforeEach(async () => {
_49 const basePath = path.resolve(__dirname, "./cadence")
_49 return emulator.start()
_49 // Stop emulator, so it could be restarted
_49 afterEach(async () => {
_49 return emulator.stop()
_49 test("basic transaction", async () => {
_49 transaction(message: String){
_49 prepare(singer: &Account){
_49 const Alice = await getAccountAddress("Alice")
_49 const signers = [Alice]
_49 const args = ["Hello, Cadence"]
_49 const [txResult, error] = await shallPass(
_49 // Transaction result will hold status, events and error message
_49 console.log(txResult, error)
shallRevert(ix, message)
Ensure interaction throws an error. Can test for specific error messages or catch any error message if message
is not provided.
Returns Promise, which contains result, when resolved.
Arguments
Name | Type | Description |
---|
ix | Interaction | transaction, either in form of a Promise or function |
message (optional) | string or RegExp | expected error message provided as either a string equality or regular expression to match, matches any error by default |
Returns
Usage
_49import path from "path"
_49} from "js-testing-framework"
_49// We need to set timeout for a higher number, cause some interactions might need more time
_49jest.setTimeout(10000)
_49describe("interactions - sendTransaction", () => {
_49 // Instantiate emulator and path to Cadence files
_49 beforeEach(async () => {
_49 const basePath = path.resolve(__dirname, "./cadence")
_49 return emulator.start()
_49 // Stop emulator, so it could be restarted
_49 afterEach(async () => {
_49 return emulator.stop()
_49 test("basic transaction", async () => {
_49 transaction(message: String){
_49 prepare(singer: &Account){
_49 panic("You shall not pass!")
_49 const Alice = await getAccountAddress("Alice")
_49 const signers = [Alice]
_49 const args = ["Hello, Cadence"]
_49 const [txResult, error] = await shallRevert(
_49 // Transaction result will hold status, events and error message
_49 console.log(txResult, error)
shallResolve(ix)
Ensure interaction resolves without throwing errors.
Arguments
Name | Type | Description |
---|
ix | Interaction | interaction, either in form of a Promise or function |
Returns
Usage
_36import path from "path"
_36import {init, emulator, shallPass, executeScript} from "js-testing-framework"
_36// We need to set timeout for a higher number, cause some interactions might need more time
_36jest.setTimeout(10000)
_36describe("interactions - sendTransaction", () => {
_36 // Instantiate emulator and path to Cadence files
_36 beforeEach(async () => {
_36 const basePath = path.resolve(__dirname, "./cadence")
_36 return emulator.start()
_36 // Stop emulator, so it could be restarted
_36 afterEach(async () => {
_36 return emulator.stop()
_36 test("basic script", async () => {
_36 access(all) fun main():Int{
_36 const [result, error] = await shallResolve(
_36 expect(result).toBe(42)
_36 expect(error).toBe(null)
shallHavePath(account, path)
Asserts that the given account has the given path enabled.
Arguments
Name | Type | Description |
---|
account | string | The address or name of the account to check for the path. |
path | string | The path to check for. |
Returns
Type | Description |
---|
Promise<void> | A Promise that resolves when the assertion is complete, or rejects with an error if it fails. |
Usage
_30import path from "path"
_30import {init, emulator, shallPass, executeScript} from "js-testing-framework"
_30// We need to set timeout for a higher number, cause some interactions might need more time
_30jest.setTimeout(10000)
_30describe("interactions - sendTransaction", () => {
_30 // Instantiate emulator and path to Cadence files
_30 beforeEach(async () => {
_30 const basePath = path.resolve(__dirname, "./cadence")
_30 return emulator.start()
_30 // Stop emulator, so it could be restarted
_30 afterEach(async () => {
_30 return emulator.stop()
_30 describe("check path with Jest helper", () => {
_30 test("pass account address", async () => {
_30 const Alice = await getAccountAddress("Alice")
_30 await shallHavePath(Alice, "/storage/flowTokenVault")
_30 test("pass account name", async () => {
_30 await shallHavePath("Alice", "/storage/flowTokenVault")
shallHaveStorageValue(account, params)
Asserts that the given account has the expected storage value at the given path.
Arguments
Name | Type | Description |
---|
account | string | The address or name of the account to check for the storage value. |
params | {pathName: string, key?: string, expect: any} | An object containing the path name, optional key, and expected value of the storage at the given path. |
params.pathName | string | The path of the storage value to retrieve. |
params.key | string (optional) | The key of the value to retrieve from the storage at the given path, if applicable. |
expect | any | The expected value of the storage at the given path and key (if applicable). |
Returns
Type | Description |
---|
Promise<void> | A Promise that resolves when the assertion is complete, or rejects with an error if it fails. |