Skip to main content

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

NameTypeDescription
ixInteractioninteraction, either in form of a Promise or function

Returns

TypeDescription
ResponseObjectTransaction result

Usage


_49
import path from "path"
_49
import {
_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
_49
jest.setTimeout(10000)
_49
_49
describe("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: &Account){
_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

NameTypeDescription
ixInteractiontransaction, either in form of a Promise or function
message (optional)string or RegExpexpected error message provided as either a string equality or regular expression to match, matches any error by default

Returns

TypeDescription
ResponseObjectTransaction result

Usage


_49
import path from "path"
_49
import {
_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
_49
jest.setTimeout(10000)
_49
_49
describe("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: &Account){
_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

NameTypeDescription
ixInteractioninteraction, either in form of a Promise or function

Returns

TypeDescription
ResponseObjectTransaction result

Usage


_36
import path from "path"
_36
import {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
_36
jest.setTimeout(10000)
_36
_36
describe("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

NameTypeDescription
accountstringThe address or name of the account to check for the path.
pathstringThe path to check for.

Returns

TypeDescription
Promise<void>A Promise that resolves when the assertion is complete, or rejects with an error if it fails.

Usage


_30
import path from "path"
_30
import {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
_30
jest.setTimeout(10000)
_30
_30
describe("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

NameTypeDescription
accountstringThe 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.pathNamestringThe path of the storage value to retrieve.
params.keystring (optional)The key of the value to retrieve from the storage at the given path, if applicable.
expectanyThe expected value of the storage at the given path and key (if applicable).

Returns

TypeDescription
Promise<void>A Promise that resolves when the assertion is complete, or rejects with an error if it fails.