Jest Helpers

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.

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

1
import path from "path"
2
import {
3
init,
4
emulator,
5
shallPass,
6
sendTransaction,
7
getAccountAddress,
8
} from "@onflow/flow-js-testing"
9
10
// We need to set timeout for a higher number, because some transactions might take up some time
11
jest.setTimeout(10000)
12
13
describe("interactions - sendTransaction", () => {
14
// Instantiate emulator and path to Cadence files
15
beforeEach(async () => {
16
const basePath = path.resolve(__dirname, "./cadence")
17
await init(basePath)
18
return emulator.start()
19
})
20
21
// Stop emulator, so it could be restarted
22
afterEach(async () => {
23
return emulator.stop()
24
})
25
26
test("basic transaction", async () => {
27
const code = `
28
transaction(message: String){
29
prepare(singer: AuthAccount){
30
log(message)
31
}
32
}
33
`
34
const Alice = await getAccountAddress("Alice")
35
const signers = [Alice]
36
const args = ["Hello, Cadence"]
37
38
const [txResult, error] = await shallPass(
39
sendTransaction({
40
code,
41
signers,
42
args,
43
})
44
)
45
46
// Transaction result will hold status, events and error message
47
console.log(txResult, error)
48
})
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

1
import path from "path"
2
import {
3
init,
4
emulator,
5
shallPass,
6
sendTransaction,
7
getAccountAddress,
8
} from "js-testing-framework"
9
10
// We need to set timeout for a higher number, cause some interactions might need more time
11
jest.setTimeout(10000)
12
13
describe("interactions - sendTransaction", () => {
14
// Instantiate emulator and path to Cadence files
15
beforeEach(async () => {
16
const basePath = path.resolve(__dirname, "./cadence")
17
await init(basePath)
18
return emulator.start()
19
})
20
21
// Stop emulator, so it could be restarted
22
afterEach(async () => {
23
return emulator.stop()
24
})
25
26
test("basic transaction", async () => {
27
const code = `
28
transaction(message: String){
29
prepare(singer: AuthAccount){
30
panic("You shall not pass!")
31
}
32
}
33
`
34
const Alice = await getAccountAddress("Alice")
35
const signers = [Alice]
36
const args = ["Hello, Cadence"]
37
38
const [txResult, error] = await shallRevert(
39
sendTransaction({
40
code,
41
signers,
42
args,
43
})
44
)
45
46
// Transaction result will hold status, events and error message
47
console.log(txResult, error)
48
})
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

1
import path from "path"
2
import {init, emulator, shallPass, executeScript} from "js-testing-framework"
3
4
// We need to set timeout for a higher number, cause some interactions might need more time
5
jest.setTimeout(10000)
6
7
describe("interactions - sendTransaction", () => {
8
// Instantiate emulator and path to Cadence files
9
beforeEach(async () => {
10
const basePath = path.resolve(__dirname, "./cadence")
11
await init(basePath)
12
return emulator.start()
13
})
14
15
// Stop emulator, so it could be restarted
16
afterEach(async () => {
17
return emulator.stop()
18
})
19
20
test("basic script", async () => {
21
const code = `
22
pub fun main():Int{
23
return 42
24
}
25
`
26
27
const [result, error] = await shallResolve(
28
executeScript({
29
code,
30
})
31
)
32
33
expect(result).toBe(42)
34
expect(error).toBe(null)
35
})
36
})