3 min read
Helper methods for Jest
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.
Ensure transaction did not throw and was sealed.
Name | Type | Description |
---|---|---|
ix | Interaction | interaction, either in form of a Promise or function |
Type | Description |
---|---|
ResponseObject | Transaction result |
1import path from "path"2import {3init,4emulator,5shallPass,6sendTransaction,7getAccountAddress,8} from "@onflow/flow-js-testing"910// We need to set timeout for a higher number, because some transactions might take up some time11jest.setTimeout(10000)1213describe("interactions - sendTransaction", () => {14// Instantiate emulator and path to Cadence files15beforeEach(async () => {16const basePath = path.resolve(__dirname, "./cadence")17await init(basePath)18return emulator.start()19})2021// Stop emulator, so it could be restarted22afterEach(async () => {23return emulator.stop()24})2526test("basic transaction", async () => {27const code = `28transaction(message: String){29prepare(singer: AuthAccount){30log(message)31}32}33`34const Alice = await getAccountAddress("Alice")35const signers = [Alice]36const args = ["Hello, Cadence"]3738const [txResult, error] = await shallPass(39sendTransaction({40code,41signers,42args,43})44)4546// Transaction result will hold status, events and error message47console.log(txResult, error)48})49})
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.
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 |
Type | Description |
---|---|
ResponseObject | Transaction result |
1import path from "path"2import {3init,4emulator,5shallPass,6sendTransaction,7getAccountAddress,8} from "js-testing-framework"910// We need to set timeout for a higher number, cause some interactions might need more time11jest.setTimeout(10000)1213describe("interactions - sendTransaction", () => {14// Instantiate emulator and path to Cadence files15beforeEach(async () => {16const basePath = path.resolve(__dirname, "./cadence")17await init(basePath)18return emulator.start()19})2021// Stop emulator, so it could be restarted22afterEach(async () => {23return emulator.stop()24})2526test("basic transaction", async () => {27const code = `28transaction(message: String){29prepare(singer: AuthAccount){30panic("You shall not pass!")31}32}33`34const Alice = await getAccountAddress("Alice")35const signers = [Alice]36const args = ["Hello, Cadence"]3738const [txResult, error] = await shallRevert(39sendTransaction({40code,41signers,42args,43})44)4546// Transaction result will hold status, events and error message47console.log(txResult, error)48})49})
Ensure interaction resolves without throwing errors.
Name | Type | Description |
---|---|---|
ix | Interaction | interaction, either in form of a Promise or function |
Type | Description |
---|---|
ResponseObject | Transaction result |
1import path from "path"2import {init, emulator, shallPass, executeScript} from "js-testing-framework"34// We need to set timeout for a higher number, cause some interactions might need more time5jest.setTimeout(10000)67describe("interactions - sendTransaction", () => {8// Instantiate emulator and path to Cadence files9beforeEach(async () => {10const basePath = path.resolve(__dirname, "./cadence")11await init(basePath)12return emulator.start()13})1415// Stop emulator, so it could be restarted16afterEach(async () => {17return emulator.stop()18})1920test("basic script", async () => {21const code = `22pub fun main():Int{23return 4224}25`2627const [result, error] = await shallResolve(28executeScript({29code,30})31)3233expect(result).toBe(42)34expect(error).toBe(null)35})36})