Jest Helpers
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​
Type | Description |
---|---|
ResponseObject | Transaction result |
Usage​
_49import path from "path"_49import {_49 init,_49 emulator,_49 shallPass,_49 sendTransaction,_49 getAccountAddress,_49} from "@onflow/flow-js-testing"_49_49// We need to set timeout for a higher number, because some transactions might take up some time_49jest.setTimeout(10000)_49_49describe("interactions - sendTransaction", () => {_49 // Instantiate emulator and path to Cadence files_49 beforeEach(async () => {_49 const basePath = path.resolve(__dirname, "./cadence")_49 await init(basePath)_49 return emulator.start()_49 })_49_49 // Stop emulator, so it could be restarted_49 afterEach(async () => {_49 return emulator.stop()_49 })_49_49 test("basic transaction", async () => {_49 const code = `_49 transaction(message: String){_49 prepare(singer: AuthAccount){_49 log(message)_49 }_49 }_49 `_49 const Alice = await getAccountAddress("Alice")_49 const signers = [Alice]_49 const args = ["Hello, Cadence"]_49_49 const [txResult, error] = await shallPass(_49 sendTransaction({_49 code,_49 signers,_49 args,_49 })_49 )_49_49 // Transaction result will hold status, events and error message_49 console.log(txResult, error)_49 })_49})
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​
Type | Description |
---|---|
ResponseObject | Transaction result |
Usage​
_49import path from "path"_49import {_49 init,_49 emulator,_49 shallPass,_49 sendTransaction,_49 getAccountAddress,_49} from "js-testing-framework"_49_49// We need to set timeout for a higher number, cause some interactions might need more time_49jest.setTimeout(10000)_49_49describe("interactions - sendTransaction", () => {_49 // Instantiate emulator and path to Cadence files_49 beforeEach(async () => {_49 const basePath = path.resolve(__dirname, "./cadence")_49 await init(basePath)_49 return emulator.start()_49 })_49_49 // Stop emulator, so it could be restarted_49 afterEach(async () => {_49 return emulator.stop()_49 })_49_49 test("basic transaction", async () => {_49 const code = `_49 transaction(message: String){_49 prepare(singer: AuthAccount){_49 panic("You shall not pass!")_49 }_49 }_49 `_49 const Alice = await getAccountAddress("Alice")_49 const signers = [Alice]_49 const args = ["Hello, Cadence"]_49_49 const [txResult, error] = await shallRevert(_49 sendTransaction({_49 code,_49 signers,_49 args,_49 })_49 )_49_49 // Transaction result will hold status, events and error message_49 console.log(txResult, error)_49 })_49})
shallResolve(ix)​
Ensure interaction resolves without throwing errors.
Arguments​
Name | Type | Description |
---|---|---|
ix | Interaction | interaction, either in form of a Promise or function |
Returns​
Type | Description |
---|---|
ResponseObject | Transaction result |
Usage​
_36import path from "path"_36import {init, emulator, shallPass, executeScript} from "js-testing-framework"_36_36// We need to set timeout for a higher number, cause some interactions might need more time_36jest.setTimeout(10000)_36_36describe("interactions - sendTransaction", () => {_36 // Instantiate emulator and path to Cadence files_36 beforeEach(async () => {_36 const basePath = path.resolve(__dirname, "./cadence")_36 await init(basePath)_36 return emulator.start()_36 })_36_36 // Stop emulator, so it could be restarted_36 afterEach(async () => {_36 return emulator.stop()_36 })_36_36 test("basic script", async () => {_36 const code = `_36 access(all) fun main():Int{_36 return 42_36 }_36 `_36_36 const [result, error] = await shallResolve(_36 executeScript({_36 code,_36 })_36 )_36_36 expect(result).toBe(42)_36 expect(error).toBe(null)_36 })_36})
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_30// We need to set timeout for a higher number, cause some interactions might need more time_30jest.setTimeout(10000)_30_30describe("interactions - sendTransaction", () => {_30 // Instantiate emulator and path to Cadence files_30 beforeEach(async () => {_30 const basePath = path.resolve(__dirname, "./cadence")_30 await init(basePath)_30 return emulator.start()_30 })_30_30 // Stop emulator, so it could be restarted_30 afterEach(async () => {_30 return emulator.stop()_30 })_30_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 })_30_30 test("pass account name", async () => {_30 await shallHavePath("Alice", "/storage/flowTokenVault")_30 })_30 })_30})
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. |